2013-10-14 155 views
12

我有一個div,我想覆蓋我的全局鏈接樣式。我有兩種鏈接樣式,一種是全球的,一種是特定的。下面的代碼:覆蓋HTML div內的鏈接樣式

A:link {text-decoration: none; color: #FF0000;} 
A:visited {text-decoration: none; color: #FF0000;} 
A:hover {text-decoration: none; color: #FF0000;} 
A:active {text-decoration: none; color: #FF0000;} 

#macrosectiontext 
{ 
    position:relative; 
    font:Arial, sans-serif; 
    text-align:center; 
    font-size:50px; 
    font-style: bold; 
    margin-top:245px; 
    opacity: 0.6; 
    background-color:transparent; 
} 

#macrosectiontext A:link {text-decoration: none; color: #000000;} 
#macrosectiontext A:visited {text-decoration: none; color: #FFFFFF;} 
#macrosectiontext A:hover {text-decoration: none; color: #FFFFFF;} 
#macrosectiontext A:active {text-decoration: none; color: #FFFFFF;} 

和我這樣使用DIV:

<div id="macrosectiontext"><a href="www.google.it">bla bla bla</a></div> 

但它似乎不起作用。 div仍繼承全局鏈接風格。

+0

我在您的HTML中沒有看到錨鏈接 –

+1

只需編寫#macrosectiontext {color:#000000};'這將起作用 –

+0

對我來說工作正常。這是一個[jsfiddle](http://jsfiddle.net/Xz8KQ/)。 –

回答

7
  1. 在CSS,我不會使用id「#macrosectiontext一個:鏈接...」的鏈接代碼,我會用一個類「.macrosectiontext」

  2. 使用較低的情況下,「一「而不是鏈接樣式中的」A「帽子

  3. 如果只使用樣式幾次,則可以在鏈接周圍使用span標籤,然後從span標籤中調用樣式代替div 。

+0

提到的第一點是關鍵。爲鏈接分配一個類並定義類內的顏色。涼! – spiderman

10

CSS在繼承上工作,所以你應該只覆蓋你想改變的屬性。

嘗試總是寫HTML & CSS小寫的,還是你的HTML和CSS是正確的

a:link, a:visited, a:hover, a:active { 
    text-decoration: none; color: #f00; 
} 

#macrosectiontext { 
    position:relative; 
    font:Arial, sans-serif; 
    text-align:center; 
    font-size:50px; 
    font-style: bold; 
    margin-top:245px; 
    opacity: 0.6; 
    background-color:transparent; 
} 

#macrosectiontext a:link {color: #000;} 
#macrosectiontext a:visited, #macrosectiontext a:hover, 
#macrosectiontext a:active { 
    color: #fff; 
} 

made a fiddle for you,以顯示你的代碼工作(改變懸停顏色,只是爲了演示