For horizontal and vertical alignment of text in graphics,
you might want to use the following CSS styles.
In particular, note that dominant-baseline:middle
is probably wrong,
since this is (usually) half way between the top and the baseline,
rather than half way between the top and the bottom.
Also, some some sources (e.g. Mozilla) use dominant-baseline:hanging
instead of
dominant-baseline:text-before-edge
. This is also probably wrong, since
hanging
is designed for Indic scripts.
Of course, if you're using a mixture of Latin, Indic, ideographs
or whatever, you'll probably need to read the documentation.
/* Horizontal alignment */
text.goesleft{text-anchor:end}
text.equalleftright{text-anchor:middle}
text.goesright{text-anchor:start}
/* Vertical alignment */
text.goesup{dominant-baseline:text-after-edge}
text.equalupdown{dominant-baseline:central}
text.goesdown{dominant-baseline:text-before-edge}
text.ruledpaper{dominant-baseline:alphabetic}
Edit: I've just noticed that Mozilla also uses dominant-baseline:baseline
which is definitely wrong: it's not even a recognized value! I assume it's defaulting to the font default, which is alphabetic
, so they got lucky.
More edit: Safari (11.1.2) understands text-before-edge
but not text-after-edge
. It also fails on ideographic
. Great stuff, Apple. So you might be forced to use alphabetic
and allow for descenders after all. Sorry.