控制使用 css 列属性设置样式的文本

Take control of text styled with css columns attribute

成分:一个div包含一些我们不知道长度的文本。我们对文本的了解只是它包含一些关键字,例如 'mango',并且这些关键字必须放在新列的开头(例如这些关键字是 字幕 ) .

问题:有没有办法用HTML5+CSS3(最好不用使用 JS,jQuery 或一些 基于脚本的补丁 )?

实际情况(DEMO):

div {
  columns: 2;
  width: 100%;
  height: auto;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud MANGO ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

编辑: 为了更好地理解这个问题,我添加了注释,我们对将要显示的文本一无所知。因此它 可能 包含 关键字 (在本例中为 mango)或不包含。问题的目的是如何控制将要显示的文本。

这是一种方法,其中替换大写单词,以便将其连同其后续文本一起包装在一个元素中。

用这个你也可以处理2个单词并得到3列等等

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>') + '</span>'
});
div {
  -webkit-columns: 2;
  -moz-columns: 2;
  columns: 2;
  width: 100%;
  height: auto;
}

div span {
  display: block;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
}
<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud MANGO ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
  reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>


如果您遇到浏览器支持问题,这里有一个 Flexbox 版本

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>') + '</span>'
});
div {
  display: flex;
  width: 100%;
  height: auto;
}

div span + span {
  margin-left: 10px;
}
<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud MANGO ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>


这里是 Table 版本

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>') + '</span>'
});
div {
  display: table;
  width: 100%;
  height: auto;
}
div span {
  display: table-cell;
}
<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud MANGO ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>


如果你想要样式 and/or 将大写单词单独占一行,你只需将它包装一个额外的元素 '</span><span><span></span>'

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span><span></span>') + '</span>'
});
div {
  -webkit-columns: 2;
  -moz-columns: 2;
  columns: 2;
  width: 100%;
  height: auto;
}

div span {
  display: block;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;
}

div span span {
  /* display: inline-block;       uncomment this to avoid new line  */
  color: orange;
  font-weight: bold;
  font-style: italic;
}
<div>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud MANGO ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
  reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>