Multi-Line Padded Text

This is one of those tricky CSS things that I see come up every few months. I guess what better place to address it than CSS-Tricks eh?

The situation involves ragged-right inline text. Like when a paragraph of text breaks to the next line whenever the next word won't fit (i.e. most text on the internet). You want to add a background behind that text which:

Follows the ragged-right edge

Is padded along both the left and right edge of each line

What you can't do is simply apply a background and padding to, say, the <p> element. Paragraphs are block-level, so the background will simply be a rectangle and not follow the ragged-right-ness.

You also can't simply apply the background and padding to a <span> or an inline element. The left and right padding will only apply to the very first and very last line. On each of the middle lines, the background will butt up immediately next to the text.

Describing this is a bit futile. Here's the problem visually:

What we want is for each line to be padded like the beginning of that first line and end of that last line. We don't want to resort to anything gross like wrapping each line in it's own span (where lines break is to unpredictable). And there is no such thing as :nth-line unfortunately.

There are some solutions though!

Harry Robert's Pseudo Element / white-space Method

The big trick here is using white-space: pre-wrap; That gives us the padding on the ragged-right lines. Then to get the padding along the left, a pseudo element is added along the left edge. Here's the original and then my fork to show the elements at work:

Note: I had to update this code when Firefox 32 dropped. Firefox requires box-decoration-break: clone; because the default is box-decoration-break: split;.

Dave Rupert's JavaScript / Unicode Method

Fair warning: Dave says don't use this. I'm including it because I think it's clever and in some weird way actually feels less hacky to me. The idea is to go through the text of each element and replace the spaces with the unicode character \u205f, the MEDIUM MATHEMATICAL SPACE character. This works with the padding better on the right edge for whatever reason. For the left edge, you just use a border-left along the block-level parent element.

It's a bit tricky to get the line-height just right so the border lines up with the padding, but I'm sure you can figure it out. There is probably even some fancy math to use to make sure it's right.

If JavaScript works for you, there is also a jQuery wraplines plugin in which then you could apply the padding to each individual line. Demo.

Matthew Pennell's Triple Element Method

Turns out you can do this with almost no fancy CSS or JS at all, but using three elements. You need a block-level parent for a border-left. Then an inline element to apply the padding and background to. Then another inline element to nudge the text back to the left to get the padding on the right edges.

<div class="padded-multiline">
<h1>
<strong>
How do I add padding to subsequent lines of an inline text element?
</strong>
</h1>
</div>

If you're looking at this in Firefox, there might be some line-height issues. I basically have no idea why that is, kinda frustrating, particularly in situations like this.

Adam Campbell's box-decoration-break Method

During a discussion that popped up over this, Adam pointed out there is a new CSS property that is (as I understand it) specifically for this. This removes the need for three elements. Technically you only need one, the inline element, but it's likely you'll be doing this on a header so you'll probably end up with a block-parent anyway, which is best for spacing.

This is working in Chrome and Safari, but not Firefoxnow working in Firefox 32+, in my quick tests. Chrome and Safari require it to be -webkit-box-decoration-break.

Prior Art

The people I attributed in this article were the people I first saw the technique from. But they don't necessarily represent The Absolute First Person Ever To Think Of It™. In the comments below, a few older blog posts came up that tackled this same issue. One by Sergey Chikuyonok (Russian) and one by HTeuMeuLeu (French).

You can use letter-spacing and text-shadow together to get some effect of a padding around the lines. However I went crazy with this and figured out you can also use text-indent and negative margin on inline elements and get some interesting results: http://codepen.io/Merri/pen/tDHsC

Sadly, the only one of the methods above that works when the line height is increased – so that you actually have several ”lines” of text with background and blank space in between the lines – is the box-shadow one. And that only works for colors, but not if you want to have a background image displayed over the whole line background.

I can‘t believe they have not come up with something in CSS yet that would allow you to specify that you want to have the left and right padding for an inline element that stretches over multiple lines to be applied at the beginning and end of each line :-(

I ended up with approaches like splitting text into elements containing single “words” with JS, and add side paddings to them, and trying to use word-spacing or negative margins to drag them close enough together again so that between words on one line the spacing looks like being the same as a single “space” again … but those approaches all had various cross-browser issues.

@Louis mentioned outlines. Those have wide support in browsers. Unfortunatenaly some browsers (Firefox) also have some rounding errors which can cause one pixel gaps between the outline and the text box. The good news is it can be patched with 1px of box-shadow to left and right.

Seems like the most cross-browser safe solution, looks great on anything I can test it on starting from IE8.

The same problem was under discussion in Sergey Chikuyonok’s (Emmet author) blog back in 2010: “Even background under the text” (in russian). There are two solutions proposed: one utilizes ‘outline’ property and another uses nested elements with relative positioning.

Nice post!! Once I had to do something similar.
I had an h1 where every word whitin it, was wrapped in a span element. Those span elements are floating and had a padding left/right to create the white-space between the words. Now I see there are a lot of solutions, but ey! it works. Here is my example: http://rehabitare.com/