Suppose a text shows up as follows on an HTML page (just one word too long to fit on one line):
Lorem ipsum dolores amet foo
bar
How can one avoid with CSS that the last word appears on the last line, and force two (or more)?
Lorem ipsum dolores amet
foo bar
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. word-break: break-all; It is used to break the words at any character to prevent overflow.
A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).
If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).
To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag.
I don't think you can do this in pure CSS.
You would have to either put a non-breaking space in between the last two words:
foo bar
or put the last two words into a span
:
<span style="white-space: nowrap">foo bar</span>
I don't think it can be done in CSS alone but this is what I came up with to solve the one word per line problem. It will replace the last n spaces with a non breaking space for all the matched elements.
https://jsfiddle.net/jackvial/19e3pm6e/2/
function noMoreLonelyWords(selector, numWords){
// Get array of all the selected elements
var elems = document.querySelectorAll(selector);
var i;
for(i = 0; i < elems.length; ++i){
// Split the text content of each element into an array
var textArray = elems[i].innerText.split(" ");
// Remove the last n words and join them with a none breaking space
var lastWords = textArray.splice(-numWords, numWords).join(" ");
// Join it all back together and replace the existing
// text with the new text
var textMinusLastWords = textArray.join(" ");
elems[i].innerHTML = textMinusLastWords + " " + lastWords;
}
}
// Goodbye lonely words
noMoreLonelyWords("p", 3);
Just wrote this dependency-free JS snippet that will solve this very problem
https://github.com/ajkochanowicz/BuddySystem
Essentially this is the source code
var buddySystem=function(e){var n=[],r=[]
n=e.length?e:n.concat(e),Array.prototype.map.call(n,function(e){var n=String(e.innerHTML)
n=n.replace(/\s+/g," ").replace(/^\s|\s$/g,""),r.push(n?e.innerHTML=n.replace(new RegExp("((?:[^ ]* ){"+((n.match(/\s/g)||0).length-1)+"}[^ ]*) "),"$1 "):void 0)})}
and you can implement it by doing this
objs = document.getElementsByClassName('corrected');
buddySystem(objs);
Now you'll never have a word by itself for any tags with the corrected
class.
You can also use jQuery if you want.
$(".corrected").buddySystem()
Check out the link for all possibilities.
Can't be done with CSS, but Shaun Inman wrote a very useful bit of Javascript to help with this a while ago:
http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin
It's a Wordpress plugin, but there are plenty of non-Wordpress clones around.
If JavaScript is an option, one can use typogr.js, a JavaScript "typogrify" implementation. This particular filter is called Widont.
<script src="https://cdnjs.cloudflare.com/ajax/libs/typogr/0.6.7/typogr.min.js"></script>
<script>
document.body.innerHTML = typogr.widont(document.body.innerHTML);
</script>
</body>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With