CSSMar 18, 2026 ยท Updated Oct 13, 2026 ยท 4 min read

How to Create a CSS Gradient Border

Four ways to put a gradient on a border, tested in Chromium: which keep rounded corners, which can be transparent inside, and the popular z-index: -1 version that vanishes inside any container with a background.

๐Ÿ”ฒ
Try the Gradient Border Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Four Techniques, Tested02border-image: Simple, Square Corners Only03Two Backgrounds: Rounded, Needs a Solid Fill04The Mask Method: Rounded and See-Through05The z-index: -1 Trap06Animating the Gradient

Four Techniques, Tested

CSS has no border-color value for gradients, so every gradient border is a workaround. We rendered the common ones in Chromium on October 13, 2026, a 200 by 100 px card with a 6 px border and 12 px radius, and read the pixels at the corner, on the border and in the middle:

TechniqueCornersGradient borderInside the card
border-imagesquarevisiblecard fill
Two backgrounds, solid fillroundedvisiblecard fill
Two backgrounds, transparent fillroundedvisiblegradient everywhere
::before behind the card, on the pageroundedvisiblecard fill
::before behind the card, inside a panelroundedinvisiblecard fill
Mask on ::beforeroundedvisibletransparent (panel shows through)

Two techniques pass everything you would expect from a border: the two-background method when the card has a solid fill, and the mask method when it needs to be transparent or sit on varying backgrounds. The other rows fail in ways that are easy to miss in a quick demo.

border-image: Simple, Square Corners Only

.card {
  border: 6px solid transparent;
  border-image: linear-gradient(90deg, #ff6b6b, #ffa62e) 1;
}

One declaration and it works everywhere, but the border image ignores border-radius: in our test the corner pixel was gradient, not page background. Use it for square boxes, dividers and table rules.

Two Backgrounds: Rounded, Needs a Solid Fill

.card {
  border: 6px solid transparent;
  border-radius: 12px;
  background:
    linear-gradient(#1a1d2e, #1a1d2e) padding-box,
    linear-gradient(90deg, #ff6b6b, #ffa62e) border-box;
}

The first background covers the inside (padding-box), the second fills everything including the transparent border (border-box). It follows the radius and needs no extra element. The catch is in the third row of the table: make the first layer transparent and the gradient behind it shows through the whole card. This method only works when the inside is a solid color.

The Mask Method: Rounded and See-Through

.card {
  position: relative;
  border-radius: 12px;
}
.card::before {
  content: "";
  position: absolute;
  inset: 0;
  padding: 6px; /* border width */
  border-radius: inherit;
  background: linear-gradient(90deg, #ff6b6b, #ffa62e);
  -webkit-mask:
    linear-gradient(#fff 0 0) content-box,
    linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  pointer-events: none;
}

The pseudo-element is filled with the gradient, then masked twice: once over its content box and once over everything. mask-composite: exclude keeps only the area covered by one mask and not the other, which is the padding ring. The inside stays transparent, so the card works on any background. MDN's compatibility data lists mask-composite in Chrome and Edge 120, Firefox 53 and Safari 15.4; the -webkit-mask-composite: xor line covers older WebKit browsers. pointer-events: none matters: without it the overlay sits on top of the card's links and buttons and swallows their clicks. Our Gradient Border Generator writes exactly this CSS.

The z-index: -1 Trap

A popular shortcut puts a slightly larger gradient ::before behind the card with z-index: -1. On a bare page it works. Put the card inside any section with its own background and the border disappears: in our test the border pixels came back as the panel color. A negative z-index paints the pseudo-element below the backgrounds of ordinary blocks in the same stacking context, so the panel covers it. Use the mask method instead; it does not depend on what is behind the card.

Animating the Gradient

@property --angle {
  syntax: '<angle>';
  initial-value: 0deg;
  inherits: false;
}
.card::before {
  background: linear-gradient(var(--angle), #ff6b6b, #ffa62e, #ff6b6b);
  animation: spin-border 3s linear infinite;
}
@keyframes spin-border { to { --angle: 360deg; } }

Registering --angle with @property is what makes it animatable. An unregistered custom property animates discretely, so without the registration the gradient jumps between start and end instead of turning. Keep it slow, and put it inside @media (prefers-reduced-motion: no-preference) so people who turn motion off do not get a spinning border.

Frequently Asked Questions

Can I use border-radius with gradient borders?+
Not with border-image: in our Chromium test the corners stayed square. Use the two-background method (a solid fill on padding-box over a gradient on border-box) when the card has a solid inside, or the mask method on a ::before pseudo-element when the inside must be transparent.
How do I animate a gradient border?+
Register a custom property with @property (syntax <angle>), use it as the gradient angle and animate it with @keyframes. Without the registration the custom property animates discretely, so the gradient jumps instead of rotating. Wrap the animation in a prefers-reduced-motion: no-preference media query.
What is the best method for gradient borders?+
The mask method when the inside must be transparent or the card sits on different backgrounds; the two-background method when the card has a solid fill and you want no extra pseudo-element. Avoid the z-index: -1 shortcut: in our test it disappeared inside a container with a background.
Try it yourself

Use the Gradient Border Generator โ€” free, no signup required.

โšก Open Gradient Border Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.