2017-04-10 82 views
0

我有一个固定宽度为400px的div。 div内有一张大于400px的表格,所以我想让div滚动。这工作正常。但我不能改变单元格的宽度。我需要单元格2是200px宽。HTML固定宽度表,列不会调整大小

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

+0

它在你的细胞中防止其小大小的文本。 – Kobbe

回答

1

添加display: block。如果您希望表格单元具有宽度,请从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>

+1

这个完美的作品。谢谢 – CheeseFlavored

1

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

+0

这完全谢谢你。尽管我更喜欢使用display:block的答案,但这也很好。 – CheeseFlavored