Hi, I'm Liam

I talk about code and stuff

CSS box-decoration-break: clone - Styling Inline Elements That Line Break

Published on

Today I learned about the CSS property box-decoration-break: clone.

I found myself wanting to style some inline text on a page to highlight it with a background for emphasis, but I noticed that when the text wraps onto a new line, the styles for the inline element just kind of… cut off abruptly.

Take a look for yourself with this example, see how the beginning and ends of lines don’t follow the same padding and border-radius as the beginning and end of the full highlighted text:

Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptatevelit esse cillum dolore eu fugiat nulla pariatur.

There’s no great way to apply separate styles to the line break spots, but I just found out about the box-decoration-break property, which you can apply to an inline element with the clone value, ensuring the complete styles are cloned for each instance of text regardless of line breaks, making the text look a bit more consistent:

Now take a look with box-decoration-break: clone applied:

Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptatevelit esse cillum dolore eu fugiat nulla pariatur.

box-decoration-break: clone has pretty great browser support (with prefixes), and there are more examples on MDN to see how it’s useful. TailwindCSS also has a utility class for this, .box-decoration-clone.

Photo of Liam Hammett
written by
Liam Hammett
Found a typo? Suggest a fix here!
Hit me up on Twitter / GitHub / LinkedIn / email me
Copyright © 2024 Liam Hammett and all that kind of stuff