2016-01-22 75 views
0

爲什麼不是以下樣式生效?爲什麼一些CSS屬性不起作用?

.tags a{ 
    color: red; 
} 

我可以通過追加!important來強制它的財產。

代碼:http://codepen.io/haxan7/pen/QyQNBR

+4

* 「爲什麼它不生效?」 *答案是[CSS特異性(https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity)。換句話說,選擇器'.theme .content a'比'.tags a'更具體,因此'color:red'被覆蓋。 –

回答

4

以下兩個聲明都適用於該錨文本。

.theme .content a{ 
    color: green; 
} 

.tags a{ 
    color: red; 
} 

顏色「綠色」具有更多的特異性,然後顏色「紅色」,這就是爲什麼「綠色」得到應用。增加顏色「紅色」的特異性,然後應用。

.theme .content .tags a { 
color: red; 
} 
+0

這解決了我的問題。但是我想知道是否有另一種方式來做到這一點,這樣我的風格適用於'tags'div,即使它們在「.theme。content」div之外。 –

1

因爲你.theme .content a風格設置標籤顏色爲綠色。

變化這在你的代碼:

.theme .content a{ 
    color: red; 
} 
+0

我知道。我的問題是爲什麼在問題中引用的代碼沒有生效。 –