gpt4 book ai didi

html - 为什么只有当html标签没有设置background-color属性时,html标签才取body标签的背景

转载 作者:可可西里 更新时间:2023-11-01 13:45:23 27 4
gpt4 key购买 nike

所以我注意到当我从 html 选择器中删除 backgroundbackground-color 属性时有 background background-color 属性出现在 body 选择器中,body 的背景会占据整个屏幕。

但是,当为 html 标签设置了 backgroundbackground-color 属性时,正文的 background 属性只影响 body 占据的屏幕部分,也就是 body 的内容,而不是整个屏幕。

这是什么原因(错误,决定...),我现在必须同时设置 htmlbody 的背景吗?谢谢。

最佳答案

这是设计使然。

根据 W3C Colors and backgrounds页面,

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.

或者,tl;dr:离开 <html>单独的背景和仅样式化主体将导致将背景分配给整个文档。这是建议。

原因可能是出于兼容性原因。很久以前,<body> , element 也起到了 Canvas 的作用,<html>元素并没有真正拥有自己的任何属性。所以你可以分配一个 bgcolor属性(property)给<body> (但不是 <html> )这将成为 Canvas 背景。

最后一个问题的答案是:仅当您愿意时。在大多数情况下,不设置 <html> 的样式根本就够了。特殊情况除外,例如如果您想要 body 周围有不同颜色的边缘,正如您注意到的那样。

背景示例 <body>仅:

body {background:lime}
Hello world

<html> 具有不同背景的示例和 <body> :

html {background:orange}
body {background:lime}
Hello world

关于html - 为什么只有当html标签没有设置background-color属性时,html标签才取body标签的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com