我有以下 CSS:
table tbody tr:last-child td {
padding-top: 7px;
border-bottom: 0;
}
table tbody tr:first-child td {
padding-top: 6px;
}
现在我可能有一张只有一行的表格。
唯一的表行现在分配给 first-child
而不是 last-child
,但我希望它是相反的。
有没有不用Javascript的方法?
最佳答案
这不可能。您的标记中一定有一些错误。如果它确实是唯一的 tr
,则 last
和 first
都会匹配。
<强> See example
但是,应用哪种 CSS 取决于 css 规则的顺序。因此,您可以通过相应地放置规则来确定是否应用 padding-top: 7px;
或 padding-top: 6px;
。
编辑:
由于您的问题是由插件引起的,该插件会在末尾自动插入一行,因此您可以简单地使用 :nth-last-child(2)
来匹配倒数第二个元素。
(但请注意,浏览器对 nth-last-child
的支持比 last-child
稍差)
关于html - 在唯一的表行上采用最后一个子级而不是第一个子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11029517/