为什么HTML正文背景颜色不遵守边距? [英] Why doesn't HTML body background-color obey margin?

查看:214
本文介绍了为什么HTML正文背景颜色不遵守边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据W3,元素的边距是透明的:



我建立了一个非常基本的HTML页面:

 < !DOCTYPE html> 
< html lang =en>
< head>
< title>文档< / title>
< style>
body {
background-color:red;
}
< / style>
< / head>
< body>< / body>
< / html>

在Chrome中,检查员报告body有8像素的边距:





但在页面上,页边距充满背景颜色! (请注意,我的书签栏和红色背景之间没有空格 - 我保证我没有滚动。





这是怎么回事? href =http://www.w3.org/TR/CSS2/colors.html#background =nofollow> CSS规范特例。


根元素的背景成为
画布的背景,并覆盖整个画布,在同一点处锚定(对于
'background-position')因为它会是
只绘制根元素本身。根元素不
再次绘制此背景。



对于HTML但是,我们建议作者为BODY元素指定
背景,而不是H TML元素对于
文档,其根元素是一个HTMLHTML元素或一个XHTML
html元素,其计算值为
'background-color'和' none'为'background-image',则用户代理必须使用
,而不是使用
元素的第一个HTMLBODY元素或XHTMLbody元素子元素的背景属性的计算值为画布绘制背景,并且不得为该子元素绘制背景
。这样的背景也必须固定在
相同的点上,如果它们只涂在根元素
元素上。


...大概是因为人们太习惯于糟糕的旧时代< body background =#ff0000>


According to w3, the margin of an element is transparent:

I've built a very basic HTML page:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <style>
        body {
            background-color: red;
        }
    </style>
</head>
<body></body>
</html> 

In Chrome, the inspector reports that "body" has an 8px margin:

But on the page, the margin is full of the background color! (Notice that there's no space between my bookmarks bar and the red background - I promise I haven't scrolled.

What's up with that?

解决方案

The CSS specification special cases it.

The background of the root element becomes the background of the canvas and covers the entire canvas, anchored (for 'background-position') at the same point as it would be if it was painted only for the root element itself. The root element does not paint this background again.

For HTML documents, however, we recommend that authors specify the background for the BODY element rather than the HTML element. For documents whose root element is an HTML "HTML" element or an XHTML "html" element that has computed values of 'transparent' for 'background-color' and 'none' for 'background-image', user agents must instead use the computed value of the background properties from that element's first HTML "BODY" element or XHTML "body" element child when painting backgrounds for the canvas, and must not paint a background for that child element. Such backgrounds must also be anchored at the same point as they would be if they were painted only for the root element.

… presumably because people were too used to <body background="#ff0000"> from the bad old days.

这篇关于为什么HTML正文背景颜色不遵守边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆