More CSS answers

by Danny
Published 2 min read

CSS has no border-gradient property, but you can get one with border-image, with layered backgrounds that keep border-radius, or with a mask for a transparent centre.

The short answer

#

There is no property that takes a gradient as a border colour, because border-color only accepts colours. The most useful workaround layers two backgrounds: a solid one clipped to the padding box and a gradient one that shows through a transparent border. It works with border-radius.


_10
.box {
_10
border: 3px solid transparent;
_10
border-radius: 12px;
_10
background:
_10
linear-gradient(#fff, #fff) padding-box,
_10
linear-gradient(135deg, #6366f1, #ec4899) border-box;
_10
}

Why it happens

#

A gradient in CSS is an image, not a colour. Properties such as border-color and color reject images, so border-color: linear-gradient(...) is invalid and the browser drops the whole declaration.

That leaves the properties that do accept images:

  • border-image paints an image into the border area, but it ignores border-radius, so the corners are always square.
  • background can draw several images, each clipped to a different box. Clipping a gradient to the border box and a solid colour to the padding box makes the gradient visible only where the border is.
  • mask can cut the middle out of a gradient, which gives a gradient border with a see-through centre.

How to fix it

#

Layered backgrounds (most cases)

This is the version in the short answer. The border is transparent, so the gradient underneath shows through it, and the first, solid layer covers the rest. The first value in the background list is drawn on top.

Use it when the element has a solid background colour. To match the page in light and dark themes, use a custom property for the inner colour.


_10
.card {
_10
--card-bg: #ffffff;
_10
border: 2px solid transparent;
_10
border-radius: 1rem;
_10
background:
_10
linear-gradient(var(--card-bg), var(--card-bg)) padding-box,
_10
conic-gradient(from 90deg, #22d3ee, #a855f7, #22d3ee) border-box;
_10
}

The inner layer must be a gradient, not a plain colour, because only the last layer in a background list may be a colour.

border-image (square corners only)

If you do not need rounded corners, border-image is the shortest option. The 1 at the end tells the browser to use the whole gradient as one slice.


_10
.divider {
_10
border: 4px solid;
_10
border-image: linear-gradient(90deg, #f59e0b, #ef4444) 1;
_10
}

You can also show it on one side only, for example with border-width: 0 0 4px for an underline.

Mask (transparent centre)

When the element sits on a photo or a pattern, a solid inner layer would hide what is behind it. Put the gradient on a pseudo-element and mask out its content box, so only the padding ring remains.


_18
.outline {
_18
position: relative;
_18
border-radius: 12px;
_18
}
_18
_18
.outline::before {
_18
content: "";
_18
position: absolute;
_18
inset: 0;
_18
padding: 2px; /* border thickness */
_18
border-radius: inherit;
_18
background: linear-gradient(135deg, #6366f1, #ec4899);
_18
mask:
_18
linear-gradient(#000 0 0) content-box,
_18
linear-gradient(#000 0 0);
_18
mask-composite: exclude;
_18
pointer-events: none;
_18
}

The two mask layers cover the content box and the whole box. exclude keeps only the area where they do not overlap, which is the padding ring. Unprefixed mask and mask-composite work in all current major browsers; very old WebKit versions needed -webkit-mask with -webkit-mask-composite: xor.

Consider a solid ring instead

If a gradient is not essential, a solid outline or box-shadow ring is cheaper and simpler, and outline also follows border-radius in current browsers.

Common pitfalls

#
  • Writing border-color: linear-gradient(...). It is invalid, so nothing happens and no error appears outside DevTools.
  • Using border-image and adding border-radius. The radius is ignored and the corners stay square.
  • Forgetting border: ... transparent in the layered method. With a coloured border, the gradient underneath is hidden.
  • Putting a plain colour as the first background layer. Only the bottom layer can be a colour, so wrap it as linear-gradient(colour, colour).
  • The pseudo-element in the mask method sitting on top of links and buttons. Keep pointer-events: none on it so clicks reach the content underneath.
#

Was this resource helpful?