简体   繁体   English

表格的CSS:1个固定宽度的列,1个小列,其余列相等

[英]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: 关于输出注意两点:

小提琴输出图像

  1. I want the columns of the stacked tables to align, as if they were part of a single table 我希望堆叠表的列对齐,就像它们是单个表的一部分一样
  2. Notice the problem is that I can't get columns 3 and 4 to split up the remaining space. 注意,问题在于我无法获得第3列和第4列来拆分剩余空间。 I've tried setting the width for the .equal class to both small and large values, but neither way accomplished the goal. 我尝试将.equal类的宽度设置为较小和较大的值,但是都没有达到目标。

Fiddle HTML 小提琴HTML

<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>

CSS CSS

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.

相关问题 具有固定大小的第一列且其余列相等大小的HTML表格 - Html table with first column of fixed size and remaining columns equal size 带有3列的html表,最后一列的宽度为灵活宽度。 前两列以25/75%的固定比率共享剩余空间 - html table with 3 column, last column width flexible width. The first two columns share the remaining space with a fixed ratio of 25/75% HTML表,第一列和最后一列固定宽度和动态之间的列,但宽度相等 - HTML Table, first and last column fixed width and columns between dynamic, but equal width CSS表:第一栏为滚动条,其余栏为自动宽度 - CSS Table: Scrollbar for first column and auto width for remaining column CSS三列,中间列固定宽度 - CSS three columns with middle column fixed width 将表格列的宽度与固定宽度表格(HTML / CSS)中的文本匹配 - Matching the width of a table column to text in a fixed width table (HTML/CSS) 无法在固定列表中设置列的CSS宽度 - Can't set CSS width of column in fixed column table 固定可滚动div中的标头表,其“表头列宽度”和“内容列宽度”相等 - Fixed header table inside a scrollable div with equal 'table head column width' and 'content column width' CSS 3列-前两个固定宽度,第三个剩余宽度 - CSS 3 columns - First two fixed width, third remaining width 使用表格布局更改表格的列宽:使用CSS固定 - Change column width of table with table-layout: fixed with CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM