在 CSS 中的 HTML 元素上设置背景时出现不需要的边距
因此众所周知,您可以在 HTML 标签和 BODY 标签中添加背景图片,从而获得双背景效果,而无需添加额外的 div 元素。我喜欢你可以这样做,但是当我尝试它时,额外的边距会添加到 BODY 元素的顶部。
我确实发现,如果您添加以下代码:
*{
margin: 0;
}
它将消除这个边距,但它也使它成为这样,所以我必须重置所有其他我不想消除边距的元素。
有谁知道一种方法来消除 HTML 标签边距。
这:
html {
margin: 0;
}
顺便说一句,不起作用。似乎是其他原因导致了额外的间距。
更新: 这是 JS Bin 示例。 http://jsbin.com/ebefah/2
我正在运行 HTML5 文档类型,并且我已经摆脱了身体上的边缘也是如此。
答案: 谢谢三十点。答案是 H1,因为利润率不断下降。一旦我摆脱了 H1 的顶部边距,一切就完美地工作了。再次感谢您!
So it is fairly well known that you can add a background image to the HTML tag and the BODY tag to get a double background effect without adding extra div elements. I love that you can do this, but when I attempt it extra margin gets added to the top of the BODY element.
I did find that if you added the following code:
*{
margin: 0;
}
it will get rid of this margin, but it also makes it so I have to reset all the other elements that I didn't want to get rid of margin on.
Does anyone know of a way to just get rid of the HTML tag margin.
This:
html {
margin: 0;
}
doesn't work BTW. It seems to be something else causing the extra spacing.
UPDATE:
Here is the JS Bin example. http://jsbin.com/ebefah/2
I am running an HTML5 doctype and I have gotten rid of the margin on the body as well.
ANSWER:
Thank you to thirtydot. The answer was the H1, due to collapsing margins. Once I got rid of the top margin on the H1 it all worked flawlessly. Thank you again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于折叠边距的影响,间隙来自
h1
(默认情况下有一些margin-top
)。阅读本文。
您可能只想设置<
h1
上的代码>margin-top: 0。The gap is coming from the
h1
(which has somemargin-top
by default), due to the effects of collapsing margins.Read this.
You possibly just want to set
margin-top: 0
on theh1
.正文有默认边距..
尝试
html,body{margin:0;padding:0}
There is a default margin to the body ..
Try
html,body{margin:0;padding:0}
使用 CSS 重置来标准化浏览器之间的默认样式。雅虎有一个不错的 CSS 重置。默认情况下,大多数元素都会在
h1
、body
、html< 等元素上应用
margin
/padding
/code> 等http://yuilibrary.com/yui/docs/cssreset/
Use a CSS reset to normalize default styles between browsers. Yahoo have a decent CSS reset. Most elements have
margin
/padding
applied by default on elements such ash1
,body
,html
etc.http://yuilibrary.com/yui/docs/cssreset/