我有一个HTML表格,其中第一列是产品名称,其他列是销售数据(根据产品的不同,介于2到5个季度之间)
我想将表格整体宽度设置为页面宽度的95%,并使第一列的宽度足够显示产品名称而不换行(尽管我还希望其最大宽度为30%)。 接下来的列我希望大小相等,并填充表格的可用宽度。
我尝试使用布局fixed和auto来设置表格,并使用table > thead > tr > th:first-child {
覆盖第一列的样式,但没有找到正确的组合来解决这个问题。
必须使用表格而不是“纯”CSS,因为它会被模板引擎消耗,该模板引擎基于<thead>
和<tbody>
元素进行渲染逻辑,并且因为它是一个模板,如果可能的话,我希望不要依赖注入固定值(例如基于列数的计算,就像这个答案),但如果需要,我可以这样做。
<html>
<head>
<style>
table {
table-layout: fixed;
width: 95%;
border: 1px solid;
}
table>tbody>tr>th {
white-space: nowrap;
}
</style>
</head>
<body>
<table>
<thead>
<tr>
<th>Product</th>
<th>Q1</th>
<th>Q2</th>
<th>Q3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Example Product One</td>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>Example product two</td>
<td>10</td>
<td>20</td>
<td>30</td>
</tr>
<tr>
<td>Example Three</td>
<td>1000</td>
<td>2000</td>
<td>3000</td>
</tr>
</tbody>
</table>
</body>
</html>