【发布时间】:2018-01-08 06:43:57
【问题描述】:
有没有办法使用简单的 html 表格隐藏空列?
我已经研究过这个问题,但不知道如何避免空列的每个单元格中的空间很小。
我知道有一些 jQuery 解决方案,我可以避免在后端显示空列,但我只需要 CSS 解决方案。
演示:
td{border: 1px solid red;}
input[type="image"]{width:100px;}
<h1>
Column with one not emty cell.
</h1>
<table style="width: 100%">
<tr>
<td width="100px">test words</td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td>123</td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
</tr>
<tr>
<td width="100px">test words</td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
</tr>
</table>
<h1>
Column with all emty cells.
</h1>
<table style="width: 100%">
<tr>
<td width="100px">test words</td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
</tr>
<tr>
<td width="100px">test words</td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
<td></td>
<td><input type="image" src="http://vectips.com/wp-content/uploads/2014/08/013.jpg"></td>
</tr>
</table>
有两种情况:当列完全为空时(您可以在空单元格中看到一个 1-2px 的孔)以及当列在某些单元格中包含内容时。
所以任务是: 当列没有空单元格时,表格必须按原样工作 当列完全为空时,该列必须完全隐藏,相邻列之间不得有任何空格。
【问题讨论】:
-
td:empty {display: none;}- 你试过了吗? -
您能否将您的 html 和您的 css 复制到 JSFiddle 中,以便每个人都可以“玩”现有的解决方案并为您找到正确的答案。谢谢
-
您想隐藏空单元格但仍保持表格结构?阐明您的预期最终结果是什么,并在可能的情况下提供示例。如果要隐藏单元格,将不可避免地影响表格结构。或者,您可以尝试
td:empty {visibility: hidden;},它应该不会影响文档流。 -
@UncaughtTypeError 请看一下帖子。