一次弄懂CSS选取器权重问题
<h2 style="color: black; text-align: left; margin-bottom: 10px;">前言</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 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>介绍css<span style="color: black;">选取</span>器权重,<span style="color: black;">期盼</span>对你有所<span style="color: black;">帮忙</span>。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">选取</span>器的种类</h2>!important内联样式ID<span style="color: black;">选取</span>器class<span style="color: black;">选取</span>器元素<span style="color: black;">选取</span>器通配符<span style="color: black;">选取</span>器<h2 style="color: black; text-align: left; margin-bottom: 10px;">权值和权级的概念</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>(<span style="color: black;">重视</span>:“权值”和“权级”的概念是为了更好的理解权重而提出的,并不是真是存在的)
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic3.zhimg.com/80/v2-fc467ae9254e881b8c0d4278f170db7e_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">选取</span>器的权重</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 有了权值和权级的概念以后,<span style="color: black;">咱们</span>就<span style="color: black;">能够</span>参考表格来对元素的权重进行理解。</p><span style="color: black;">选取</span>器的权重<span style="color: black;">能够</span>相加,例:
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-9338f6aaa538f741805ec077eb965649_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的权值为 = id<span style="color: black;">选取</span>器100+类<span style="color: black;">选取</span>器10+元素<span style="color: black;">选取</span>器1 = 111</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">权值相<span style="color: black;">同期</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因为</span>HTML代码的执行特点时从上往下执行,<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;"><img src="https://pic2.zhimg.com/80/v2-2ed91496ac225632edf3462ddc4a2529_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>器<span style="color: black;">同期</span>选中div元素,且color属性<span style="color: black;">出现</span>冲突,此时生效的是后面的class2,<span style="color: black;">因此呢</span>元素的前景色为蓝色</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">权值<span style="color: black;">区别</span>时</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 两种<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>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-52d529c798053fcbef9823ea1fc3dbb9_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>器的权值为1,大于通配<span style="color: black;">选取</span>器的权值0,<span style="color: black;">因此呢</span>对div生效的样式应该是元素<span style="color: black;">选取</span>器里的样式。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">权值跃迁</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> 权值跃迁讨论的问题是,当有11个元素<span style="color: black;">选取</span>器和1个类<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;"> 结论是:权级高的<span style="color: black;">选取</span>器的样式会生效。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">理解方式1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><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>论有多少个0级的<span style="color: black;">选取</span>器,生效的仍然是1级选择器,因 为<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</p> CSS里面的进制并不是10进制,而是256进制,<span style="color: black;">因此</span>10个低级<span style="color: black;">选取</span>器并<span style="color: black;">不可</span>等于1个高级<span style="color: black;">选取</span>器
<h2 style="color: black; text-align: left; margin-bottom: 10px;">其他<span style="color: black;">选取</span>器</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">伪类<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;">伪类<span style="color: black;">选取</span>器和属性<span style="color: black;">选取</span>器的权重相当于类<span style="color: black;">选取</span>器的权重,权级为2级。伪元素<span style="color: black;">选取</span>器</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">伪元素<span style="color: black;">选取</span>器的权重相当于元素<span style="color: black;">选取</span>器的权重,权级为1级。子代选择器后代<span style="color: black;">选取</span>器</p>子代和后代有可能是元素<span style="color: black;">选取</span>器、类<span style="color: black;">选取</span>器、id<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>分析。<h2 style="color: black; text-align: left; margin-bottom: 10px;">结语</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以上<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>麻烦点个赞~</p>
楼主继续加油啊!外链论坛加油! 你的见解独到,让我受益匪浅,非常感谢。 哈哈、笑死我了、太搞笑了吧等。 一看到楼主的气势,我就觉得楼主同在社区里灌水。 可以发布外链的网站 http://www.fok120.com/ 说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。
页:
[1]