2009-11-16 53 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

相关问题