BACK TO BLOG LIST

SUBSCRIBE: RSS ATOM

Prevent Your Em-Dashes From Wrapping

by NJB // // Lafayette, CO

© 2024 Nicholas Bernhard

CC-BY-SA 4.0

In my e-books, I take care to use proper em dashes, like so: —. It’s important for me to use proper em dashes rather than, say, one, two, or three hyphens.

There is a problem, though, with em dashes on web pages: the HTML renderer will sometimes wrap them incorrectly. I think starting a line with an em dash doesn’t look great, and it looks wrong to have a line consist of a single em dash.

Consider this line from the Clark Ashton Smith poem Recompense:

Screenshot of a web page showing a line from the Clark Ashton Smith poem Recompense. The em dash at the end of the line has been wrapped to a new line.

In some cases, this may be fine, but let’s try to aim a little higher. The solution is to wrap the em dash and the word before it in a <span> element, and then give the span the styling “white-space: nowrap;”.

Simple enough, but how can the creation of the span element be automated during the parsing process?

I put the question to people on FOSStodon, and received this response from wizzwizz4:

string = string.replace(/(\s)(\S+—)/gi, “$1$2”);

This is the result:

Screenshot of a web page showing a line from the Clark Ashton Smith poem Recompense. In this screenshot, the last word of the line, ‘desire’, and the em dash character have been wrapped together in a span element. ‘Desire’ and the em dash both appear together on the second line.

Seems to be working well, or at least headed in the right direction. Than you, wizzwizz4!

This may seem like a little thing, because it is. Still, it’s a good example of Charles Colton’s famous summary of Michelangelo’s art: “Trifles make perfection, and perfection is no trifle.”

SHOW/HIDE COMMENTS
  • No comments yet.

WANT TO COMMENT?