2009-11-16 51 views
13

我想將背景顏色應用於COLGROUP,但僅限於表格的TBODY。給定一個典型的日曆表,其結構如下所示:CSS選擇器將樣式應用於COLGROUP,但僅限於TBODY(不是THEAD)中?

<table> 
    <colgroup class="weekdays" span="5"/> 
    <colgroup class="weekend" span="2"/> 
    <thead> 
    <tr> 
     <td/><td/><td/><td/><td/> 

     <!-- I'd like the columns of the following two cells to be untouched. --> 
     <td/><td/> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td/><td/><td/><td/><td/> 

     <!-- I'd like selector to apply the style only to the columns of the following two cells --> 
     <td/><td/> 
    </tr> 
    ... 
    </tbody> 
</table> 

是否有一個CSS選擇器(或類似的東西),它可以讓我申請不同的風格在THEAD和TBODY的「週末」 COLGROUP?

回答

14

以您的HTML爲例,我不相信有一個選擇器可以實現您想要的功能。當涉及到CSS行爲時,colgroup元素是相當獨特的,因爲設置colgroup的樣式最終會影響(通過CSS繼承規則)與colgroup完全無關的元素。這意味着你不能使用選擇器,如colgroup.weekend tbody,因爲tbody不是colgroup的子集(反之亦然),它不會繼承這種方式。

我能來實現你想要的最接近的是以下幾點:

thead td { background-color:white; }
colgroup.weekend { background-color:red; }

thead tdcolgroup.weekend更具體的選擇,所以你最終「覆蓋」的COLGROUP的爲頭彩。

+0

好的,謝謝你的信息! – Manne 2009-11-16 12:56:32

相關問題