好吧,这真把我搞糊涂了。我在一个div里面有一些内容,就像这样:

<div style="background-color: green; width: 200px; height: 300px;">

Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.

</div>

但是,由于“word”太长,内容会溢出DIV(正如预期的那样)。

我怎么能迫使浏览器在必要的地方“打破”这个词,以适应里面的所有内容?


当前回答

试试我们的风格

white-space: normal;

其他回答

我刚刚在谷歌上搜索了同样的问题,并在这里发布了我的最终解决方案。这也和这个问题有关。

您可以通过赋予div样式word-wrap: break-word轻松地做到这一点(您可能还需要设置它的宽度)。

div {
    word-wrap: break-word;         /* All browsers since IE 5.5+ */
    overflow-wrap: break-word;     /* Renamed property in CSS3 draft spec */
    width: 100%;
}

然而,对于表,你还需要应用:table-layout: fixed。这意味着列的宽度不再是流动的,而是仅根据第一行中的列的宽度(或通过指定的宽度)定义。点击这里阅读更多。

示例代码:

table {
    table-layout: fixed;
    width: 100%;
}

table td {
    word-wrap: break-word;         /* All browsers since IE 5.5+ */
    overflow-wrap: break-word;     /* Renamed property in CSS3 draft spec */
}

这可以添加到“跨浏览器”解决方案的可接受答案中。

来源:

http://kenneth.io/blog/2012/03/04/word-wrapping-hypernation-using-css/ http://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/

.your_element{
    -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;
}

首先,您应该确定元素的宽度。例句:

# sampleDiv { 宽度:80%; 自动换行:break-word; } < div id = " sampleDiv " > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < / div >

因此,当文本达到元素宽度时,它将被分解成行。

我用下面的代码解决了这个问题。

display: table-caption;

这样做:

<div id="sampleDiv">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>

#sampleDiv{
   overflow-wrap: break-word;
}