Multi-Line Padded Text

Chris Coyier //

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:

  1. Follows the ragged-right edge
  2. 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:

See the Pen BtpGo by Chris Coyier (@chriscoyier) on CodePen

Unfortunately Firefox doesn't dig the way the pseudo element is positioned in that technique. Probably fixable, but, there might be a better way...

Fabien Doiron's box-shadow Method

Turns out you can use zero-spread box-shadow on an inline element on only the x-axis to pad each line. Essentially:

.padded-multi-line {
  display: inline;
  background: orange;
  box-shadow: 10px 0 0 orange, -10px 0 0 orange;
}

Here is the original and then my fork to show how it works:

See the Pen Wrapping Highlighted Text by Chris Coyier (@chriscoyier) on CodePen

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.

Here is the original and my stripped down fork:

See the Pen Wrapping Highlighted Text by Chris Coyier (@chriscoyier) on CodePen

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>
.padded-multiline { 
  line-height: 1.3; 
  padding: 2px 0; 
  border-left: 20px solid #c0c;
  width: 400px;
  margin: 20px auto;
}
.padded-multiline h1 { 
  background-color: #c0c;
  padding: 4px 0;
  color: #fff; 
  display: inline;
  margin: 0; 
}
.padded-multiline h1 strong { 
  position: relative;
  left: -10px; 
}

The original is in this thread, and my demo:

See the Pen pvBFg by Chris Coyier (@chriscoyier) on CodePen

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.

Here is the original and my stripped down demo:

See the Pen hIvFe by Chris Coyier (@chriscoyier) on CodePen

This is working in Chrome and Safari, but not Firefox now 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).