CSS 如何在css中制作没有边距的div标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14590331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:29:55  来源:igfitidea点击:

How to make a div tag without margins in css?

css

提问by Thomas-

Possible Duplicate:
Get a div to go across the whole page

可能的重复:
获取一个 div 来遍历整个页面

I need to make a div tag that will start from the beginning of the browser, not after a margin, for example when I make this code:

我需要制作一个 div 标签,该标签将从浏览器的开头开始,而不是在边距之后,例如当我制作以下代码时:

<html>
<head>
<title>CSS Test</title>
<style type="text/css">
div
{
width:100%;
height:100%;
border:1px solid black;
}
</style>
</head>
<body>
<div>
</div>
</body>
</html>

The div starts after a margin from top, left and right. But I want it to start with no margin, even when I make the margin:0px 0px; it still won't work. So, what do I do to make it with no margins and is that method useable with images too?

div 从顶部、左侧和右侧的边距开始。但我希望它开始时没有边距,即使我设置了边距:0px 0px; 它仍然不起作用。那么,我该怎么做才能使它没有边距,并且该方法也可用于图像?

回答by Joseph Silber

The browser's user-agent-styles automatically add padding to the bodyelement. Remove it:

浏览器的用户代理样式会自动向body元素添加填充。去掉它:

html, body {
    padding: 0;
}

You should look into normalize.css, which would fix all these tiny issues for you.

你应该研究一下normalize.css,它会为你解决所有这些小问题。

回答by bikeshedder

html, body {
    margin: 0;
    padding: 0;
}
div {
    margin: 0;
    border: 1px solid black;
    width: 100%;
    height: 100%;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
}

Without the box-sizingyou will end up with a div which is 100%+2px wide and 100%+2px tall.

如果没有,box-sizing你最终会得到一个 100%+2px 宽和 100%+2px 高的 div。