Achieving full justification in HTML and CSS: works in limited-quirks mode but no-quirks mode messes up the height
Asked Answered
B

1

11

I am trying to achieve full justification (as distinct from left justification, where the final line is left-aligned rather than justified) in HTML and CSS.

I have this document, plus a doctype definition:

<style>
    p {
        border: 1px solid blue;
        text-align: justify;
    }
    p::after {
        content: "";
        width: 100%;
        display: inline-block;
    }
</style>
<meta charset=utf-8>
<title>Justification</title>
<p>Foo bar</p>
<p>Foo bar</p>
<p>Foo bar</p>

With the HTML 4.01 Transitional doctype (<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">), the document is rendered in limited-quirks mode and each paragraph is fully justified as desired, with no extra space being taken.

With the HTML 5 doctype (<!DOCTYPE html>) or with the HTML 4.01 (Strict) doctype (<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> ), the document is rendered in no-quirks mode and each paragraph is fully justified, but takes an extra line of space. Adding height: 0 to the ::after does nothing (it already has no height as the likes of background: red will demonstrate).

Live demonstrations: HTML 4.01 Transitional and HTML 5 editions.

How can I get the HTML 4.01 Transitional rendering in the document with the Strict or HTML 5 doctype?

(Incidentally, I am aware of the workaround, given known contents, of assigning a value for height to the p element and depending on the default overflow behaviour to achieve effectively the right result. I will not accept that as an answer—I am seeking a genuine solution that can be done without hidden knowledge or JavaScript interception of resizing—assume the paragraph to be an arbitrary number of lines.)

Blondell answered 29/6, 2014 at 4:3 Comment(5)
Be careful not to call the HTML 4 transitional doctype "the HTML 4 doctype". The strict HTML 4 doctype will produce the same result as the HTML5 doctype does. So this is not a difference between HTML 4 and HTML5, but the difference between strict and almost-strict mode.Teleran
@Teleran It’s so long since I’ve written HTML4 that I had forgotten about the strict doctype for some reason, even though I still recalled the XHTML strict/transitional divide!Blondell
@BoltClock: thanks for the hint there; you’re absolutely right, and that may help me in tracking it down. Hopefully it’s possible! I’ve updated the question to clarify these things.Blondell
The difference between left justification and full justification isn't just the last line, it's that in left justification the right side of the text can be ragged.Xenia
@JasonS: left alignment allows ragged right; left justified alignment (“left justification”) means that all lines are justified except the last, which is left-aligned. Right justified text is perfectly reasonable, for example, meaning that the last line is right-aligned.Blondell
A
12

Instead of the :after trick that tries to control the justification of the last line, use the text-align-last property, which is now rather well supported if you additionally use a -moz- prefixed version:

p {
    text-align: justify;
    -moz-text-align-last: justify;
    text-align-last: justify;
}
Atcliffe answered 29/6, 2014 at 8:46 Comment(2)
Great! Didn’t know about that one, and it does exactly what I wanted. ☺ Not sure if Opera and Safari support it at present; the MDN article suggests they don’t, but it’s probably out of date, especially with regards to the Blink‐based Opera; for what I’m doing at present that’s an acceptable trade‐off anyway.Blondell
still no safari, iOS, opera as of Dec 2016. bummer.Manifold

© 2022 - 2024 — McMap. All rights reserved.