css选取器权重优先级
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">总结</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.常用的<span style="color: black;">选取</span>器权重优先级:!important>id>class>tag</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.!important<span style="color: black;">能够</span><span style="color: black;">提高</span>样式的优先级,<span style="color: black;">然则</span>不<span style="color: black;">意见</span><span style="color: black;">运用</span>,<span style="color: black;">那样</span>这一条简写的样式属性所<span style="color: black;">表率</span>的子属性都会被应用加上!important</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.<span style="color: black;">倘若</span>两条样式都<span style="color: black;">运用</span>!important,则权重值高的优先级更高</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.在css样式表中,同一个CSS样式你写了两次,后面的会覆盖前面的</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5.样式指向同一元素,权重规则生效,权重大的被应用</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">6.样式指向同一元素,权重规则生效,权重相<span style="color: black;">同期</span>,就近原则生效,后面定义的被应用</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">7.样式不指向同一元素时,权重规则失效,就近原则生效,离<span style="color: black;">目的</span>元素<span style="color: black;">近期</span>的样式被应用</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么是权重</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.权重决定了你css规则<span style="color: black;">怎么样</span>被浏览器解析直到生效。“css权重关系到你的css规则是<span style="color: black;">怎么样</span><span style="color: black;">表示</span>的”。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.当<span style="color: black;">非常多</span>的样式被应用到某一个元素上时,权重是一个决定哪种样式生效,<span style="color: black;">或</span>是优先级的过程。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.<span style="color: black;">每一个</span><span style="color: black;">选取</span>器都有自己的权重。你的每条css规则,都<span style="color: black;">包括</span>一个权重级别。 这个级别<span style="color: black;">是由于</span><span style="color: black;">区别</span>的<span style="color: black;">选取</span>器加权计算的,<span style="color: black;">经过</span>权重,<span style="color: black;">区别</span>的样式<span style="color: black;">最后</span>会<span style="color: black;">功效</span>到你的网页中 。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.<span style="color: black;">倘若</span>两个<span style="color: black;">选取</span>器<span style="color: black;">同期</span><span style="color: black;">功效</span>到一个元素上,权重高者生效。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">权重记忆口诀:从0<span style="color: black;">起始</span>,一个行内样式+1000,一个id<span style="color: black;">选取</span>器+100,一个属性<span style="color: black;">选取</span>器、class<span style="color: black;">或</span>伪类+10,一个元素<span style="color: black;">选取</span>器,<span style="color: black;">或</span>伪元素+1,通配符+0。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-ed4a36e29a401675f0ca958452b5324f_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如下</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic4.zhimg.com/80/v2-0950247ddb72a1c42c2c7ed8c463ea43_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">样式重复多写的<span style="color: black;">状况</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在css样式表中,同一个CSS样式你写了两次,后面的会覆盖前面的,在<span style="color: black;">研发</span>中基本不会<span style="color: black;">运用</span>。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">#box {
bac<span style="color: black;">公斤</span>round-color: green;
}
/* 这条生效 */
#box {
bac<span style="color: black;">公斤</span>round-color: blue;
}</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">区别</span>权重,则权重高的生效</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>权重高的样式生效</title>
<style>
/* 权重值:1 */
div{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round-color: red;
}
/* 权重值:10 */
.box2{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round-color: yellow;
}
/* 权重值:100 */
#box{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round-color: green;
}
</style>
</head>
<body>
<div id=box class=box2></div>
</body>
</html></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">!important<span style="color: black;">提高</span>样式优先级</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">!important的<span style="color: black;">功效</span>是<span style="color: black;">提高</span>了样式的优先级,<span style="color: black;">倘若</span>加了这句话优先级是最高的,<span style="color: black;">然则</span>不<span style="color: black;">意见</span><span style="color: black;">运用</span>。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><style>
div{
bac<span style="color: black;">公斤</span>round: blue !important;
}
#box{
bac<span style="color: black;">公斤</span>round-color: green;
}
</style>
</head>
<body>
<div id="box" style="bac<span style="color: black;">公斤</span>round-color: red;width: 100px;height: 100px;"></div>
</body></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">两种样式都<span style="color: black;">运用</span>!important</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>当两个样式都<span style="color: black;">运用</span>!important时</title>
<style>
.box{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round-color: red !important;
}
div{
width: 100px;
height: 100px;
bac<span style="color: black;">公斤</span>round-color: green !important;
}
</style>
</head>
<body>
<!-- 当两个样式都<span style="color: black;">运用</span>!important时,权重值大的优先级更高 -->
<div class=box></div>
</body>
</html></div>
我完全赞同你的观点,思考很有深度。 外贸B2B平台有哪些? 感谢你的精彩评论,为我的思绪打开了新的窗口。 你的见解真是独到,让我受益匪浅。 顶楼主,说得太好了!
页:
[1]