2015-07-19 53 views
1
<table id="table1"> 
    <colgroup><col><col><col><col></colgroup>  
    <tr><th class="green">th1</th><th>th2</th><th>th3</th><th>th4</th> </tr> 
    <tr><td class="green">Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td></tr> 
    <tr><td class="green">Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td></tr> 
    <tr><td class="green">Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td></tr> 
    <tr><td class="green">Cell1</td><td>Cell2</td><td>Cell3</td><td>Cell4</td></tr> 
</table> 

CSS:爲什麼表格單元格上的css類沒有優先級?

#table1 {width:100%; table-layout:fixed;} 
#table1 td, #table1 th{border:1px solid; background-color:yellow;} 
.green {background-color:green;} 

我試圖避免。綠色類重要的,因爲我會寫很多那些在我的網頁,我讀它是不好的做法! http://jsfiddle.net/y3cnc0ty/

回答

1

這是由於CSS「特異性」。 CSS選擇器有一個優先順序,ID比CLASS更重要。
你可以閱讀更多有關這對MDN

您可以用

#table1 .green {background-color:green;} 
1

克服這一嘗試使用此

table1的TR td.green {背景色:綠色;}

我不確定,但我相信既然你已經使用了黃色bg的深層參考,你需要一個嵌套在裏面的元素的類似或更深的參考。