1

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.

4
  • what's the purpose of this? wouldn't you end up with two columns which are different sizes but backwards (little column on the left instead of on the right)? Commented Apr 28, 2017 at 9:06
  • The pourpouse is to avoid the keyword (in this example mango) to be shown in the last row of the first column so to take control over the text that will be shown with this style. If the keyword is not present, then nothing happens. Imagine that the keyword will be the subtitle of a list, it wouldn't be so nice that the title is shown as the last word in the left column, wouldn't it? Commented Apr 28, 2017 at 9:23
  • oh, then is this for a list or a body of text like you've used in your demo? Commented May 1, 2017 at 2:07
  • @Anthony It's for both! The text to be shown MAY contain a list with a subtitle, but it's not mandatory: a body of text like I've used in my demo it's acceptable. Commented May 2, 2017 at 8:21

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.