Text and typography / text-align: justify
Should PDF body text be justified
Justified text stretches the spaces on each line so both edges align. It is the default in most printed books and it needs hyphenation to look right.
What it is for
Justification without hyphenation produces very uneven word spacing in narrow measures, which reads worse than a ragged edge. The two settings belong together or not at all.
Support: Works everywhere
Supported. text-justify, which controls the algorithm, is much less consistently implemented and is usually not worth relying on.
The syntax
Written the way it would appear in a document stylesheet, using text-align: justify as intended.
/* Together, or neither. */
.document p {
text-align: justify;
hyphens: auto; /* needs lang on html */
}
/* Not in narrow columns, where the spacing damage outweighs the tidy edge. */
.sidebar p {
text-align: left;
hyphens: none;
}The mistake people make
Justifying a narrow column without hyphenation. The result has visible rivers of white running down the block, which is worse than the ragged right edge it replaced.
Frequently asked
Does text-align: justify work when generating a PDF?
Works everywhere. Supported. text-justify, which controls the algorithm, is much less consistently implemented and is usually not worth relying on.
How would I know if it were not working?
You would not, from the CSS. Unimplemented and unmatched CSS both fail silently, which is why the reliable test is to set a deliberately extreme value and see whether anything changes. If a 40mm margin looks the same as an 18mm one, the rule is not reaching the page.
Related properties
Things that come up in the same part of a document stylesheet.
How to embed a custom font in a PDF
@font-face. Works everywhere.
Which font-display value should a PDF use
font-display. Works everywhere.
How to use different fonts for different scripts in a PDF
unicode-range. Works everywhere.
How to align numbers in a PDF table column
font-variant-numeric. Works everywhere.
How to enable hyphenation in a PDF
hyphens. Works everywhere.
Every property, with its support status
The full reference, grouped by what part of the page it touches.
Paste the CSS and see what the rendered page does with it.