html - CSS。仅当 TBODY 没有行时显示 TFOOT

标签 html css css-selectors html-table

我有一个 HTML 表格,带有 <thead> + <tbody> + <tfoot> .我需要显示 <tfoot>仅当 <tbody>没有行。我知道如何使用 JS/jQuery 来实现,但也许有纯 CSS 解决方案?

最佳答案

使用 :empty

Demo (当tfoot没有行时显示tbody)

table tbody:empty + tfoot {
    display: table-footer-group;
}

table tbody + tfoot {
    display: none;
    color: red;
}

隐藏tfoot什么时候tbody有一些内容

table tbody:not(:empty) + tfoot {
    display: block;
}

table tbody + tfoot {
    display: none;
    color: red;
}

Demo 2


解释:

修改太多,我只想提供2个选择器,第一个是table tbody:empty + tfoot这将选择 tfoot如果tbody IS EMPTY,第二个是table tbody:not(:empty) + tfoot这将选择 tfoot如果tbody 不是空的。

Note: I am using + which is an adjacent selector, so as you see, I have tfoot element, after the tbody element, if it is before tbody than you need to use JS or jQuery as you select reverse in CSS. Also, make sure you use display: table-footer-group; as pointed by Mr Lister for tfoot element and not display: block;

关于html - CSS。仅当 TBODY 没有行时显示 TFOOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420117/

相关文章:

jquery.toggle() : is it possible to hide div with text?

html - 由于边框折叠属性,表格的边框半径不起作用

相当于第一个 :child to target first <p> in div with heading and img preceding? 的 css

java - 如何使用 selenium 和 java 在模态窗口中单击“确定”按钮?

html - CSS 将标题与内部菜单对齐

html - Sightly:迭代一个列表并使用其索引打印另一个列表(不嵌套,只是共享索引)

javascript - jQuery 找到第一个有 child 的 parent

html - 仅限 CSS - 带有滚动条的动态标题高度和 100% 内容高度

javascript - 如何仅使用 jquery 制作动态进度条?

css - nth-child 和 nth-last-child 不一起工作