Take control of text styled with css columns attri

2019-07-30 16:51发布

问题:

Ingredients: a div containing some text which we don't know the length of. What we know about the text is just it contains some keywords, 'mango' for example, and those keyword must placed at the start of a new column (like those keyword are subtitles).

Question: Is there a way to achieve this goal with HTML5 + CSS3 (preferably without using JS, jQuery or some script-based-patches)?

Actual situation (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>

EDIT: To understand better the question, I add the note that we don't know anything about the text that will be shown. So it may contain the keyword (in this case mango) or not. The aim of the question is how to take control over the text that will be shown.

回答1:

Here is one way, where one replace the capitalized words so the will be wrapped in an element along with their following text.

With this you can also deal with 2 words and get 3 columns etc.

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>$1') + '</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>


If you get issues with browser support, here is a Flexbox version

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>$1') + '</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>


And here is a Table version

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span>$1') + '</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>


If you want to style and/or have the capitalized word on a line by itself, you simply wrap it an extra element '</span><span><span>$1</span>'

window.addEventListener("load", function() {
  var div = document.querySelector('div');
  div.innerHTML = '<span>' + div.innerHTML.replace(/(\b[A-Z]+\b)/g, '</span><span><span>$1</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>