css设置上下样式覆盖的问题

html:
<div id="article">

<div id="subject">

<dl>123</dl>
</div>

<form action="">
<dl>456</dl>
</form>

</div>

#article #subject dl{

height: 100px;
line-height: 40px;
width: 100px;
background-color: red;
}
#article dl{

height: 40px;
line-height: 40px;
width: 200px;
background-color: blue;
}
以上的代码,为什么下面的样式无法覆盖上面的

#subject dl{

height: 100px;
line-height: 40px;
width: 100px;
background-color: red;
}
#article dl{

height: 40px;
line-height: 40px;
width: 200px;
background-color: blue;
}
以上的代码下面的css就会覆盖上面的?

选择器优先级问题:

    #article #subject dl的优先级比#subject dl优先级高,所以无法覆盖;

    #subject dl的优先级没有#article dl高,所以会覆盖;

为同一个元素设置多个样式时,此时哪个样式生效由选择器的优先级确定:

选择器的优先级(权重):

内联样式    1000

id选择器    100

类和伪类选择器    10

元素选择器    1

统配选择器    0

继承的样式    无

    当一个选择器中含有多个选择器时,需要将所有的选择器的优先级进行相加,然后再进行比较,优先级高的优先显示,选择器的计算不会超过其最大的数量级(10个id选择器的优先级

    不能达到1000)

    分组选择器(并集选择器)的优先级单独计算,不会相加。

    样式后面加!important,该样式获取最高优先级,内联样式不能加!important属性。

    样式相同的谁在下面执行谁(样式的覆盖)。

温馨提示:内容为网友见解,仅供参考
第1个回答  2019-04-11
很简单,你直接在这个div上添加样式就行了。

<div class="3" style="width:200px;......"></div>

你看一下那个div class 3 里面都定义了哪些。
比如定义了width height padding什么,你想调整哪个,就直接在上面的style后面加就行了,会覆盖,要注意一致就OK了。追问

我是想明白原理,#subject也是 #articel的子元素,dl是#subject和form的子元素,#article dl{}设置的样式按道理是都要覆盖第一个样式 #article #subject dl设置啊。

本回答被网友采纳
第2个回答  2021-01-24

Vue实践-CSS样式position/display/float属性对比使用

第3个回答  2019-05-26
直接在这个div上添加样式
相似回答