本文介紹了CSS 直接后代 (>) 在選擇性方面沒有任何價值嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
鑒于以下類聲明和代碼...
Given the following class declarations and code...
.foo > a { color:green; }
.bar a { color:red; }
<div class="bar">
<div class="foo">
<a href="#">SOME LINK</a>
</div>
</div>
... 我認為鏈接會是綠色的,因為雖然兩個聲明都有一個類 (010) 和一個元素 (001),但 .foo
有直接后代選擇器.但是,唉,鏈接是紅色的.為什么?
... I thought that the link would be green because, while both declarations have a class (010) and an element (001), .foo
has the direct descendant selector. But alas, the link is red. Why?
推薦答案
>
對于 css 特異性沒有任何價值.
There's no value for >
for css specificity.
這兩種情況都有 11 個特異性值:
Both case have 11 value for specificity:
.foo > a { color:green; }/*specificity value is 11*/
.bar a { color:red; }/*specificity value is 11*/
<小時>
在您的情況下,您可以像這樣使用以獲得更大的特異性:
In your case you may use like this to have greater specificty:
.bar .foo > a { color:green; }/*greater specificity value is 21*/
.foo a { color:red; }/*specificity value is 11*/
<小時>
好的,我將在這里添加特異性的工作原理:
Ok, I'm going add here how specificity works:
Selector Specificity Specificity in large base
inline-style 1 0 0 0 1000
id selector 0 1 0 0 100
class,pseudo,attribute selector 0 0 1 0 10
type selector and pseudo elements 0 0 0 1 1
這篇關于CSS 直接后代 (>) 在選擇性方面沒有任何價值嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!