2017-12-27 891 views
1

我想強調所有鏈接,但是如果鏈接是h1,h2等,則不能。 這裏是一個簡單的CSS:選擇所有沒有h標籤的鏈接

#heatmapthemead-the-content-container .entry-content a { 
    text-decoration: underline; 
} 

這強調所有環節,包括H1,H2,H3 當我使用:不選擇器不工作,也H1,H2的逗留強調

#heatmapthemead-the-content-container .entry-content a:not(h1,h2,h3) { 
     text-decoration: none; 
    } 

我也使用!重要的:

h1,h2,h3,h4,h5,h6 a { 
text-decoration: none !important; 
} 

但h1,h2鏈接保持下劃線。 有沒有人有一招?

+0

首先,從具有最少特異性的CSS選擇器開始。是否有必要在CSS選擇器中包含'#heatmapthemead-content-container .entry-content'?這是否過分矯枉過正?會做一些簡單的工作,比如'h1 a'?根據需要始終以最小的特異性進行。 '重要的'通常用於非常特殊的情況,其中,這種情況似乎沒有資格。 – hungerstar

回答

0

你的最後一個例子有正確的想法,如果你沒有像這應該工作:

h1 a, h2 a, h3 a, h4 a, h5 a, h6 a { 
    text-decoration: none !important; 
} 
0

您不必使用:not。您只需選擇標籤但添加一個標籤,以便選擇鏈接的h1。通過執行以下它應該工作。

h1 a, h2 a, h3 a, h4 a, h5 a, h6 a { 
    text-decoration: none !important; 
} 

查看更多單擊here

2

沒有必要讓它!important。看下面的例子。

a{ 
 
    text-decoration: underline; 
 
} 
 

 
h1 a, 
 
h2 a, 
 
h3 a{ 
 
    text-decoration: none; 
 
}
<div> 
 
    <h1><a href="#">Sample</a></h1> 
 
    <p><a href="#">Sample</a></p> 
 
    <h2><a href="#">Sample</a></h2> 
 
    <div><a href="#">Sample</a></div> 
 
    <h3><a href="#">Sample</a></h3> 
 
</div>

你寫在錯誤的方式代碼,把它寫像

h1 a, h2 a, h3 a{ 
    text-decoration: none; 
} 

等。

而且這樣寫會使代碼更具可讀性,並且更容易找到你的錯誤。

h1 a, 
h2 a, 
h3 a{ 
    text-decoration: none; 
} 

它不會在輸出上產生任何影響。

+0

FWIW,在這個特定的例子中,'text-decoration:none;'選擇器不必在'text-decoration:underline;'選擇器之後,因爲它具有更高的特異性。如果他們具有相同的特徵,他們只需要追趕,然後梯級就會發揮作用,而最後宣佈的任何東西都會勝出。 – hungerstar

相關問題