嵌套表中的长字包装

时间:2013-10-23 08:56:15

标签: css html-table

我试图用一个长话来说。我看过这篇文章: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>

您可以在http://jsfiddle.net/ZmnQ6/4/

测试此代码

3 个答案:

答案 0 :(得分:4)

表默认采用table-layout: auto;,因此每个内容增加,宽度也会增加,因此您需要将表格布局设置为固定。

table{
    table-layout: fixed;
}

demo

答案 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>

逻辑:

divtd具有不同的display特征。您需要将嵌套在div内的td更改为display,例如此处为inline-block,并使用固定的width来实现您的{{1}}。正在寻找。

希望这有帮助。