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.
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:
| Technique | Corners | Gradient border | Inside the card |
|---|---|---|---|
border-image | square | visible | card fill |
| Two backgrounds, solid fill | rounded | visible | card fill |
| Two backgrounds, transparent fill | rounded | visible | gradient everywhere |
::before behind the card, on the page | rounded | visible | card fill |
::before behind the card, inside a panel | rounded | invisible | card fill |
Mask on ::before | rounded | visible | transparent (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?
How do I animate a gradient border?
What is the best method for gradient borders?
Use the Gradient Border Generator โ free, no signup required.
โก Open Gradient Border Generator- [1]W3C, CSS Masking Module Level 1: mask-composite โ
- [2]MDN browser-compat-data, css.properties.mask-composite, checked Oct 13, 2026 โ
- [3]W3C, CSS Backgrounds and Borders Level 3: border images and background-clip โ
- [4]W3C, CSS 2.2 Appendix E: elaborate description of stacking contexts (painting order) โ
- [5]W3C, CSS Custom Properties Level 1 (custom properties animate discretely) โ
- [6]W3C, CSS Properties and Values API Level 1 (@property) โ