将 CSS 应用于 html、body 和通用选择器 * 的区别?

发布于 2024-12-01 08:30:14 字数 264 浏览 0 评论 0原文

当这三个规则应用于同一个 HTML 文档时有何不同?

html {
    color: black;
    background-color: white;
}

body {
    color: black;
    background-color: white;
}

* {
    color: black;
    background-color: white;
}

How are these three rules different when applied to the same HTML document?

html {
    color: black;
    background-color: white;
}

body {
    color: black;
    background-color: white;
}

* {
    color: black;
    background-color: white;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-12-08 08:30:14
  1. html {
        颜色:黑色;
        背景颜色:白色;
    }
    

    此规则将颜色应用于 html 元素。 html 元素的所有后代都继承其 color(但不包括 background-color),包括 bodybody 元素没有默认背景颜色,这意味着它是透明的,因此 html 的背景将显示出来,除非您为 body 设置背景>.

    尽管 html 的背景绘制在整个视口上,但 html 元素本身不会自动跨越视口的整个高度;背景只是传播到视口。有关详细信息,请参阅此答案

  2. 正文 {
        颜色:黑色;
        背景颜色:白色;
    }
    

    此规则将颜色应用于 body 元素。 body 元素的所有后代都继承其 color

    html的背景自动传播到视口类似,body的背景也会自动传播到html,直到除非您也为 html 设置背景。有关说明,请参阅此答案。因此,如果您只需要一个背景(在通常情况下),无论您使用第一条规则还是第二条规则都不会产生任何真正的区别。

    但是,您可以将 htmlbody 的背景样式与其他技巧结合起来,以获得一些漂亮的背景效果,例如 我已经在这里完成了。请参阅上面链接的答案了解具体方法。

  3. <代码>* {
        颜色:黑色;
        背景颜色:白色;
    }
    

    此规则将颜色应用于每个元素,因此这两个属性都不是隐式继承的。但是您可以轻松地用其他任何规则覆盖此规则,包括上述两个规则中的任何一个,因为 * 在选择器特异性方面实际上没有任何意义。

    因为这会完全破坏任何通常继承的属性(例如 color)的继承链,所以在 * 规则中设置这些属性被认为是不好的做法,除非您有以这种方式破坏继承的非常充分理由(大多数涉及破坏继承的用例要求您仅对一个元素执行此操作,而不是所有元素)。

  1. html {
        color: black;
        background-color: white;
    }
    

    This rule applies the colors to the html element. All descendants of the html element inherit its color (but not background-color), including body. The body element has no default background color, meaning it's transparent, so html's background will show through until and unless you set a background for body.

    Although the background of html is painted over the entire viewport, the html element itself does not span the entire height of the viewport automatically; the background is simply propagated to the viewport. See this answer for details.

  2. body {
        color: black;
        background-color: white;
    }
    

    This rule applies the colors to the body element. All descendants of the body element inherit its color.

    Similarly to how the background of html is propagated to the viewport automatically, the background of body will be propagated to html automatically, until and unless you set a background for html as well. See this answer for an explanation. Because of this, if you only need one background (in usual circumstances), whether you use the first rule or the second rule won't make any real difference.

    You can, however, combine background styles for html and body with other tricks to get some nifty background effects, like I've done here. See the above linked answer for how.

  3. * {
        color: black;
        background-color: white;
    }
    

    This rule applies the colors to every element, so neither of the two properties is implicitly inherited. But you can easily override this rule with anything else, including either of the above two rules, as * has literally no significance in selector specificity.

    Because this breaks the inheritance chain completely for any property that is normally inherited such as color, setting those properties in a * rule is considered bad practice unless you have a very good reason to break inheritance this way (most use cases that involve breaking inheritance require you to do it for just one element, not all of them).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文