2013-10-14 234 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,以显示你的代码工作(改变悬停颜色,只是为了演示