[英]CSS for table: 1 fixed width column, 1 small column, remaining columns equal
I'm trying to create a table whose first column is fixed width, whose second column contracts as small as it can to fit its text, and whose remaining columns split all remaining space equally, regardless of their contents. 我正在尝试创建一个表,该表的第一列是固定宽度的,其第二列要缩小以适合其文本,并且其剩余列将所有剩余空间均分,无论它们的内容如何。
Here's a fiddle to demonstrate, my attempt so far: http://jsfiddle.net/B8LnP/ 这是一个小提琴来演示,到目前为止我的尝试: http : //jsfiddle.net/B8LnP/
Two things to notice about the output: 关于输出注意两点:
.equal
class to both small and large values, but neither way accomplished the goal. .equal
类的宽度设置为较小和较大的值,但是都没有达到目标。 <table>
<tr>
<td class="fixed">fixed</td>
<td class="small">small_as_possible</td>
<td class="equal">split remaining</td>
<td class="equal">equally</td>
</tr>
</table>
<!-- another instance to make sure the columns align -->
<table>
<tr>
<td class="fixed">fixed</td>
<td class="small">small_as_possible</td>
<td class="equal">equally</td>
<td class="equal">split remaining</td>
</tr>
</table>
table { width: 95%; margin: 0 auto}
td {border: 1px solid}
.fixed {width: 200px}
.small {width: 1px}
.equal { /* what to put here? */ }
This will do the work for you: 这将为您完成工作:
CSS: CSS:
table { width: 95%; margin: 0 auto; table-layout:fixed;}
td {border: 1px solid}
.fixed {width: 200px;}
.small {width: 120px;}
.equal {width: 50%;}
Demo: http://jsfiddle.net/B8LnP/1/ 演示: http : //jsfiddle.net/B8LnP/1/
So What happens? 那么会发生什么呢?
Please note the table-layout:fixed;
请注意
table-layout:fixed;
. 。 With that and the
width
set on table
(percentage of the containing element like in your case or a fixed amount), what happens is that first the fixed width columns are deducted from the table's width, and then the rest of the remaining width of the table is split among the remaining columns which have a percentage width or no width set at all (same as auto). 以此为基础,并在
table
设置width
(在您的情况下,包含元素的百分比或固定的数量),首先会从表格的宽度中减去固定宽度的列,然后从表格的其余宽度中减去表格会在剩余的列中进行拆分,这些列的宽度或百分比完全没有设置(与auto相同)。
Also if all of the remaining columns (other than the ones with fixed width) have a percent width, but the total of the percentages doesn't add up to 100%, the remaining percents are automatically distributed among fixed width columns, so make sure the percentages add up to 100%. 另外,如果所有其余列(固定宽度的列除外)的宽度均是百分比,但百分比的总和不等于100%,则剩余的百分比会自动分配到固定宽度的列中,因此请确保百分比总计为100%。
您不能使用类来做到这一点,需要使用colspan属性来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.