简体   繁体   English

如何在 DIV 中的长单词中强制换行?

[英]How to force a line break in a long word in a DIV?

Okay, this is really confusing me.好吧,这真的让我很困惑。 I have some content inside of a div like so:我在 div 中有一些内容,如下所示:

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

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

</div>

However, the content overflows the DIV (as expected) because the 'word' is too long.但是,由于“单词”太长,内容会溢出 DIV(如预期的那样)。

How can I force the browser to 'break' the word where necessary to fit all of the content inside?如何强制浏览器在必要的地方“打破”单词以适应所有内容?

Use word-wrap:break-word; 使用 word-wrap:break-word;

It even works in IE6, which is a pleasant surprise. 它甚至可以在 IE6 中运行,这是一个惊喜。


word-wrap: break-word has been replaced with overflow-wrap: break-word; word-wrap: break-word已替换为overflow-wrap: break-word; which works in every modern browser.它适用于每个现代浏览器。 IE, being a dead browser, will forever rely on the deprecated and non-standard word-wrap instead. IE,作为一个死的浏览器,将永远依赖于已弃用和非标准的word-wrap

Existing uses of word-wrap today still work as it is an alias for overflow-wrap per the specification.今天, word-wrap的现有用途仍然有效,因为它是每个规范的overflow-wrap的别名。

I am not sure about the browser compatibility我不确定浏览器的兼容性

word-break: break-all;

Also you can use the <wbr> tag您也可以使用<wbr>标签

<wbr> (word break) means: "The browser may insert a line break here, if it wishes." <wbr> (分词)意思是:“如果浏览器愿意,可以在这里插入一个换行符。” It the browser does not think a line break necessary nothing happens.如果浏览器认为不需要换行,则什么也不会发生。

This could be added to the accepted answer for a 'cross-browser' solution.这可以添加到“跨浏览器”解决方案的公认答案中。

Sources:资料来源:

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

I was just Googling the same issue, and posted my final solution HERE .我只是在谷歌上搜索同样的问题,并在此处发布了我的最终解决方案。 It's relevant to this question too.这也与这个问题有关。

You can do this easily with a div by giving it the style word-wrap: break-word (and you may need to set its width, too).您可以使用 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%;
}

However, for tables , you also need to apply: table-layout: fixed .但是,对于表格,您还需要申请: table-layout: fixed This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths).这意味着列宽不再是可变的,而是仅基于第一行中列的宽度(或通过指定的宽度)定义。 Read more here . 在这里阅读更多

Sample code:示例代码:

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 */
}

&#8203; is the HTML entity for a unicode character called the zero-width space (ZWSP) which is an invisible character which specifies a line-break opportunity.是 unicode 字符的 HTML 实体,称为零宽度空间 (ZWSP),它是指定换行机会的不可见字符。 Similarly the hyphen's purpose is to specify a line-break opportunity within a word boundary.同样,连字符的目的是在单词边界内指定换行机会。

Found that using the following worked across most major browsers (Chrome, IE, Safari iOS/OSX) except Firefox (v50.0.2) when using flexbox and relying on width: auto .发现在大多数主要浏览器(Chrome、IE、Safari iOS/OSX)上使用以下内容都有效,除了 Firefox(v50.0.2)使用 Z7F3D565C67F89F217A301D03D795B 时使用width: auto

.your_element {
    word-wrap: break-word;   
    overflow-wrap: break-word;
    word-break: break-word;
}

Note: you may need to add browser vendor prefixes if you are not using an autoprefixer.注意:如果您不使用自动前缀,您可能需要添加浏览器供应商前缀。

Another thing to watch out for is text using &nbsp;另一件需要注意的是使用&nbsp;的文本。 for spacing can cause line breaks mid-word. for 间距会导致单词中间换行。

Whitespace is preserved by the browser.空白由浏览器保留。 Text will wrap when necessary, and on line breaks文本将在必要时换行,并在换行符处

.pre-wrap {
    white-space: pre-wrap;
    word-break: break-word;
}

DEMO演示

 td { word-break: break-word; white-space: pre-wrap; -moz-white-space: pre-wrap; } table { width: 100px; border: 1px solid black; display: block; }
 <table> <tr><th>list</th> <td> 1.longtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtext 2.breaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreakline </td> </tr> </table>

CSS word-wrap:break-word; CSS word-wrap:break-word; , tested in FireFox 3.6.3 ,在 FireFox 3.6.3 中测试

Remove white-space: nowrap , if there is any.删除white-space: nowrap ,如果有的话。

Implement:实施:

white-space: inherit;
word-break: break-word;

I solved my problem with code below.我用下面的代码解决了我的问题。

display: table-caption;

First you should identify the width of your element.首先,您应该确定元素的宽度。 Eg:例如:

 #sampleDiv{ width: 80%; word-wrap:break-word; }
 <div id="sampleDiv">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>

so that when the text reaches the element width, it will be broken down into lines.这样当文本达到元素宽度时,它将被分解为行。

From MDN :来自MDN

The overflow-wrap CSS property specifies whether or not the browser should insert line breaks within words to prevent text from overflowing its content box. overflow-wrap CSS 属性指定浏览器是否应在单词中插入换行符以防止文本溢出其内容框。

In contrast to word-break , overflow-wrap will only create a break if an entire word cannot be placed on its own line without overflowing.word-break相比, overflow-wrap仅在无法将整个单词放在自己的行上而不溢出时才会创建中断。

So you can use:所以你可以使用:

overflow-wrap: break-word;

Can I use ?我可以使用吗?

As mentioned in @davidcondrey's reply, there is not just the ZWSP, but also the SHY &#173; &shy;正如@davidcondrey 的回复中提到的,不仅有ZWSP,还有SHY &#173; &shy; &#173; &shy; that can be used in very long, constructed words (think German or Dutch) that have to be broken on the spot you want it to be.可以用在很长的构造词中(想想德语或荷兰语),必须在想要的地方被打破。 Invisible, but it gives a hyphen the moment it's needed, thus keeping both word connected and line filled to the utmost.不可见,但它会在需要的时候给出一个连字符,从而最大限度地保持单词连接和行填充。

That way the word luchthavenpolitieagent might be noted as lucht&shy;haven&shy;politie&shy;agent which gives longer parts than the syllables of the word.这样,单词luchthavenpolitieagent可能会被记为lucht&shy;haven&shy;politie&shy;agent ,它给出的部分比单词的音节更长。
Though I never read anything official about it, these soft hyphens manage to get higher priority in browsers than the official hyphens in the single words of the construct ( if they have some extension for it at all).虽然我从来没有读过任何关于它的官方信息,但这些软连字符在浏览器中的优先级高于构造的单个单词中的官方连字符(如果它们有一些扩展名的话)。
In practice, no browser is capable of breaking such a long, constructed word by itself;实际上,没有浏览器能够自行破解这么长的构造词。 on smaller screens resulting in a new line for it, or in some cases even a one-word-line (like when two of those constructed words follow up).在较小的屏幕上会产生一个新的行,或者在某些情况下甚至是一个单词行(例如当其中两个构造单词跟随时)。

FYI: it's Dutch for airport police officer仅供参考:机场警察是荷兰语

word-break: normal seems better to use than word-break: break-word because break-word breaks initials such as EN word-break: normal 似乎比 word-break: break-word 更好用,因为 break-word 会破坏首字母,例如 EN

word-break: normal
.word-break {
   word-break: keep-all;
   word-wrap: break-word;
}

You can use a grid system for this.您可以为此使用网格系统

<div class="container" style="background-color: green; width: 200px; height: 300px;">
   <div class="row">Thisisatest.Thisisatest.Thisisatest.</div>
   <div class="row">Thisisatest.Thisisatest.Thisisatest.</div>
 </div>

Do this:做这个:

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

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

just try this in our style试试我们的风格

white-space: normal;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM