Html 页面的强制换行问题总结

(编辑:jimmy 日期: 2025/6/12 浏览:2)

一般是使用样式:table-layout:fixed。不过在Firefox下面,会有一些问题,参考Gmail的一些做法,做了几个测试,得出 一种解决办法。

  如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字不 撑破表格的目的,一般是使用样式:table-layout:fixed。不过在Firefox下面,会有一些问题,参考Gmail的一些做法,做了几个 测试,得出一种解决办法。

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

<table border=1 width=80>

<tr>

<td>abcdefghigklmnopqrstuvwxyz 1234567890

</td>

</tr>

</table>
效果:Html 页面的强制换行问题总结
能看到width=80并没有起作用,表格被字符撑开了。

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

<style>

.tbl {table-layout:fixed}

</style>



<table class=tbl border=1 width=80>

<tr>

<td>abcdefghigklmnopqrstuvwxyz 1234567890

</td>

</tr>

</table>
效果:Html 页面的强制换行问题总结
width=80起作用了,不过表格换行了。

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

<style>

.tbl {table-layout:fixed}

</style>



<table class=tbl border=1 width=80>

<tr>

<td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890

</td>

</tr>

</table>
效果:Html 页面的强制换行问题总结
width=80起作用了,换行也被干掉了。

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

<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>
效果:Html 页面的强制换行问题总结
不幸发生了,第一个td的nowrap不起作用了

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

<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>
效果:Html 页面的强制换行问题总结
改成百分比,终于搞定了

例6:(Firefox浏览器)在使用百分比固定td大小情况下使用样式table-layout:fixed和nowrap效果:Html 页面的强制换行问题总结
把例5放到firefox下面,又ft了

例7:(Firefox浏览器)在使用百分比固定td大小情况下使用样式table-layout:fixed和nowrap,并且使用div

<style>

.tbl {table-layout:fixed}

.td {overflow:hidden;}

</style>



<table class=tbl border=1 width=80>

<tr>

<td width=25% class=td nowrap>

<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>

</td>

<td class=td nowrap>

<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>

</td>

</tr>

</table>
效果:Html 页面的强制换行问题总结
天下终于太平了

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

<style>

.tbl {table-layout:fixed}

.td {overflow:hidden;}

</style>



<table class=tbl border=1 width=80>

<tr>

<td width=20 class=td nowrap>

<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>

</td>

<td class=td nowrap>

<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>

</td>

</tr>

</table>
效果:Html 页面的强制换行问题总结
nowrap又不起作用了

最终,例7是个在IE和Firefox都能完美解决页面强制换行问题的解决方案。

一句话新闻

高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。