问题
I'm trying to wrap a long word. I have seen this post : How to prevent long words from breaking my div?
It works great in a simple case like this :
.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>
But my case has two nested tables like this :
<!-- 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>
You can test this code here http://jsfiddle.net/ZmnQ6/4/
回答1:
table is taking the table-layout: auto;
by default so as per contents increase the width also increases so you need to set table-layout to fixed.
table{
table-layout: fixed;
}
demo
回答2:
There are 2 completely different algorithms for table layout: with or without table-layout: fixed
Former will adapt width of cells to the author's will
Latter will adapt width of cells to the content (the relative quantity/width/whatever of content in cells)
回答3:
I think this is what you are looking for.
WORKING DEMO
The 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>
The Logic:
The div
and td
have different display
characteristics. You need to make your div
which is nested inside the td
to change its display
to, for instance here inline-block
with a fixed width
to achive what you are looking for.
Hope this helps.
来源:https://stackoverflow.com/questions/19537209/long-word-wrap-in-nested-tables