100%宽度的表格溢出div容器


136

我的html表有问题,它的父容器溢出了。

.page {
    width: 280px;
    border:solid 1px blue;
}

.my-table {
    word-wrap: break-word; 
}

.my-table td {
    border:solid 1px #333;
}
<div class="page">
    <table class="my-table">
        <tr>
            <th>Header Text</th>
            <th>Col 1</th>
            <th>Col 2</th>
            <th>Col 3</th>
            <th>Col 4</th>
            <th>Header Text</th>
        </tr>
        <tr>
            <td>An archipelago is a chain or cluster of islands. The word archipelago is derived from the Greek ἄρχι- – arkhi and πέλαγος – pélagosthrough the Italian arcipelago. In Italian, possibly following a tradition of antiquity, the Arcipelago (from medieval Greek *ἀρχιπέλαγος) was the proper name for the Aegean Sea and, later, usage shifted to refer to the Aegean Islands (since the sea is remarkable for its large number of islands). It is now used to refer to any island group or, sometimes, to a sea containing a large number of scattered islands such as the Aegean Sea.[1]</td>
            <td>some data</td>
            <td>some data</td>
            <td>some data</td>
            <td>some data</td>
            <td>An archipelago is a chain or cluster of islands. The word archipelago is derived from the Greek ἄρχι- – arkhi and πέλαγος – pélagosthrough the Italian arcipelago. In Italian, possibly following a tradition of antiquity, the Arcipelago (from medieval Greek *ἀρχιπέλαγος) was the proper name for the Aegean Sea and, later, usage shifted to refer to the Aegean Islands (since the sea is remarkable for its large number of islands). It is now used to refer to any island group or, sometimes, to a sea containing a large number of scattered islands such as the Aegean Sea.[1]</td>
        </tr>
    </table>    
</div>

如何强制将标题文本和表格单元格文本都包装为适合其容器的方式?我更喜欢仅使用CSS的方法,但是如果绝对必要的话,也可以使用Javascript(或jQuery)。

Answers:


252

从纯粹的“使其适合div”的角度来看,将以下内容添加到表类(jsfiddle):

table-layout: fixed;
width: 100%;

根据需要设置列宽;否则,固定布局算法将在您的各列之间均匀分配表格宽度。

为了快速参考,以下是表格布局算法,重点是我的:

  • 固定(来源
    使用这种(快速)算法,表格的水平布局不取决于单元格的内容;它仅取决于表格的宽度,列的宽度以及边框或单元格的间距。
  • 自动(来源
    在这种算法中(通常不超过两遍),表格的宽度由其列的宽度[,由内容决定](以及中间的边框)给出。

    该算法可能效率不高,因为它要求用户代理在确定最终布局之前必须访问表中的所有内容,并且可能需要多次通过。

单击以查看源文档以查看每种算法的详细信息。


54

尝试添加

word-break: break-all 

到表格元素上的CSS。

这将使表单元格中的单词断裂,以使表的宽度不会比其包含的div宽,但表列的大小仍会动态调整。 jsfiddle演示


10
甚至更好word-wrap: break-word;
Apolo

1
@Apolo-同意!我在最初的jsfiddle演示中也使用了该代码(请参见上面答案主体中的链接)。
乔恩·施耐德

1
闪烁浏览器的word-break: break-word效果最佳。
Volvox

最好使用overflow-wrap属性,因为它是标准化的
Romko

18

display: block;和添加overflow: auto;.my-table。这将简单地切断280px超出您执行的限制的任何内容。由于像pélagosthrough这样的单词比宽,因此无法通过要求使它“看起来很漂亮” 280px


仅就我的情况而言:我需要表格适合所有父级宽度(在高分辨率下),因此我用div包围了表格并对其应用了样式。现在,表格尝试使用所有父表格的宽度,但是如果表格内容溢出,则会显示滚动条。
manuman94 '18 -10-15

2

尝试添加到td

display: -webkit-box; // to make td as block
word-break: break-word; // to make content justify

溢出的TDS将与新行对齐。


1

好吧,考虑到您的限制,我认为overflow: scroll;.pagediv 上进行设置可能是您唯一的选择。280像素非常窄,并且考虑到您的字体大小,仅靠自动换行是行不通的。有些单词很长,无法包装。您可以大幅度减小字体大小,也可以使用溢出:滚动。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.