css - CSS中不同背景颜色的表格标题单元格(th)

标签 css tableheader

我有以下 CSS 样式以正确的格式显示表格数据,但我想以不同的颜色显示表格标题( th )的替代背景......

如何仅在 CSS 下方修改以实现相同的效果
即每个 TH3,TH5 应该有蓝色背景(除了第一个 TH1 - 它将有默认的红色背景)
而 TH2,TH4,TH6 应该有黄色背景。

我已经尝试过第 n 个子选择器,并且在某处我读到了有关 th+th 两种方式都不起作用的信息。

 <style type="text/css">
    table {
           /*...all table attributes like fontsize etc*/
            font-size:11px;
            color:#333333;                
    }
    table th {
            /*...all th attributes like padding etc*/
            background-color:#d4e3e5;
            padding: 8px;
    }
    table td {
            /*...all td attributes like padding etc*/
            padding: 8px;
    }
    </style>

感谢所有回复,但第 n 个子选择器不起作用,我已经尝试过。是否有任何基本的方法来修改 CSS 并实现相同的目标?

最佳答案

fiddle
第n个 child 正在工作。

 th:nth-child(odd) {
     background-color:red; // Replace it with your desired color
 }

 th:nth-child(even) {
     background-color: yellow;
 }
如果您有进一步的问题,请发布您的 HTML 和 CSS。

关于css - CSS中不同背景颜色的表格标题单元格(th),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598601/

相关文章:

css - PhpStorm 将 Less 文件夹编译为 style.css

javascript - 当输入有焦点时保持 Div 的可见性

css - 当子宽度设置为 100% 时,子 div 仅右侧与父 div 发生奇怪的重叠

javascript - JQuery 在多个插件后改变宽度

function - SSRS在表头中使用函数

ios - 删除时多个原型(prototype)单元格标题会移动

objective-c - 从 UITableView 标题部分设置 UILabel 文本

javascript - 无法获得 html svg 中的输出

html - 在 div 内对齐表单和按钮

php - 按表头对 MySql 行进行排序