2011-05-07 102 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