This note has an interactive demo. Try it in the original
A gradient border with rounded corners has always needed a trick, and a dashed gradient border has been close to impossible in plain CSS. background-clip: border-area fixes both: it tells the browser to paint a background only where the border is actually drawn. Safari has had it since 18.2, and Chrome 150 shipped it this summer, so it now works in Chromium browsers and Safari.
The problem it solves
There are two classic ways to get a gradient border, and both have a catch.
The first is border-image. It accepts a gradient, but MDN is blunt about it: border-radius "has no effect on the border image". If you want rounded corners, MDN's own advice is to "draw it as the background instead".
That leads to the second trick, which I've copied into more projects than I can count. You stack two backgrounds: a solid one clipped to the padding box (the inside of the card) and the gradient clipped to the border box (the whole thing), and you make the border transparent so the gradient shows through:
.card {
border: 8px solid transparent;
border-radius: 28px;
background-image:
linear-gradient(#141B2D 0 0),
conic-gradient(#FF5200, #00D4AA, #FF7E41, #FF5200);
background-origin: border-box;
background-clip: padding-box, border-box;
}
It works for solid borders. But the gradient covers the whole border box, so the border's own shape no longer matters. Switch the style to dashed and you still get a solid ring of gradient, because the gaps between the dashes are painted too. That's the right-hand card in the demo above: pick dashed, dotted or double and watch it ignore you.
What border-area does
The CSS Backgrounds Level 4 spec defines the new value like this: "The background is clipped to the area painted by the border, taking border-width and border-style into account but ignoring any transparency introduced by border-color."
That last part is the clever bit. You set border-color: transparent so the border itself is invisible, but the browser still knows where a dash, a dot or a double line would be painted, and clips the background to exactly that shape. The code changes by one keyword:
.card {
border: 8px dashed transparent;
border-radius: 28px;
/* layer 1: the inside; layer 2: the border */
background-image:
linear-gradient(#141B2D 0 0),
conic-gradient(from var(--angle), #FF5200, #00D4AA, #FF7E41, #7C5CFF, #FF5200);
background-origin: border-box;
background-clip: padding-box, border-area;
}
This is the CSS behind the left-hand card in the demo, and the panel underneath rewrites it live as you change the style, width, radius or gradient. With dashed, each dash gets its own slice of the conic gradient and the gaps stay empty. Rounded corners just work, because the radius shapes the border, and the border shapes the background.
The --angle in the gradient is a registered custom property (@property --angle { syntax: "<angle>"; inherits: true; initial-value: 0deg; }), which is what lets a keyframe animate it and makes the colors travel around the border. That part has nothing to do with border-area, but the two make a nice pair.
Since it's a background, anything a background can do works on the border: gradients, repeating stripes (the third option in the demo) or an image. The WebKit team's article goes one step further and combines it with text clipping on a button, with background-clip: border-area, text: one gradient layer paints the border and another one paints the label.
Browser support and gotchas
-
Support today: Safari 18.2 (December 2024) and Chrome 150 (June 2026). Firefox doesn't have it yet: the Bugzilla ticket for
background-clip: border-areais still open. So this is not Baseline, and it needs a fallback. -
Detect it: in CSS with
@supports (background-clip: border-area); in JavaScript withCSS.supports('background-clip', 'border-area'). The WebKit article suggests a solid-color border as the fallback, and that's what the demo does in Firefox: it shows a warning and a single-color border that keeps its style. Another reasonable plan B, if your border is solid, is the two-layer trick above, which works everywhere. -
Don't forget
background-origin: border-box. With the defaultpadding-boxorigin the background starts inside the border and repeats underneath it, which the WebKit article calls "unexpected buggy-looking results". -
Careful with the
backgroundshorthand. It resetsbackground-originback to its default, so if you setbackgroundafter the longhands, you lose the fix above. I use longhands for all of it. -
The bottom layer decides
background-color. The spec says the background color is clipped according to thebackground-clipof the bottom-most layer. In the code above that's the gradient withborder-area, so abackground-colorwould only show on the border. Paint the inside with its own layer, as the demo does.
What to keep an eye on
Firefox is the missing piece. Once it ships, this becomes the default way to draw gradient borders and the padding-box/border-box trick can retire. Until then, I'd use border-area behind @supports for dashed, dotted or double gradient borders, where there was no good answer before, and keep the old trick for the solid ones.
Sources
- background-clip (MDN)
- CSS Backgrounds Level 4: background-clip (W3C Editor's Draft)
- Make creative borders with background-clip: border-area (WebKit blog)
- New in Chrome 150 (Chrome for Developers)
- border-image (MDN)
Originally published at richi.codes.
Top comments (0)