在表单元格中打破长字与百分比宽度

Exp*_*lls 21 html css

http://jsfiddle.net/L2yLe/

小提琴说了这一切.我想要一个仅限css的解决方案,将表格的宽度限制为div的宽度.应该打破长字符串(但正如您所看到的那样),这会导致表溢出.

我不想使用任何像素宽度.这应该是完全流动的.

<div>
<table>
    <tr>
        <td>
            short string
        </td>
        <td>
somereallylongstringofdatasomereallylongstringofdatasomereallylongstringofdata
        </td>
        <td>
            short string
        </td>
    </tr>
</table>
</div>

div {
    width: 50%;
    background-color: darkgray;
    padding: 15px;
    margin: 10px auto;
}
table {
    border-collapse: collapse;
}
td {
    border: 1px solid black;
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*dov 27

伙计们看看http://blog.kenneth.io/blog/2012/03/04/word-wrapping-hypernation-using-css/

/* Warning: Needed for oldIE support, but words are broken up letter-by-letter */
   -ms-word-break: break-all;
   word-break: break-all;

   /* Non standard for webkit */
   word-break: break-word;

   -webkit-hyphens: auto;
   -moz-hyphens: auto;
   -ms-hyphens: auto;
   hyphens: auto;
Run Code Online (Sandbox Code Playgroud)

  • 我们无法在办公室网络中看到链接,请发布一些代码而不是链接.:( (2认同)

san*_*eep 11

写这个:

table {
border-collapse: collapse;
    table-layout: fixed;
    width:100%
}
td {
    border: 1px solid black;
    word-wrap:break-word;
}
Run Code Online (Sandbox Code Playgroud)

检查一下:http://jsfiddle.net/L2yLe/7/

  • 这非常有效,但唯一的问题是我希望某些列比其他列更宽.固定表格布局根本不允许您调整列的大小,即使像素宽度也是如此. (2认同)