首页 > 解决方案 > max-width 对表格没有任何作用

问题描述

我正在尝试一件非常简单的事情(我想),但它不起作用。如果我设置这样的表格样式:

    table {
     width: 537px;
     height: 19px;
     max-width:  100%; 
    }
<table>
  <tr>
    <td style="background-color: #ffe129;">text</td>
    <td style="background-color: #bdff4d;">text</td>
  </tr>
</table>

然后表格宽度将537px即使我的窗口宽度为 400 像素。一个 div 将是400px.

添加display:block到表格样式会破坏表格:请参见此处

标签: csshtml-table

解决方案


我将定义max-width: 537px;例如,然后width: 100%通过这种方式给出如果你说的max-width537pxwidth:100% ; 它将始终尊重屏幕的宽度,并且不会超过537px

table
{
 width: 100%;
 height: 19px;
 max-width:  537px; 
}
<table>
<tr>
<td style="background-color: #ffe129;"></td>
<td style="background-color: #bdff4d;"></td>
</tr>
</table>


推荐阅读