table不能换行问题解决,CSS之自动换行总结

table不能换行问题解决,CSS之自动换行总结

http://hi.baidu.com/jaywclove/blog/item/ca3cb354f7838e1a3b293542.html

 

table不能换行问题 一般是:
一行里面数字或是字母 还或者结尾有多个感叹号导致 table不能换行

中文默认的会自动换行的

字母不能换行问题
style="table-layout:fixed;word-break:break-all;"

在单元格属性里加入上面这句,如:
<td style="table-layout:fixed;word-break:break-all;">

用表格做网页排版的时候,一般都能正常使用。偏偏有时会碰到一段连续的英文词或者一堆感叹号(!!!)把网页就撑开的现象:(

总结了一下,只要在CSS中定义了如下句子,可保网页不会再被撑开了。

自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼,下面介绍的是CSS如何实现换行的方法

对于div,p等块级元素

正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义的宽度之后自动换行

html

<div id="wrap">正常文字的换行(亚洲文字和非亚洲文字)元素拥有默认的white-space:normal,当定义</div>

css

#wrap{white-space:normal; width:200px; }


IE浏览器

连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行

html

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>


css

#wrap{word-break:break-all; width:200px;}

或者

#wrap{word-wrap:break-word; width:200px;}


Firefox浏览器
连续的英文字符和阿拉伯数字的断行,Firefox的所有版本的没有解决这个问题,我们只有让超出边界的字符隐藏或者,给容器添加滚动条

html

<div id="wrap">abcdefghijklmnabcdefghijklmnabcdefghijklmn111111111</div>


css

#wrap{word-break:break-all; width:200px; overflow:auto;}


对于table元素
IE浏览器

1. 使用 table-layout:fixed;强制table的宽度,多余内容隐藏

<table style="table-layout:fixed" width="200">
<tr>
<td>abcdefghigklmnopqrstuvwxyz1234567890ssssssssssssss
</td>
</tr>
</table>


2. 使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break : break-all;或者word-wrap : break-word ;换行

<table width="200" style="table-layout:fixed;">
<tr>
<td width="25%" style="word-break : break-all; ">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
<td style="word-wrap : break-word ;">abcdefghigklmnopqrstuvwxyz 1234567890
</td>
</tr>
</table>


3.在td,th中嵌套div,p等采用上面提到的div,p的换行方法

Firefox浏览器

1. 使用 table-layout:fixed;强制table的宽度,内层td,th采用word-break : break-all;或者word-wrap : break-word ;换行,使用overflow:hidden;隐藏超出内,这里overflow:auto;无法起作用

<table style="table-layout:fixed" width="200">
<tr>
<td width="25%"  style="word-break : break-all; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
<td width="75%" style="word-wrap : break-word; overflow:hidden; ">abcdefghigklmnopqrstuvwxyz1234567890</td>
</tr>
</table>


2.在td,th中嵌套div,p等采用上面提到的对付Firefox的方法

最后,这种现象出现的几率很小,但是不能排除网友的恶搞。

table{table-layout: fixed;}
td(word-break: break-all; word-wrap:break-word;)


注释一下:

1.第一条table{table-layout: fixed;},此样式可以让表格中有!!!(感叹号)之类的字符时自动换行。

2.td{word-break: break-all},一般用这句这OK了,但在有些特殊情况下还是会撑开,因此需要再加上后面一句(word-wrap:break-word;)就可以解决。此样式可以让表格中的一些连续的英文单词自动换行。

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现u-table中的自动换行且可以根据内容计算高度,你可以使用CSS的`word-wrap`和`height`属性来实现。以下是一个示例代码,你可以参考: ```vue <template> <div> <u-table :data="tableData" class="custom-table"> <u-table-column label="Name"> <template slot-scope="props"> <div class="cell" :style="{ height: getCellHeight(props.row.name) }">{{ props.row.name }}</div> </template> </u-table-column> <u-table-column label="Age"> <template slot-scope="props"> <div class="cell" :style="{ height: getCellHeight(props.row.age) }">{{ props.row.age }}</div> </template> </u-table-column> <u-table-column label="Email"> <template slot-scope="props"> <div class="cell" :style="{ height: getCellHeight(props.row.email) }">{{ props.row.email }}</div> </template> </u-table-column> </u-table> </div> </template> <script> export default { data() { return { tableData: [ { name: 'John', age: 32, email: 'john@example.com' }, { name: 'Jane', age: 28, email: 'jane@example.com' }, { name: 'Bob', age: 45, email: 'bob@example.com' } ] }; }, methods: { getCellHeight(content) { // 根据内容计算高度,可以根据实际需求进行调整 const lines = content.split('\n').length; // 假设换行符为\n const lineHeight = 20; // 假设每行的高度为20px const minHeight = 40; // 最小高度为40px const height = Math.max(lines * lineHeight, minHeight); return height + 'px'; } } }; </script> <style scoped> .custom-table .cell { word-wrap: break-word; white-space: pre-wrap; overflow: hidden; } </style> ``` 在这个示例中,我使用了自定义的CSS类名 `.custom-table` 来给u-table添加样式,并使用 `.cell` 类名给单元格添加样式。在 `.cell` 样式中,我设置了 `word-wrap: break-word` 来实现自动换行,以及 `overflow: hidden` 来隐藏溢出的内容。我还在每个单元格的 `div` 元素上使用了动态绑定的 `:style` 属性,将计算得到的高度应用到单元格上。 在 `getCellHeight` 方法中,我根据内容计算了单元格的高度。你可以根据实际需求进行相应的调整,例如根据字符数计算高度或者设置最大高度等。 希望这个示例能满足你的需求!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值