【发布时间】:2019-10-05 16:39:53
【问题描述】:
我有一些这样的html代码:
<table>
<tr>
<td>first line </td>
<td>second line </td>
<td>third line </td>
</tr>
</table>
我只希望“第一行”具有红色。 * 我不能为标签使用内联样式或设置类。我只能使用外部 CSS 文件。
【问题讨论】:
标签: css css-selectors
我有一些这样的html代码:
<table>
<tr>
<td>first line </td>
<td>second line </td>
<td>third line </td>
</tr>
</table>
我只希望“第一行”具有红色。 * 我不能为标签使用内联样式或设置类。我只能使用外部 CSS 文件。
【问题讨论】:
标签: css css-selectors
您可以使用:first-child 选择器。来自文档:
:first-childCSS 伪类表示一组兄弟元素中的第一个元素。
演示:
table tr td:first-child {
color: red;
}
<table>
<tr>
<td>first line </td>
<td>second line </td>
<td>third line </td>
</tr>
</table>
【讨论】:
您可以使用first-of-type。
table td:first-of-type {
color: green;
}
<table>
<tr>
<td>first line </td>
<td>second line </td>
<td>third line </td>
</tr>
</table>
【讨论】: