html - 已经使用 :nth-child 时,将 CSS 样式应用于类型的第一个元素
问题描述
在下面的示例中,我不想对单元格应用任何背景颜色Not here...
。
换句话说,我只想将我的 CSS 应用于.class tag:modifer
type 的第一个子元素tag
。
.foo {
border-collapse: collapse;
}
.foo tr:nth-child(1) td:nth-child(1) {
background-color: green;
}
.foo tr:nth-child(2) td:nth-child(2) {
background-color: red;
}
<table class="foo">
<tr><td>A</td><td>B</td></tr>
<tr><td>C</td>
<td>
<table><tr><td>Not here...</td></tr></table>
</td>
</tr>
</div>
解决方案
- 使用直接后代组合器
>
- 找到树上更高的祖先
<div>
- 采用
nth-of-type
div > table > tbody > tr:first-of-type > td:first-of-type
.foo { border-collapse: collapse; } div>table>tbody>tr:first-of-type>td:first-of-type { background-color: green; } .foo tr:nth-child(2) td:nth-child(2) { background-color: red; }
<div> <table class="foo"> <tr> <td>A</td> <td>B</td> </tr> <tr> <td>C</td> <td> <table> <tr> <td>Not here...</td> </tr> </table> </td> </tr> </table> </div>
推荐阅读
- c# - 关闭 C# 控制台应用程序后继续运行 SQL 存储过程
- tensorflow - 我怎样才能让我的模型接受字符串输入
- javascript - 手风琴开关打开 + 减号
- python - 如何解决导入 pygame 期间的错误
- c# - 如何动态添加或删除文本框?
- python - 如何在 Qt 中创建一个基本的自定义 QGraphicsEffect?
- node.js - 值之间的猫鼬查询 Express Js
- javascript - 使用 ES6Modules 导出 * as *
- javascript - 在 Angular 8 中监听 DOM 事件的最佳实践是什么?
- java - Mock private wrapper/primitive/String field