Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you open most books on the shelf you will notice that text is not left-aligned, but is in fact justified to create a consistent vertical margin on the right-hand side[0]. It's the big thing I always miss when I try reading on the web.

One way to do this is with JavaScript implementations of the LaTeX hyphenation/justification algorithm. Check out [1] and [2] for examples.

[0]: https://i.imgur.com/65ICUVW.png

[1]: https://github.com/robertknight/tex-linebreak

[2]: https://github.com/bramstein/typeset



Justified text looks pretty, but why does that matter? Most books are bought to be read, not to sit there admiring the right edge justification.

There's even a plausible theory that justified text is bad for readability (perhaps because the brain loses that hint of which line is which). I haven't seen a lot of studies[1] replicating it, but I don't see any studies taking the other side. There's also the problem that it alters the inter-word spacing, which might also reduce readability since the brain can't rely on consistent word spacing.

I sympathize if you're conditioned to be annoyed every time you see an unjustified right column, but that's not a reason to recommend justified text.

Publishers (and authors, when they're considering text style details) need to understand that ebook publishing isn't the same as publishing print books. For print, decisions about font and styling have to be made, because otherwise there's no text.

For an ebook, unless it's a picture book or textbook with careful design[2] or some other kind of book where layout is the primary concern and readability is secondary, no features of the main style of text should be hardcoded. It needs to be left up to the ebook reader, and whatever defaults plus user customization it has.

[1] e.g. https://doi.org/10.1111/j.1467-8535.1986.tb00491.x

[2] These kinds of books, where publisher-dictated styling makes sense, shouldn't even be published as epubs; it often ends up as a disaster no matter how hard they try. They should publish the books as pdfs only.


To me, you are conditioned to prefer right aligned text.

It is OK, we all configure our readers to our taste.


I haven't looked through fully to make sure it has no ill effects, but if you add to the css on p:

   text-align: justify;
   text-justify: inter-word;
you should get what you're looking for. At least it worked in Firefox inspector.


Don’t think I’ve ever seen anyone suggest setting text-justify. It’s one of those properties that I could imagine having been used in anger and with cause (justifiedly :P) less than a hundred times in total, though the true number is probably higher than that. Anyway, in this case it won’t change anything, because the text is English, and the default value of auto will be equivalent to inter-word. (If the text was Japanese, it would actively harm things, because auto would be equivalent to inter-character, and you’d mess that up.)

Anyway, in all current browsers, `text-align: justify` does greedy text layout, where it fits as many words in each line as it can before wrapping. This is technically trivial, but visually inferior to Knuth-Plass line breaking, which tries to find the most visually-pleasing place to break lines. Look at the screenshots of https://github.com/robertknight/tex-linebreak to see the difference. It can be rather stark.


I think text-justify is fine as long as the text width is somewhat wide (>72 characters) and it's inside a @supports(hyphens: auto). It's not as nice as TeX, but it's fast and Good Enough.


Are there any implementations of Knuth-Plass line breaking in HTML or CSS libraries? Maybe in a static site generator?


It's possible, but the markup is very, very messy. I made a proof-of-concept by hand a couple months ago [1], but it would require quite a bit more work to automate it. It's probably best to just wait for browsers to implement CSS Text Module Level 4, which includes a property for enabling use of better line breaking algorithms.

[1] https://mpetroff.net/files/pre-calculated-line-breaks-demo/


Haha we always reach for javascript first.


Thank you!

With modern 4k screens in very small displays the old argument of "computers don't have enough resolution to show justified text properly" becomes absurdly outdated.

We now have the technology to have nice justified text everywhere. Please use it.

Right aligned jagged text looks ugly.


> One way to do this is with JavaScript implementations of the LaTeX hyphenation/justification algorithm.

Another is just to use CSS (text-align: justify).

Why use JS for this?




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: