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

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

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

</div>

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

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


当前回答

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

display: table-caption;

其他回答

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

来源:

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;
}

用word-break: normal似乎比用word-break: break-word更好,因为break-word会打断EN这样的首字母

word-break: normal

使用自动换行:break-word;

它甚至可以在IE6中工作,这是一个令人惊喜的惊喜。


Word-wrap: break-word已替换为overflow-wrap: break-word;它适用于所有现代浏览器。IE作为一个已死的浏览器,将永远依赖已弃用的非标准换行。

目前word-wrap的现有用法仍然适用,因为它是规范中溢换的别名。

CSS换行:break-word;,在FireFox 3.6.3中测试

这样做:

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

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