Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid one word on the last line with CSS?

Tags:

css

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
like image 962
bart Avatar asked Jan 28 '11 00:01

bart


People also ask

How do you break a word to the next line in CSS?

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.

How do you break a text line in CSS?

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).

How do you stop a word-break in CSS?

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).

How do you skip a line in CSS?

To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag.


5 Answers

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&nbsp;bar

or put the last two words into a span:

<span style="white-space: nowrap">foo bar</span>
like image 168
Pekka Avatar answered Sep 30 '22 10:09

Pekka


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("&nbsp;");

        // 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);
like image 36
Jack Vial Avatar answered Sep 30 '22 08:09

Jack Vial


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&nbsp;"):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.

like image 39
Adam Grant Avatar answered Sep 30 '22 10:09

Adam Grant


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.

like image 30
Phil Powell Avatar answered Sep 30 '22 08:09

Phil Powell


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>
like image 33
K3---rnc Avatar answered Sep 30 '22 10:09

K3---rnc