2011-05-07 109 views
0

我只想改变标题文本的颜色,我不知道为什么下面的代码不起作用?CSS - 如何覆盖使用CSS?

<div class="mytitle"> 
this is the title in white 
</div> 

<div class="mytitle alt"> 
this is the title in red 
</div> 
<div class="mytitle alt2"> 
this is the title in black 
</div> 




.mytitle{ 
    font-family: Verdana, Geneva, sans-serif; 
    font-size: 37px !important; 
    line-height: 40px !important; 
    display:block; 
    font-weight: bold; 
    margin: 0 0 12px; 
    padding: 0 15px 5px 0 !important; 
    color: white; 
    width:500px; 
} 
.mytitle .alt{ 
    color: red; 
} 
.mytitle .alt2{ 
    color: black; 
} 

编辑:它只会使用“mytitle”标记,它不会使用alt或alt2,为什么?

回答

5

您需要删除的类定义之间的空间:

.mytitle.alt 
+0

这是我的错别字,实际的CSS有一个空间。 – 001 2011-05-07 14:46:37

+0

001,*删除*空格。 .mytitle .alt(带空格)适用于.alt INSIDE .mytitle,而.mytitle.alt(不含空格)适用于具有这两个类的元素。 – 2011-05-07 14:48:43

+0

@ 001当我修复它时,它适用于我:http://jsfiddle.net/Q77pF/ – 2011-05-07 14:49:03

1

简单删除.mytitle

.alt{ color: red;}  
.alt2{ color: black;} 

,你会得到所有的ALT/ALT2有色,或

.mytitle.alt{ color: red; }  
.mytitle.alt2{ color: black;} 
两者在一起的

+0

非常感谢!有用! – 001 2011-05-07 15:02:15