[英]HTML Table with no fixed width and all columns the width of largest column (without JS)
是否有可能有一个表格,其中以下所有内容都是正确的:
white-space: nowrap
的按钮)。 似乎table-layout: fixed
处理相等的列,但需要表格上的宽度,并且只考虑第一行单元格的宽度。 切换到 `table-layout: auto 会导致列宽不均匀(并且为列设置百分比宽度没有任何效果)。
table { border-collapse: collapse; } th, td { outline: 1px solid black; padding: 5px; }.tableWrapper { max-width: 600px; }.tableFixed { table-layout: fixed; width: 100%; }.tableFixedWithPercentageWidths { th, td { min-width: 33.33%; } } button { white-space: nowrap; }
<div class="tableWrapper"> <h4>Auto</h4> <table> <tr> <th>Alpha</th> <th>Bravo</th> <th>Charlie</th> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td><button>This is a really long button label</button></td> </tr> </table> <h4>Fixed</h4> <table class="tableFixed"> <tr> <th>Alpha</th> <th>Bravo</th> <th>Charlie</th> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td><button>This is a really long button label</button></td> </tr> </table> <h4>Auto with percentage cell widths</h4> <table class="tableFixedWithPercentageWidths"> <tr> <th>Alpha</th> <th>Bravo</th> <th>Charlie</th> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td><button>This is a really long button label</button></td> </tr> </table> <h4>Fixed with percentage cell widths</h4> <table class="tableFixed tableFixedWithPercentageWidths"> <tr> <th>Alpha</th> <th>Bravo</th> <th>Charlie</th> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td>Charlie</td> </tr> <tr> <td>Alpha</td> <td>Bravo</td> <td><button>This is a really long button label</button></td> </tr> </table> </div>
是否可以单独使用 CSS 来实现这一目标?
如果您不想使用任何专有的<table>
功能,可以使用 ZC7A628CBA22E28EB17B5F5C6AE2A266AZ grid
。 (即,如果您只关心外观)。
这是你想要的吗?
HTML:
<html>
<body>
<div class="tableWrapper">
<h4>Mine</h4>
<table>
<tr>
<th>Alpha</th>
<th>Bravo</th>
<th>Charlie</th>
</tr>
<tr>
<td>Alpha</td>
<td>Bravo</td>
<td>Charlie</td>
</tr>
<tr>
<td>Alpha</td>
<td>Bravo</td>
<td>Charlie</td>
</tr>
<tr>
<td>Alpha</td>
<td>Bravo</td>
<td><button>This is a really long button label yessssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss</button></td>
</tr>
</table>
</table>
</div>
</body>
</html>
CSS:
table {
/* border-collapse: collapse ; */
overflow-x: scroll;
min-width:max-content;
}
th, td {
outline: 1px solid black;
padding: 5px;
width: 33%;
}
button {
white-space: nowrap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.