[英]How to style the entire first row of a table in CSS
I wanted to know if there's a way to style (background, text, hover effect) the entire first row of a table? 我想知道是否可以样式化表格的整个第一行(背景,文本,悬停效果)? I have tried a lot but nothing works.
我已经尝试了很多,但没有任何效果。 I tried this one too:
我也尝试过这个:
.category_table td{
background: #000;
color: #fff;
font-weight: bold;}
but nothing works. 但没有任何效果。 Can somebody please help me out on this?
有人可以帮我这个忙吗?
You said it yourself, the first ROW, so that is what you need to target. 您自己说的,第一个ROW,所以这是您需要定位的目标。
.category_table tbody tr:first-child td { /* styles for first row's cells */ }
(I've assumed your table has a tbody
; if not, remove this part of the selector.) (我假设您的表有一个
tbody
;如果没有,请删除选择器的这一部分。)
You'll probably have to do this: 您可能必须这样做:
HTML: HTML:
<table><tr class="row1">
<td>Something</td>
<td>Somthing</td>
</tr><tr>
<td>Something</td>
<td>Somthing</td>
</tr></table>
CSS: CSS:
.row { /*Style the 1st row*/
}
.row td { /*style each <td> in the 1st row*/
}
If you want best browser compatibility add a class to the row. 如果要获得最佳的浏览器兼容性,请在该行中添加一个类。 If you only care about newer browsers then you can use the tr:first-child selector
如果您只关心较新的浏览器,则可以使用tr:first-child选择器
In the above link, you'll notice styles being applied from both in modern browsers. 在上面的链接中,您会注意到两种样式都在现代浏览器中应用。 If you view in an old browser (eg IE6) then only the colouring styles will take effect the font-size selector wont as its an unsupported selector.
如果您在旧的浏览器(例如IE6)中查看,则只有颜色样式才会生效,字体大小选择器不会作为其不受支持的选择器。 If you only want to target new browsers then the first-child doesn't require any changes to your HTML which may or may not be an issue
如果您只想定位新的浏览器,则第一个孩子不需要对HTML进行任何更改,这可能会或可能不会造成问题
In the general case, you need to select the first child of the first thead
or tbody
child of the table: 通常,您需要选择表中第一个
thead
或tbody
子级的第一个子级:
thead:first-of-type > :first-child,
tbody:first-of-type > :first-child {
font-weight: bold; /* or whatever you want to set */
}
The simpler selector tr:first-child
works in simple cases where the table
element contains only one tbody
element as a child (which is the case when you write just tr
elements inside it – a single tbody
is then implied). 更简单的选择器
tr:first-child
在以下情况下适用tr:first-child
table
元素仅包含一个tbody
元素作为子元素(在您仅在其中写入tr
元素的情况下–则意味着一个tbody
)。 But in general, a table may contain a thead
element, a tfoot
element, and several tbody
elements. 但是通常,一个表可以包含
thead
元素, tfoot
元素和几个tbody
元素。
Note: If there is no thead
element and there is a tfoot
element in markup before the tbody
element(s), it is debatable whether the first tr
in the tfoot
element is the first row. 注意:如果没有
thead
元素,有一个tfoot
在之前标记元素tbody
元素(一个或多个),这是值得商榷的第一是否tr
在tfoot
元素是第一行。 In markup it is, but it is rendered last (in default rendering). 在标记中是,但是最后渲染(默认渲染)。 I have interpreted that
tfoot
elements are to be ignored in this issue. 我已经解释过,在这个问题上,
tfoot
元素将被忽略。
Support to the CSS selectors used here is good but not universal. 对此处使用的CSS选择器的支持很好,但并不通用。 For support in all CSS-enabled browsers, add a
class
attribute to the first tr
element, eg <tr class=first>
, and use a class selector, eg .first
. 为了支持所有启用CSS的浏览器,请在第一个
tr
元素中添加一个class
属性,例如<tr class=first>
,并使用一个类选择器,例如.first
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.