我试图用一个长话来说。我看过这篇文章:How to prevent long words from breaking my div?
在这样的简单案例中它很有用:
.wrapWords
{
white-space: pre; /* CSS 2.0 */
white-space: pre-wrap; /* CSS 2.1 */
white-space: pre-line; /* CSS 3.0 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: -moz-pre-wrap; /* Mozilla */
white-space: -hp-pre-wrap; /* HP Printers */
word-wrap: break-word; /* IE 5+ */
}
<!-- This wraps correctly -->
<div style="width:145px;">
<div class="wrapWords" style="width:100%;">
<a href="#">AAAAAAAAAAAAAAAAAA</a>
</div>
<div>
但我的案例有两个嵌套表:
<!-- This doesn't work -->
<table style="width:100%;">
<tr>
<td style="width:145px;">
<table style="width:100%;">
<tr>
<td style="width:100%;">
<div class="wrapWords" style="width:100%;">
<a href="#">BBBBBBBBBBBBBBBBBB</a>
</div>
</td>
<td>
</td>
</tr>
</table>
</td>
<td>
</td>
</tr>
</table>
测试此代码
答案 0 :(得分:4)
答案 1 :(得分:0)
表格布局有两种完全不同的算法:有或没有table-layout: fixed
前者会根据作者的意愿调整细胞宽度 后者将使细胞的宽度适应内容(相对数量/宽度/细胞中的任何内容)
答案 2 :(得分:0)
我认为这就是你要找的东西。
<强> WORKING DEMO 强>
HTML:
<!-- This wraps correctly -->
<div style="width:145px;">
<div class="wrapWords" style="width:100%;">
<a href="#">AAAAAAAAAAAAAAAAAA</a>
</div>
<div>
<!-- This doesn't work -->
<table style="width:100%;">
<tr>
<td style="width:145px;">
<table style="width:100%;">
<tr>
<td style="width:130px;">
<div class="wrapWords" style="width:inherit; display:inline-block;">
<a href="#">BBBBBBBBBBBBBBBBBB</a>
</div>
</td>
<td>
</td>
</tr>
</table>
</td>
<td>
</td>
</tr>
</table>
逻辑:
div
和td
具有不同的display
特征。您需要将嵌套在div
内的td
更改为display
,例如此处为inline-block
,并使用固定的width
来实现您的{{1}}。正在寻找。
希望这有帮助。