HTML 固定宽度 Table,列不会调整大小

HTML Fixed Width Table, columns won't resize

我有一个固定宽度为 400px 的 div。 div里面有一个大于400px的table,所以我想让div滚动。那很好用。但我无法更改单元格的宽度。我需要单元格 2 的宽度为 200 像素。

<div style='width:400px;overflow:scroll'>
<table style='border:1px solid black'>
<th>field1</th>
<th style='width:200px'>field2</th>
<th>field3</th>
<th>field4</th>
<th>field5</th>
<th>field6</th>
<th>field7</th>
<th>field8</th>
<th>field9</th>
<th>field10</th>
<th>field11</th>
<th>field12</th>
<th>field13</th>
<th>field14</th>
<th>field15</th>
</table>
</div>

添加display: block。如果您希望 table 单元格具有宽度,请从 this solution 添加 table-layout: fixed; width: 100%;。不幸的是,这不适用于您的滚动功能。

<div style='width:400px;overflow:scroll'>
<table style='border:1px solid black'>
<th>field1</th>
<th style='width:200px; display: block;'>field2</th>
<th>field3</th>
<th>field4</th>
<th>field5</th>
<th>field6</th>
<th>field7</th>
<th>field8</th>
<th>field9</th>
<th>field10</th>
<th>field11</th>
<th>field12</th>
<th>field13</th>
<th>field14</th>
<th>field15</th>
</table>
</div>

nowrap 属性添加到您的 <th> 元素。关于nowrap属性的更多信息

<div style="width: 400px; overflow: scroll">
    <table style="border: 1px solid black">
        <th>field1</th>
        <th style="width: 200px" nowrap>field2</th>
        <th>field3</th>
        <th>field4</th>
        <th>field5</th>
        <th>field6</th>
        <th>field7</th>
        <th>field8</th>
        <th>field9</th>
        <th>field10</th>
        <th>field11</th>
        <th>field12</th>
        <th>field13</th>
        <th>field14</th>
        <th>field15</th>
    </table>
</div>