table-layout:auto
是表格布局中的默認值,采用瀏覽器自動表格布局算法,但是缺點會很明顯
- 給
td
指定的width
不一定生效,td
的width
會自動調整 text-overflow: ellipsis
也會失效,同樣,img
會撐大td
舉個例子:
<table>
<tr>
<td>31</td>
<td>32ssssssssssssss</td>
</tr>
<tr>
<td>31</td>
<td>
<img src="assets/tiger.png" alt="">
</td>
</tr>
</table>
table {
display : table;
width : 200px;
height : 200px;
border-collapse : collapse;
table-layout : auto;
}
td {
overflow : hidden;
width : 100px;
height : 100px;
border : 1px solid black;
text-overflow : ellipsis;
}
這樣的結果就是這樣的:
td
的 width
和 img
都沒有溢出隱藏,反而撐大了td
,但是如果把 table-layout
改為 fixed
,效果如圖:
td
的text-overflow : ellipsis
和overflow : hidden
都會起作用,但是這里也有幾個地方要注意:
text-overflow : ellipsis
生效的前提是overflow
不為visible
,最好是類似hidden
的值- 需要指定
table
的width
- 如果
td
的寬度加起來超過table
的width
,即使給table
加上overflow:hidden
,td
也不會被隱藏。