简体   繁体   English

自动换行:断词; 修复或替代

[英]word-wrap: break word; fix or alternative

With word-wrap: break-word;自动换行:断字; if the word is going to break anyway, I dont want a line-break first, it is useless and ugly.如果这个词无论如何都要断,我不想先换行,这是无用的和丑陋的。 How to avoid that?如何避免这种情况?

Example:例子:

Given a div with word-wrap: break-word;给定一个带有自动换行的 div : break-word; and a width equivalent to those 23 "x" characters like so:宽度等于 23 个“x”字符,如下所示:

xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx


The div innerHTML is "xx xxxxxxxxxxxxxxxxxxxxxxxxxxxx" like so: div innerHTML 是“xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,如下所示:

<div>xx xxxxxxxxxxxxxxxxxxxxxxxxxxxx</div>   


This is how the innerHTML of the div is displayed vs how it is wanted respecting the 23 "x" width.这就是 div 的 innerHTML 的显示方式与 23"x" 宽度所需的方式。

What happens:发生什么了:
xx xx
xxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxx
xxxxx xxxxxx

What I want:我想要的是:
xx xxxxxxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxxxxx
xxxxxxxx xxxxxxxxx



word-break: break-all;断字:断断续续; and similar or more recent attributes are not a solution to the problem.并且类似或更近的属性不是问题的解决方案。 I dont want words that can avoid breaking to break.我不想要可以避免断断续续的词。 I only want words with a width superior than the container width to break BUT without line-break first like with word-wrap: break-word;我只希望宽度大于容器宽度的单词在没有换行符的情况下先打断,就像word-wrap: break-word; because it is simply useless and ugly.因为它根本没用而且丑陋。

In complement of the answer of MaxiGui.补充MaxiGui的答案。 You can also use non-breaking space &nbsp;您还可以使用不间断空格&nbsp; in your HTML (or replace space with non-breaking space with JS).在您的 HTML 中(或用 JS 用不间断空格替换空格)。 But I think it's better to use of word-break: break-all;但我认为最好使用word-break: break-all;

EDIT : hyphens do all the job https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens编辑:连字符完成所有工作https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens

As @Sfili_81 mentionned it正如@Sfili_81 提到的那样

something like word-break: break-all;类似于word-break: break-all;

Here you go干得好

 var txt = document.getElementById("demo").innerHTML; var dataArr = txt.split(' '); var paragraph = document.getElementById("demo"); var finalString = ""; for (var i = 0; i < dataArr.length; i++){ if (dataArr[i].length > 6 ) { finalString = finalString + " <span>"+ dataArr[i]+"</span>"; } else{ finalString = finalString + " " + dataArr[i]; } } paragraph.innerHTML = finalString;
 div{ width:50px; background: red; } span{ word-break:break-all; }
 <div id="demo">test teeeeeeeeeeest test test test</div>

PS: I still flagged the question as duplicate from: How to force a line break in a long word in a DIV? PS:我仍然将问题标记为重复: How to force a line break in a long word in a DIV?

EDIT编辑

As break-all is also breaking the whole text / word and break-word is letting a white space / gap before the long word, then it is not adapting.由于break-all也打破了整个文本/单词,而break-word在长单词之前留了一个空格/间隙,那么它就没有适应。 So only solution is JS.所以唯一的解决方案是JS。 In this example, We are injecting span element to text with length over 6.在这个例子中,我们将 span 元素注入到长度超过 6 的文本中。

An alternative solution using jQuery:使用 jQuery 的替代解决方案:

HTML: HTML:

<div id="demo">xx xxx xxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxx</div>

JS: JS:

$(document).ready(function() {
  var words = $('#demo').text().split(' ');
  $('#demo').empty();
  $.each(words, function(i, v) {
    $('#demo').append($('<span>').text(v));
  })
});

CSS: CSS:

#demo {
  width: 100px;
  border: 1px solid #F00;
  word-wrap: wrap;
  word-break: break-all;
}

#demo span {
  display: inline-block;
  border: 1px dashed #00F;
  word-wrap: break-word;
  margin-right: 2px; /* add the space back */
}

This solution is slightly better than the accepted one, as it does not require a hardcoded value of the words' length.此解决方案比公认的略好,因为它不需要字长的硬编码值。

JSFiddle demo JSFiddle 演示

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

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