我已经构建了一个使用CSS使用visibility:collapse隐藏中间行的HTML表。
默认情况下,只有表的第一行和最后一行是可见的。
在此表中,右边有一列是使用行跨度设置的。此列可以包含多行文本。
我的问题是,如果该列的高度大于默认显示的表行的合并高度(第一个和最后一个),则该列中的整个内容似乎被截断。
.hide {
visibility: collapse
}
body {
padding: 2rem;
}<table border="1">
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
</table>
我应该在CSS中更改什么,才能显示行跨度单元格的所有“文本”行,而不是被截断?不能使用JavaScript。
发布于 2022-05-23 19:44:11
我认为最好使用display:none;或visibility:hidden;。或者在TD上使用overflow:auto;,但是这样就会得到一个滚动条小提琴。
关于visibility:collapse;,请查看css-技巧上的这篇文章
.hide{
/* Using this, will mess up everything */
/* visibility:collapse; */
/* Using this, will give you something where the middle row is hidden */
/* visibility:hidden; */
/* display none works the best */
display: none;
}<table border="1">
<tbody>
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
</tbody>
</table>
发布于 2022-05-27 03:18:47
深入研究这个问题,很明显,问题描述和示例中构建<td rowspan="3">的方式使每个人都偏离了轨道。包括我..。
在经历了令人沮丧的几个小时之后,答案其实很简单: OP试图将6行文本强制放入3行空间,因为使用了5 <br>__s。内容空间不够,所以单元格就会溢出。行跨度不应读3,而应读6!
一旦发现了空间问题,一种解决方案是创建更多的表行来容纳6行,并安排使用滚动条,当表中可用的行少于6行时,同时裁剪溢出的单元格。
在页面表:表元素:在小空间中显示大表中,MDN将表定义为table { display: block; overflow: auto },并引入tbody { white-space: nowrap }以支持表内容的滚动。
我创建了一些注释代码,显示了原始的和解决方案1,并为类.hide和其他行提供了简单的切换。
我喜欢吗?不,远非如此,但表机制似乎就是这样工作的。我不认为这是个窃听器,只是我们需要解决的问题.
更新
在回答@MaxiGui下面的公平点时,我没有遵守的操作,“我应该在CSS中更改什么,以获得行跨度单元格的所有行”文本“而不是被截断的”问题“,我添加了另一个解决方案。
添加的解决方案2显示了OP原始代码,但只需在.hide /table-row之间切换类,而不是在visibility: collapse/visible之间切换。
因为如果必须将元素的所有大小属性设置为0 (零,空值)才能模拟display: none,那么使用display属性而不是visibility来隐藏表行要简单得多。
但是,“任择议定书”没有明确提到保留visibility的要求.
/* original code */
body { padding: 2rem }
.hide { visibility: collapse }
/* solution 1 */
.solution {
display: inline-block;
overflow: auto; /* to show scrollbars when required */
}
/*
MDN excerpt
(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table#displaying_large_tables_in_small_spaces)
When looking at these styles you'll notice that table's display property
has been set to block. While this allows scrolling, the table loses some
of its integrity, and table cells try to become as small as possible.
To mitigate this issue white-space has been set to nowrap on the <tbody>.
*/
.solution tbody { white-space: nowrap }
.solution td {
overflow: hidden; /* clips excess content */
vertical-align: top; /* moves content to top of cell */
}
#tgl-line ~ .solution {
min-width: calc(6.75rem + 17px); /* create some room for content and scrollbar */
/* just for demo, change to max-width when .solution { display: block } */
}
#tgl-line:checked ~ table .hide { visibility: visible } /* both tables */
#tgl-line:checked ~ .solution { min-width: 6.75rem } /* solution only */
#tgl-rows ~ .solution .rows { display: none } /* additional rows hidden */
#tgl-rows:checked ~ .solution .rows { display: table-row } /* in view... */
/* solution 2 */
.hide-d { display: none } /* alt classname for solution 2 */
#tgl-line:checked ~ table .hide-d { display: table-row }
/* eye-candy */
h2 { margin-top: 4rem }<input id ="tgl-line" type="checkbox">
<label for="tgl-line">toggle .hide (all examples)</label>
<h2>original: toggle 'visibility' (rowspan=3)</h2>
<table border="1">
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
</table>
<h2>solution 1: toggle 'visibility' (rowspan=6)</h2>
<input id ="tgl-rows" type="checkbox">
<label for="tgl-rows">toggle rows</label>
<br><br>
<table class="solution" border="1">
<tbody>
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="6" class="special">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
<tr class="rows">
<td>D1</td>
<td>D2</td>
</tr>
<tr class="rows">
<td>E1</td>
<td>E2</td>
</tr>
<tr class="rows">
<td>F1</td>
<td>F2</td>
</tr>
</tbody>
</table>
<h2>solution 2: toggle 'display' (rowspan=3)</h2>
<table border="1">
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide-d">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
</table>
。
发布于 2022-05-24 07:56:16
.hide {
position: absolute;
left: -999em;
}
body {
padding: 2rem;
}<table border="1">
<tr>
<td>A1</td>
<td>A2</td>
<td rowspan="3">Text 1<br>Text 2<br>Text 3<br>Text 4<br>Text 5<br>Text 6</td>
</tr>
<tr class="hide">
<td>B1</td>
<td>B2</td>
</tr>
<tr>
<td>C1</td>
<td>C2</td>
</tr>
</table>
display: none;元素从正常流中移除并隐藏;它所占用的空间被折叠。内容被屏幕阅读器忽略。因此,如果您想隐藏它,但使它对屏幕阅读器可见,您可以使用上面的代码在我看来。
https://stackoverflow.com/questions/64542457
复制相似问题