欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

css中table-layout:fixed 属性的用法

程序员文章站 2022-06-01 15:26:10
...
table-layout:fixed 属性的用法:
如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字

不撑破表格的目的,一般是使用样式:table-layout:fixed。但是在Firefox下面,会有一些问题,参考Gmail的一些做法,做了几个

测试,得出一种解决办法。

例1:(IE浏览器)普通的情况

CODE:
<table border="1" width="80"><tr><td>abcdefghigklmnopqrstuvwxyz 1234567890</td></tr></table>

可以看到width=80并没有起作用,表格被字符撑开了。

例2:(IE浏览器)使用样式table-layout:fixed

CODE:
<style>
.tbl {table-layout:fixed;}
</style>
<table class="tbl" border="1" width="80"><tr><td>abcdefghigklmnopqrstuvwxyz 1234567890</td></tr></table>

width=80起作用了,但是表格换行了。

例3:(IE浏览器)使用样式table-layout:fixed与nowrap

CODE:
<style>
.tbl {table-layout:fixed;}
</style>
<table class="tbl" border="1" width="80"><tr><td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td></tr></table>

width=80起作用了,换行也被干掉了。


例4:(IE浏览器)在使用数值固定td大小情况下使用样式table-layout:fixed与nowrap

CODE:
<style>
.tbl {table-layout:fixed;}
</style>
<table class="tbl" border=1 width=80>
<tr>
<td width="20" nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
<td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
</tr>
</table>


不幸发生了,第一个td的nowrap不起作用了


例5:(IE浏览器)在使用百分比固定td大小情况下使用样式table-layout:fixed与nowrap

CODE:
<style>
.tbl {table-layout:fixed;}
</style>
<table class="tbl" border="1" width="80">
<tr>
<td width="25%" nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
<td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
</tr>
</table>

改成百分比,终于搞定了

例6:(Firefox浏览器)在使用百分比固定td大小情况下使用样式table-layout:fixed与nowrap效果:把例5放到firefox下面,又失效了

例7:(Firefox浏览器)在使用百分比固定td大小情况下使用样式table-layout:fixed与nowrap,并且使用p
CODE:
<style>
.tbl {table-layout:fixed;}
.td {overflow:hidden;}
</style>
<table class="tbl" border="1" width="80">
<tr>
<td width="25%" class="td" nowrap>
<p>abcdefghigklmnopqrstuvwxyz 1234567890</p></td>
<td class=td nowrap><p>abcdefghigklmnopqrstuvwxyz 1234567890</p>
</td>
</tr>
</table>

天下终于太平了

例8:(Firefox浏览器)在使用数值固定td大小情况下使用样式table-layout:fixed与nowrap,并且使用p

CODE:
<style>
.tbl {table-layout:fixed;}
.td {overflow:hidden;}
</style>

CODE:
<table class="tbl" border="1" width="80">
<tr>
<td width="20" class="td" nowrap><p>abcdefghigklmnopqrstuvwxyz 1234567890</p></td>
<td class=td nowrap><p>abcdefghigklmnopqrstuvwxyz 1234567890</p></td>
</tr>
</table>

nowrap又不起作用了
最终,例7是一个在IE和Firefox都可以完美解决页面强制换行问题的解决方案。

我也继续查了一些资料,既然首行的宽度才起作用,那我是否可以定义一下首行呢。

发现了colgroup属性。

<table width="100%" border="0" cell
padding
="3" cellspacing="1" bgcolor="#000000" style="table-layout:fixed">
<colgroup>
<col style="width:10%;"></col>
<col style="width:30%;"></col>
<col style="width:40%;"></col>
<col style="width:10%;"></col>
<col></col>
</colgroup>
<tr>
   <td colspan="5">&nbsp;</td>
 </tr> <tr>
   <td nowrap bgcolor="#FFFFFF">文字文字文字文字文字文字</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
 </tr>
 <tr>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td bgcolor="#FFFFFF">&nbsp;</td>
 </tr></table>

以上就是css中table-layout:fixed 属性的用法的详细内容,更多请关注其它相关文章!