表奇数甚至td与多色

时间:2012-12-15 19:16:46

标签: html css

I have a table styled with CSS with even and oddtd偶数/奇数CSS代码:

#table_box tr:nth-child(odd) td { background-color:#ffffff } /*odd*/
#table_box tr:nth-child(even) td { background-color:#f5f9fa } /* even*/
#table_box tr:hover td { background-color:#fffbae; } /* hovering */

enter image description here

我希望有多个偶数/奇数颜色的行样式,如下图所示:
enter image description here

我可以用CSS做到这一点吗?我试图这样做,但颜色不显示,只是偶数/奇怪的风格。

2 个答案:

答案 0 :(得分:2)

使用此片段:
HTML更改:

        <tr>
                <td class="blueh2">1</td>
                <td class="blueh2">&#1777;&#1779;&#1785;&#1777;/&#1776;&#1780;/&#1779; &#1776;&#1785;:&#1780;&#1785;:&#1780;&#1783;</td>
                <td class="blueh2">&#1777;&#1779;&#1785;&#1777;/&#1776;&#1780;/&#1779; &#1777;&#1778;:&#1777;&#1785;:&#1780;&#1776;</td>
                <td class="greenh2">2.5</td>
                <td class="greenh2">2.5</td>
                <td class="purpleh2">6.75MB</td>
                <td class="purpleh2">54.13MB</td>
                <td class="purpleh2">60.87MB</td>
                <td class="purpleh2">60.87MB</td>
            </tr>

CSS更改:

#table_box tr:nth-child(odd) td { background-color:#ffffff } /*odd*/
#table_box tr:nth-child(even) td.greenh2 { background-color:#f00} /* even*/
#table_box tr:nth-child(even) td.blueh2 { background-color:#0f0} /* even*/
#table_box tr:nth-child(even) td.purpleh2 { background-color:#00f} /* even*/
#table_box tr:hover td { background-color:#fffbae; } /* hovering */

th.grey{
color:#7c7c7c!important;
}
th.blueh{
background-color:#174797;
color:#FFF!important;
}
th.blueh2{
background-color:#5492cf;
color:#FFF!important;
padding:3px!important;
}
th.greenh{
background-color:#579f0f;
color:#FFF!important;
}
th.greenh2{
background-color:#a4dd4f;
color:#FFF!important;
padding:3px!important;
}
th.purpleh{
background-color:#8e04ca;
color:#FFF!important;
}
th.purpleh2{
background-color:#d984fd;
color:#FFF!important;
padding:3px!important;
}

你需要再调整一下以适应你想要的输出,但我想总体思路很清楚 如果您愿意,也可以使用普通CSS而不更改HTML代码:

#table_box tr:nth-child(odd) td { background-color:#ffffff } /*odd*/
#table_box tr:nth-child(even) td:nth-child(1) { background-color:#f00} /* even*/
#table_box tr:nth-child(even) td:nth-child(2) { background-color:#0f0} /* even*/
#table_box tr:nth-child(even) td:nth-child(3) { background-color:#00f} /* even*/
#table_box tr:hover td { background-color:#fffbae; } /* hovering */

答案 1 :(得分:1)

查看<colgroup>代码。