css - css中的级联规则问题

标签 css css-selectors


<style>
div#b {
background-color:blue;

}
#b {
background-color:red;

}
</style>

<div id='a'> div a
    <div id='b'>
    div b
    </div>
</div>

我对这种风格和这种 html 有两个问题。为什么 div b 需要蓝色。我想知道级联规则,我可以在哪里了解更多信息?我的第二个问题是我应该如何使用 css 使 div b 出现在 div a 中?

最佳答案

CSS 选择器根据特异性工作。更具体的选择器意味着该选择器中定义的规则将被使用以支持不太具体的选择器。

一般来说:

  • div、img等元素选择器的权重为1
  • 类选择器(例如 .myClass)的权重为 10
  • id 选择器,例如#myId,权重为 100

由此您可以很容易地确定上述失败的原因。

div#b = 101
#b = 100

101 > 100

关于css - css中的级联规则问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4473735/

相关文章:

html - SCSS 第 n 个子网格布局

css - 选择器无法正常工作

css - 将非 CSS 选择器与相邻选择器组合

sass - 在 Sass 中修改选择器的中间部分(添加/删除类等)

html - ie7 BUG : padding left and right with direction rtl

html - 为什么在打开网页时调整大小的桌面浏览器和移动设备在大小方面不一样?例子

html - 无法居中文本

javascript - CSS :hover not working in chrome for delayed dynamically added classes

html - 输入 HTML CSS 中的按钮

html - 按钮换行?