Decorating lines of text with box-decoration-break
Publikováno: 8.5.2018
An institution’s motto, an artist’s intro, a company’s tagline, a community’s principle, a service’s greeting… all of them have one thing in common: they’re one brief paragraph displayed on a website’s home page — or at least the about page!
It’s rare that just one word or one line of text welcomes you to a website. So, let’s look at some interesting ways we could style the lines of a paragraph.
To see how things currently are, let’s try giving …
The post Decorating lines of text with box-decoration-break appeared first on CSS-Tricks.
An institution’s motto, an artist’s intro, a company’s tagline, a community’s principle, a service’s greeting… all of them have one thing in common: they’re one brief paragraph displayed on a website’s home page — or at least the about page!
It’s rare that just one word or one line of text welcomes you to a website. So, let’s look at some interesting ways we could style the lines of a paragraph.
To see how things currently are, let’s try giving borders to all the lines of some text in an inline span
and see how it looks:
<p><span>Hummingbirds are birds from...</span></p>
span {
border: 2px solid;
}
See the Pen Broken inline box. by Preethi (@rpsthecoder) on CodePen.
The edges appear broken, which they technically are, as the inline box has been fragmented into multiple lines. But we can fix those broken edges with box-decoration-break
!
The box-decoration-break
property in CSS can decorate the edges of fragments of a broken inline box (as well as of a page, column, and region boxes).
Its value, clone
, does that with the same design that appears in the box’s unbroken edges, and its default value, slice
, does not copy the decorations at the edges, keeping the break very visible like you saw in the demo above.
Let’s try it:
span {
border: 2px solid;
box-decoration-break: clone;
}
See the Pen Broken inline box w/ box-decoration-break. by Preethi (@rpsthecoder) on CodePen.
The property affects not only the border but also the shadow, spacing, and background of the broken edges.
Let’s play with the background first. While writing the post on knockout text, I was working with the background-clip
property and wanted to see if the design held up for multiple lines of text. It didn’t.
The background gradient I applied was not replicated in every line, and after clipping it, only the first one was left with a background. That is, unless box-decoration-break: clone
is added:
<p><span>Singapore:<br>Lion City</span></p>
span {
background-image: linear-gradient(135deg, yellow, violet);
background-clip: text;
color: transparent;
padding: .5em;
box-decoration-break: clone;
}
See the Pen Gradient multi-line text w/box-decoration-break. by Preethi (@rpsthecoder) on CodePen.
The background-clip
property with the text
value clips a background to the shape of its foreground text. Since we used box-decoration-break
, the gradient background is shown and clipped uniformly across all the lines of the text.
Going back to the border, let’s see how its shape and shadow can be copied across the broken edges, along with padding:
<img src="tree.png">
<p><span>Supertrees are tree-like structures...</span></p>
<img src="tree.png">
<p><span>Supertrees are tree-like structures...</span></p>
span {
background: rgb(230,157,231);
border-radius: 50% 0%;
box-shadow: 0 0 6px rgb(41,185,82), 0 0 3px beige inset;
padding: .5em 1.3em;
box-decoration-break: clone;
}
p:nth-of-type(2) span {
background-clip: content-box;
}
See the Pen Inline border shape & shadow w/box-decoration-break. by Preethi (@rpsthecoder) on CodePen.
In the second paragraph of the demo, the background is cropped until the content box (background-clip: content-box
). As you can see, the crop happens in the broken edges as well, because of box-decoration-break: clone
.
Another way we can style borders is with images. You might see a gradient border around the lines of text below, covering the broken edges, if the browser you’re now using supports border-image
and the application of box-decoration-break
over its result.
<p><span>The Malaysia–Singapore Second Link...</span></p>
span {
border: 2px solid;
border-image: linear-gradient(45deg, #eeb075, #2d4944) 1;
background: #eef6f3;
padding: .5em 1.3em;
box-decoration-break: clone;
}
See the Pen Inline border image w/ box-decoration-break. by Preethi (@rpsthecoder) on CodePen.
An additional behavior we can tap into for decorating individual lines is of outline's. In supported browsers, box-decoration-break
can add an outline to every line of the text, including the broken edges, which is useful for creating bicolored dashed borders.
<p><span>Cloud Forest replicates...</span></p>
span {
outline: 2px dashed rgb(216,255,248);
box-shadow: 0 0 0 2px rgb(39,144,198);
background: #fffede;
padding: .5em 1.3em;
animation: 1s animateBorder ease infinite;
box-decoration-break: clone;
}
@keyframes animateBorder{
to{
outline-color: rgb(39,144,198);
box-shadow: 0 0 0 2px rgb(216,255,248);
}
}
See the Pen Inline outline w/ box-decoration-break. by Preethi (@rpsthecoder) on CodePen.
As observed in the demo, box-decoration-break
withstands animation.
Besides borders and backgrounds, box-decoration-break
can also manage shapes applied over elements. There is not much use for it in inline boxes, and is maybe better used in a column or page box, although the application is not yet widely supported in browsers.
But to show an example of what that does, let’s try applying the clip-path
property to the span
.
The property clip-path
itself is only fully supported by Firefox, so only in it you might see an expected outcome. But following are two images: the results of applying a circular clip path over the span
, without and with box-decoration-break
.
span {
clip-path: circle(50% at 202.1165px 69.5px);
...
}
span {
clip-path: circle(50% at 202.1165px 69.5px);
box-decoration-break: clone;
...
}
You’ll notice in the first image that the 50%
radius value is derived from the width of the inline box (the longest line) where box-decoration-break
is not used.
The second image shows how box-decoration-break: clone
redefines the computed value for 50%
by basing them on the widths of the individual lines while keeping the center same as before.
And here’s how the inset
function of clip-path
(an inset rectangle) applied over the span
clips it without and with box-decoration-break
:
span {
clip-path: inset(0);
...
}
span {
clip-path: inset(0);
box-decoration-break: clone;
...
}
Without box-decoration-break
, only a portion of the first line that matches the length of the shortest is visible, and with box-decoration-break: clone
, the first line is fully visible while the rest of the box is clipped.
So, maybe if you ever want to show only the first line and hide the rest, this can come in handy. But, as I mentioned before, this application is more suitable for other types of boxes than ones that are inline. Either way, I wanted to show you how it works.
Wrapping Up
The box-decoration-break: clone
copies any border, spatial, and background designs applied on a fragmented inline box’s unbroken edges to its broken ones. This creates an even design across all the lines of the text, decorating them uniformly and can be super useful for all those blurbs of text that we commonly use on websites.
The post Decorating lines of text with box-decoration-break appeared first on CSS-Tricks.