仅将背景颜色应用于父表格

时间:2012-03-31 06:27:52

标签: css css3 html-table

我有一个表结构,我无法访问jsp文件来添加类文件。我必须通过CSS管理它。在这种情况下,我需要为第一个表应用背景颜色。不要嵌套表格。我们怎样才能用CSS做到这一点?示例:http://jsfiddle.net/qdDnJ/

5 个答案:

答案 0 :(得分:2)

根据我的理解,你可以这样写:

tr th{
    background:red;
}
tr table th{
    background:none;
}

选中此http://jsfiddle.net/qdDnJ/2/

答案 1 :(得分:1)

将第一张桌子的th与第二张桌子的th区分开来。

评论后编辑:

请参见此处http://jsfiddle.net/qdDnJ/25/

我假设div是第一个表的父容器。

您可以将其替换为表格的父级。

e.g。如果body是父级,则css应为,

    body > table > tbody > tr > th {
      background-color:red;
    }

答案 2 :(得分:0)

你可以这样做:

table th:first-child {
    background: red;
}
table table th:first-child {
    background: none;
}​

我只是给外表一个类并使用它:

table.class-name th:first-child {
    background: red;
}

答案 3 :(得分:0)

我知道最简单的方法是使用子选择器

#yourtableId > tbody > tr > th { background: red; }

Demo

答案 4 :(得分:0)

根据问题,每个人都尝试了很多东西来实现目标。

但是根据HTML,我们可以编写以下css并避免让孩子获得背景颜色..

在此解决方案中,我们不需要任何id和类或任何事物接受.gap class。即使这个类不存在,我们也可以应用css。

检查demo

HERE是.gap

的CSS
table th {background-color:red;}
table td.gap tr th {background:none;}

HERE是没有.gap

的CSS
table th {background-color:red;}
table td tr th {background:none;}
相关问题