---
title: "How do I add a gradient border in CSS?"
description: "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."
url: https://www.danny.engineering/answer/css/how-do-i-add-a-gradient-border-in-css
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["CSS"]
---

# How do I add a gradient border in CSS?

## 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`.


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


## 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.


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


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.


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


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.


```css
.outline {
  position: relative;
  border-radius: 12px;
}

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


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.

## Related reading

- [MDN: background-clip](https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip)
- [MDN: border-image](https://developer.mozilla.org/en-US/docs/Web/CSS/border-image)
- [MDN: mask-composite](https://developer.mozilla.org/en-US/docs/Web/CSS/mask-composite)
