Html 将样式应用于第一行的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10450221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:09:34  来源:igfitidea点击:

Apply style to cells of first row

htmlcsscss-selectorshtml-table

提问by xperator

It should be very simple but I can't figure it out.

它应该非常简单,但我无法弄清楚。

I have a table like this :

我有一张这样的桌子:

<table class="category_table">
 <tr><td> blabla 1</td><td> blabla 2 </td></tr>
 <tr><td> blabla 3 </td><td> blabla 4 </td></tr>
</table>

I want to make tdtags of first trrow have vertical-align. But not the second row.

我想让td第一tr行的标签有vertical-align. 但不是第二行。

.category_table td{
    vertical-align:top;
}

回答by BoltClock

Use tr:first-childto take the first tr:

使用tr:first-child采取先tr

.category_table tr:first-child td {
    vertical-align: top;
}

If you have nested tables, and you don't want to apply styles to the inner rows, add some child selectors so only the top-level tds in the first top-level trget the styles:

如果您有嵌套表,并且不想将样式应用于内部行,请添加一些子选择器,以便只有td第一个顶级中的顶级 str获得样式:

.category_table > tbody > tr:first-child > td {
    vertical-align: top;
}

回答by Simone

This should do the work:

这应该做的工作:

.category_table tr:first-child td {
    vertical-align: top;
}

回答by Vikas Kumar

Below works for first trof the table under thead

以下适用于tr下表中的第一个thead

table thead tr:first-child {
   background: #f2f2f2;
}

And this works for the first trof theadand tbodyboth:

这适用于第一trtheadtbody两个:

table thead tbody tr:first-child {
   background: #f2f2f2;
}