简体   繁体   English

具有固定大小的第一列且其余列相等大小的HTML表格

[英]Html table with first column of fixed size and remaining columns equal size

The table I have has one header column and remaining data columns. 我的表有一个标题列和其余数据列。 I wish to have the following: 我希望有以下几点:

  • the table takes 100% of the screen width. 该表占用屏幕宽度的100%。
  • the first column has a fixed width of 150px. 第一列的固定宽度为150px。
  • Remaining columns to have widths divided equally. 其余列的宽度均分。 I had posted my code here 我已经在这里发布了我的代码
     #my-table{width:100%;border-collapse:collapse;} /*or whatever width you want*/ #my-table td{width:2000px;padding:4px;border:1px solid #000;vertical-align:top;} /*something big or 100%*/ #my-table td:first-child {width:150px;padding:4px;border:1px solid #000;vertical-align:top;} 
     <table id="my-table"><tr> <td> CELL 1 Whith a lot of text in it</td> <td> CELL 2 </td> <td> CELL 3 </td> <td> CELL 4 Whith a lot of text in it </td> <td> CELL 5 </td> </tr></table> 

Any assistance is appreciated. 任何帮助表示赞赏。

All you need is to assign the following per td tag. 您需要为每个td标签分配以下内容。

This way you can specify which column gets more space. 这样,您可以指定哪一列获得更多空间。

<td style="width:60%">... </td>
<td style="width:150px">... </td>

The browser will take care of the rest. 浏览器将负责其余的工作。

To force your width you can use 要强制宽度,您可以使用

<td style="width: calc((100% - 150px)/5)">... </td> <!-- width is on fifth of remaining area -->

Please note that there are other solutions like css grid. 请注意,还有其他解决方案,例如CSS网格。

You should use th for the header cells: 您应该将th用作标题单元格:

<table>
  <tr>
    <th>Number</th>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

For the css: 对于CSS:

table {
    width:100%;
}
th {
    width:150px;
} 
td {
   width: auto;
}

You can also omit the css for td, because 'width' is 'auto' by default. 您也可以省略ts的css,因为默认情况下'width'为'auto'。

You can add 'table-layout:fixed;' 您可以添加“ table-layout:fixed;” to #my-table and set the width of the first column with style attribute or through a css property. 到#my-table,并使用style属性或通过CSS属性设置第一列的宽度。

<table id="my-table">
    <tr>
        <td style="width: 150px;"> CELL 1 Whith a lot of text in it</td>
        <td> CELL 2 </td>
        <td> CELL 3 </td>
        <td> CELL 4 Whith a lot of text in it </td>
        <td> CELL 5 </td>
   </tr>
</table>

#my-table {
  width:100%;
  border-collapse:collapse;
  table-layout: fixed;
}

#my-table td {
  padding:4px;
  border:1px solid #000;
  vertical-align:top;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM