UPDATE: Just posted a new simplified technique but unfortunately it only works in Firefox so far, not Chrome: https://kristinlbradley.wordpress.com/2014/07/29/improved-justify-last-line-paragraph-text/
Original Method:
Normally if you use CSS to justify text, the last line of text is aligned left rather than justified as on other lines. This is the desired behavior in most instances but for some layouts you may want to justify the last line as text as well. Following is a cross-browser method you can use which will work in IE6+ in addition to popular modern browsers. (This method is also useful if you want to justify just one line of text such as in a heading.)
Tested in IE6, IE7, IE8, FF, Safari, Chrome
Regular Justified Text vs. Text Using This Method:
CSS:
p, h1 { text-align: justify; text-align-last: justify; } p:after, h1:after { content: ""; display: inline-block; width: 100%; }
A paragraph tag (p) and a header (h1) are used above just as examples. You can substitute any block level html elements instead such as a div.
See it in action: http://www.webcodeshare.appspot.com/agx3ZWJjb2Rlc2hhcmVyDwsSB1dlYmNvZGUY8vsQDA
Caveat: One issue with this method is that extra space is added to the bottom of html elements in browsers other than IE. It appears to happen as a result of how line-height spacing is added by browsers to the last visible line of text.
Update: You can add a negative bottom margin to the elements you are justifying (such as “margin-bottom: -1.2em;”) to remove extra spacing added by :after. You will need to add a margin back for IE.
Robert
/ June 14, 2013All justified like that looks bad, I need it to adjust words throughout the paragraph so the last line doesn’t look any more spaced out than the other lines. I have a specific need to do that – ashame there doesn’t seem to be an answer.
kristinlbradley
/ February 5, 2014I agree it’s usually a bad idea to justify the last line for most designs on the web.
There isn’t a really good practical way to adjust the spacing of text on that level on the web currently unfortunately.
Wilko
/ January 31, 2014OMG. I love you! I’ve been looking for a good tutorial on this. Nothing worked, until I found your post. Thanks!
kristinlbradley
/ February 5, 2014Glad you found it helpful. 🙂
japo32
/ July 29, 2014Reblogged this on Japo32 and commented:
Great easy way to justify text.
If you want to justify just a single line use this:
h2 {
height: 40px; /* Specifying the height and line-height prevents */
line-height: 40px; /* extra space from being added to the bottom. */
text-align: justify;
text-align-last: justify; /* Internet Explorer 6+ */
}
h2:after { /* All other browsers */
content: “.”;
display: inline-block;
width: 100%;
height: 0;
visibility: hidden;
}
kristinlbradley
/ July 29, 2014I just posted a new simplified way to do this that eliminates the issues of my old method: https://kristinlbradley.wordpress.com/2014/07/29/improved-justify-last-line-paragraph-text/ (My new method isn’t yet cross-browser tested though.)
kristinlbradley
/ July 29, 2014Did some more testing and it turns out the new method is working in Firefox but not Chrome. I’ll continue to test and see if I can improve it.