In old newspapers, titles of articles would, if they stretched over three lines, often be centered like this (picture):
|RACKET KINGPINS |
| ARE ROUNDED UP |
| IN BOMB KILLING|
I.e. the alignment for the first line is left, second center, third right. Similarly, for titles that stretched over two lines, the first would be left-aligned and the second right-aligned. For one-liners, the title would be centered.
I've been trying to emulate this for a theme I'm working on but the problem is, how do I separate the lines from each other? The title for each post does of course not contain any actual hard-coded line breaks but break automatically according to the width constraints, so I can't just look for line breaks to split up the innerHTML
of the DOM, for example.
So how would you go about doing this?
(I welcome a JavaScript solution, if that was unclear)
You can try looping through all the characters, in each loop, select the current character into a range object, from that you can use
getBoundingClientRect()
method of the range object to get thebottom
of the character rect. It's the way we detect the ending character on each line. Here is the signs to detect the ending character:getBoundingClientRect()
) with bothtop
andbottom
being0
.word-break:break-all
, the ending character may be some other character (not the space character). In this case we will detect the next character following the ending character, the next character will havebottom
different from the ending character'sbottom
.So it's fairly complicated indeed. In fact at first I thought we had only 1 case (the second case) but if we ignore the first case, we will have unexpected result. So firstly we need to check the first case, then the second case. Now detecting the ending character in each line helps us separate the whole text into separate lines. Then we can wrap each line in a
div
element with appropritate style set (to align the text in each line).Here is the demo's code:
HTML:
CSS:
JS:
Demo.
Without JavaScript, you can't.
This is one of the limitations of HTML/CSS. If you need printing capabilities where you control exact position and size of text, use PDF.
Without JavaScript, you can't predict if a paragraph will span multiple lines and how much, simply because a user:
May not have the same fonts installed,
Or may have configured the text to appear bigger in his browser.
You can, instead:
Add manually the line breaks in a way that in most situations, the width of every line will be inferior to the width of the container,
Forbid line breaks (
white-space: nowrap;
) and:Align the three lines according to your needs.
With JavaScript, you may determine where to put line breaks by counting the actual number of lines, and then reducing the text character by character, until you get two lines, then one line, finding this way the exact locations where you may insert line breaks.
I consider it hackish enough, but, well, it's just my opinion. You may, on the other hand, care that changes to user configuration (such as text-only zoom like the one which was used in old browsers) once the page is loaded may break the layout.
You could create a function to
explode
the string into an array of chars in javascript and then manipulate the array as needed.You could make he manipulation easier by using a pr-made "word wrap" function on the string prior to exploding it, so that the title will automatically have
\n
characters inserted appropriately beforehand according to your max width specification.Then after exploding you just need to wrap the sections in divs by inserting them in the array at the
\n
break points and use css classes to line them up as needed.Then
implode
the array back into a string...