Gradient Borders in CSS, Rounded and Animated
Updated 2026-10-28. 5 borders.
CSS has no gradient value for border-color, and border-image ignores border-radius. To get a rounded gradient border, paint two backgrounds: a solid fill clipped to the padding box over a gradient clipped to the border box, with a transparent border. For a hollow outline, use a masked pseudo-element instead. Five working versions follow.
Use the padding-box trick when the inside of the box has a solid colour, such as a card on a plain page. It is one element, one background declaration and works everywhere. When the inside must stay see-through, over a photo or a patterned section, use the mask-composite outline, which cuts the middle out of a gradient layer.
The rotating conic border draws the eye, so save it for one featured card or a plan you want people to pick. The focus ring version applies the same idea to form fields, showing the gradient only while the field is active. The outline buttons bring a gradient edge to call-to-action buttons with a clear background.
For Tailwind, the padding-box trick translates to arbitrary background values on one element, or to a wrapper with a gradient background, one or two pixels of padding and an inner element with a solid fill and a slightly smaller radius.
Compare them
| Variant | Built with | Size (gzipped) | Running cost | Browser support |
|---|---|---|---|---|
| Gradient Border CSS Rounded Cards With Padding Box Trick | CSS only | 1 KB | One extra background layer per card, painted once; no animation or script. | All current browsers |
| Gradient Border CSS Input Focus Ring | CSS only | 1.1 KB | Background swaps on one wrapper per field when focus moves, plus a short shadow transition. | All current browsers |
| Gradient Border CSS Mask Composite Transparent Outline | CSS only | 0.9 KB | One masked pseudo-element per outlined box; the mask is composited once and then cached. | All current browsers (WebKit prefix included) |
| Gradient Border CSS Outline Buttons With Gradient Text | CSS only | 0.9 KB | A masked ring and text clipped to a gradient; hover transitions the ring's padding on one element. | All current browsers |
| Gradient Border CSS Rotating Conic Animated Border | CSS only | 1.2 KB | One oversized layer rotated by a transform keyframe; the GPU composites it without repainting. | All current browsers |
Gradient Border CSS Rounded Cards With Padding Box Trick
Three course cards for a pottery studio, each framed by a two pixel gradient border: orange to pink at a 12px radius, teal to indigo at 24px and yellow to green at 36px. The cards prove the point of the technique: the gradient follows any corner radius, which border-image cannot do. On phones the cards stack into one column with the same borders.
<section class="gb1">
<h2 class="gb1-title">Courses starting in March</h2>
<ul class="gb1-grid">
<li class="gb1-card" style="--gb1-grad: linear-gradient(135deg, #f97316, #ec4899); --gb1-radius: 12px">
<p class="gb1-meta">6 weeks, Mondays</p>
<h3>Glaze chemistry</h3>
<p>Mix and test your own stoneware glazes, from celadon to tenmoku.</p>
</li>
<li class="gb1-card" style="--gb1-grad: linear-gradient(135deg, #14b8a6, #6366f1); --gb1-radius: 24px">
<p class="gb1-meta">4 weeks, Wednesdays</p>
<h3>Throwing large forms</h3>
<p>Centre ten kilos of clay and pull walls for planters and jars.</p>
</li>
<li class="gb1-card" style="--gb1-grad: linear-gradient(135deg, #eab308, #16a34a); --gb1-radius: 36px">
<p class="gb1-meta">1 day, Saturday</p>
<h3>Raku firing day</h3>
<p>Pull glowing pots from the kiln and smoke them in sawdust.</p>
</li>
</ul>
</section>
<style>
.gb1 { background: #f5f5f4; padding: 64px 20px; font-family: ui-sans-serif, system-ui, sans-serif; }
.gb1-title { margin: 0 0 40px; text-align: center; font-size: 24px; color: #1c1917; }
.gb1-grid { display: grid; gap: 20px; max-width: 64rem; margin: 0 auto; padding: 0; list-style: none; }
@media (min-width: 640px) { .gb1-grid { grid-template-columns: repeat(3, 1fr); } }
/* THE TRICK
border: transparent, so the backgrounds show through it.
Layer 1: a solid colour, clipped to the padding box (everything inside the border).
Layer 2: the gradient, clipped to the border box (reaches the outer edge).
Only the border ring shows layer 2, and border-radius rounds both. */
.gb1-card {
--gb1-fill: #fff; /* must be a solid colour: it hides the gradient behind it */
--gb1-width: 2px; /* border thickness */
padding: 24px;
border: var(--gb1-width) solid transparent;
border-radius: var(--gb1-radius, 16px);
background:
linear-gradient(var(--gb1-fill), var(--gb1-fill)) padding-box,
var(--gb1-grad) border-box;
}
.gb1-meta { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: #78716c; }
.gb1-card h3 { margin: 8px 0; font-size: 18px; color: #1c1917; }
.gb1-card p:last-child { margin: 0; font-size: 14px; line-height: 1.6; color: #57534e; }
</style>
With reduced motion: Nothing on these cards moves, so the gradient borders look exactly the same.
Running cost: One extra background layer per card, painted once; no animation or script.
- Set `--gb1-grad` on each card to any gradient, including radial or conic ones.
- Change `--gb1-width` from 2px to 1px for a fine hairline or 4px for a bold frame.
- Match `--gb1-fill` to the card colour you want inside; it has to be solid, so for a see-through middle use the mask method instead.
- In Tailwind, write the background as an arbitrary value: `[background:linear-gradient(#fff,#fff)_padding-box,linear-gradient(135deg,#f97316,#ec4899)_border-box]` with `border-2 border-transparent`.
Full page for Gradient Border CSS Rounded Cards With Padding Box Trick
Gradient Border CSS Input Focus Ring
A demo request form on white whose fields sit inside wrappers with a quiet grey border. When a field gets focus, by click or by Tab, its wrapper swaps the grey for an emerald to sky to indigo gradient and gains a soft blue glow beneath. The name, email and team size select all use the same wrapper, so the effect is consistent.
<form class="gb4">
<h2 class="gb4-title">Request a demo</h2>
<label for="gb4-name">Full name</label>
<div class="gb4-field"><input id="gb4-name" type="text" value="Priya Natarajan"></div>
<label for="gb4-email">Work email</label>
<div class="gb4-field"><input id="gb4-email" type="email" placeholder="[email protected]"></div>
<button type="submit" class="gb4-submit">Book my demo</button>
</form>
<style>
.gb4 { --gb4-rest: #e2e8f0; --gb4-hover: #cbd5e1; --gb4-grad: linear-gradient(90deg, #10b981, #0ea5e9, #6366f1); max-width: 28rem; margin: 0 auto; padding: 48px 20px; font-family: ui-sans-serif, system-ui, sans-serif; }
.gb4-title { margin: 0 0 20px; font-size: 20px; color: #0f172a; }
.gb4 label { display: block; margin-top: 16px; font-size: 14px; font-weight: 500; color: #1e293b; }
/* The wrapper draws the border: grey at rest, gradient while the input inside has focus.
A plain colour is written as a one-colour gradient so both states use the same two-layer shape. */
.gb4-field {
margin-top: 6px;
border: 2px solid transparent; border-radius: 12px;
background: linear-gradient(#fff, #fff) padding-box, linear-gradient(var(--gb4-rest), var(--gb4-rest)) border-box;
transition: box-shadow .2s;
}
.gb4-field:hover { background: linear-gradient(#fff, #fff) padding-box, linear-gradient(var(--gb4-hover), var(--gb4-hover)) border-box; }
.gb4-field:focus-within {
background: linear-gradient(#fff, #fff) padding-box, var(--gb4-grad) border-box;
box-shadow: 0 4px 18px -6px rgb(14 165 233 / .45);
}
/* The input itself has no border or outline: the wrapper is the visible focus indicator. */
.gb4-field input { box-sizing: border-box; width: 100%; height: 48px; padding: 0 16px; border: 0; border-radius: 10px; background: transparent; font: inherit; font-size: 14px; color: #0f172a; outline: none; }
.gb4-field input::placeholder { color: #64748b; }
.gb4-submit { width: 100%; height: 48px; margin-top: 24px; border: 0; border-radius: 12px; background: #0f172a; color: #fff; font: 600 14px ui-sans-serif, system-ui, sans-serif; cursor: pointer; }
.gb4-submit:focus-visible { outline: 2px solid #0ea5e9; outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .gb4-field { transition: none; } }
</style>
<script>
/* Demo only: stop the page reloading on submit. Remove this when you wire up your form. */
document.querySelector(".gb4").addEventListener("submit", function (e) { e.preventDefault(); });
</script>
With reduced motion: The soft glow under a focused field appears at once instead of fading in.
Running cost: Background swaps on one wrapper per field when focus moves, plus a short shadow transition.
- Set `--gb4-grad` for the focus colours and `--gb4-rest` for the resting border.
- Remove the `box-shadow` line in the `:focus-within` rule if you want only the border to change.
Gradient Border CSS Mask Composite Transparent Outline
A release note card on a dotted navy grid where only the hairline outline is coloured, sky to lime to pink, and the dots run straight through the middle of the card. Three small stat tiles inside use the same outline at a smaller radius. The gradient lives on a pseudo-element whose centre is removed with two mask layers and mask-composite.
<section class="gb2-bg">
<div class="gb2-ring gb2-card">
<p class="gb2-kicker">Build log</p>
<h2 class="gb2-title">Release 4.2 shipped to the field units</h2>
<p class="gb2-text">The dotted grid behind this card shows straight through its middle. Only the outline carries the gradient.</p>
</div>
</section>
<style>
.gb2-bg { padding: 64px 20px; background-color: #0c1222; background-image: radial-gradient(rgb(148 163 184 / .28) 1px, transparent 1px); background-size: 18px 18px; font-family: ui-sans-serif, system-ui, sans-serif; }
.gb2-card { max-width: 28rem; margin: 0 auto; padding: 28px; border-radius: 20px; }
.gb2-kicker { margin: 0; font: 12px ui-monospace, Menlo, monospace; letter-spacing: .15em; text-transform: uppercase; color: #7dd3fc; }
.gb2-title { margin: 12px 0; font-size: 24px; color: #fff; }
.gb2-text { margin: 0; font-size: 14px; line-height: 1.6; color: #cbd5e1; }
/* HOW IT WORKS
::before covers the box and is painted with the gradient.
Its padding is the border width. Two mask layers: one covering only the content box,
one covering everything. "exclude" (xor in WebKit) keeps where they do NOT overlap:
just the padding ring. The middle stays fully transparent. */
.gb2-ring { position: relative; }
.gb2-ring::before {
content: "";
position: absolute; inset: 0;
padding: 1.5px; /* border width */
border-radius: inherit; /* follows the box's corners */
background: linear-gradient(140deg, #38bdf8, #a3e635 50%, #f472b6);
-webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
-webkit-mask-composite: xor;
mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0);
pointer-events: none; /* clicks pass through to the content */
}
</style>
With reduced motion: The outline is static, so it looks the same whatever the motion setting.
Running cost: One masked pseudo-element per outlined box; the mask is composited once and then cached.
- Change the 1.5px `padding` on `::before` to set the outline width.
- Add the `.gb2-ring` class to any rounded element; it picks up that element's radius through `border-radius: inherit`.
Gradient Border CSS Outline Buttons With Gradient Text
Two see-through buttons for a bird guide on a striped paper background: a magenta to amber pill and a teal to blue rounded button. Both the two pixel outline and the label carry the gradient, and the stripes show through the middle. On hover the outline swells inward until it fills the button and the label turns white.
<div class="gb5-bg">
<h2 class="gb5-title">A pocket guide to 140 coastal birds</h2>
<div class="gb5-row">
<a href="#" class="gb5 gb5-pill" style="--g: linear-gradient(90deg, #db2777, #f59e0b)"><span>Download the field guide</span></a>
<a href="#" class="gb5" style="--g: linear-gradient(90deg, #0d9488, #2563eb)"><span>View sample pages</span></a>
</div>
</div>
<style>
.gb5-bg { padding: 64px 20px; text-align: center; background: repeating-linear-gradient(135deg, #fdf6ec 0 14px, #faefdf 14px 28px); font-family: ui-sans-serif, system-ui, sans-serif; }
.gb5-title { margin: 0 0 32px; font: 400 30px/1.2 ui-serif, Georgia, serif; color: #1c1917; }
.gb5-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 16px; }
/* Outline: a masked ::before ring (see the mask-composite piece), so the button stays see-through. */
.gb5 { position: relative; isolation: isolate; display: inline-flex; align-items: center; min-height: 48px; padding: 0 24px; border-radius: 10px; font-size: 14px; font-weight: 600; text-decoration: none; }
.gb5-pill { border-radius: 999px; }
.gb5::before {
content: ""; position: absolute; inset: 0; z-index: -1;
padding: 2px; border-radius: inherit; background: var(--g);
-webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
-webkit-mask-composite: xor;
mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0);
transition: padding .25s;
}
/* Gradient label: paint the gradient as background and clip it to the text shape. */
.gb5 span { background: var(--g); -webkit-background-clip: text; background-clip: text; color: transparent; transition: color .25s; }
/* Hover: grow the ring's padding until it covers the whole button, and turn the label white. */
.gb5:hover::before { padding: 40px; }
.gb5:hover span { color: #fff; }
.gb5:focus-visible { outline: 2px solid #1c1917; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) { .gb5::before, .gb5 span { transition: none; } }
</style>
With reduced motion: Hovering fills the button and whitens the label in one step, without the ring closing in.
Running cost: A masked ring and text clipped to a gradient; hover transitions the ring's padding on one element.
- Set `--g` on each link to its own gradient; the ring and the label both read it.
- Drop the hover fill by deleting the `:hover::before` rule if you want a pure outline.
Gradient Border CSS Rotating Conic Animated Border
A coffee roastery's most booked tour on a dark espresso card, with a bright amber and rose arc that travels around its edge every four and a half seconds. Behind the card spins a square conic gradient twice its width; the card sits two pixels inside a rounded frame that clips everything else, so only the ring shows the motion.
<section class="gb3-bg">
<div class="gb3">
<div class="gb3-card">
<p class="gb3-kicker">Most booked</p>
<h2 class="gb3-title">Weekend roastery tour</h2>
<p class="gb3-text">Cupping, a roast of your own and a bag to take home.</p>
<p class="gb3-price"><b>£48</b> per person</p>
<a href="#" class="gb3-cta">Choose a Saturday</a>
</div>
</div>
</section>
<style>
.gb3-bg { padding: 64px 20px; background: #1a1410; font-family: ui-sans-serif, system-ui, sans-serif; }
/* The frame: rounded, clips its children, 2px padding = border width. */
.gb3 { position: relative; max-width: 24rem; margin: 0 auto; padding: 2px; border-radius: 22px; overflow: hidden; isolation: isolate; background: rgb(69 26 3 / .4); }
/* A square twice as wide as the card, painted with a conic gradient that is mostly transparent,
spinning behind the card. Only the 2px ring around the card shows it, so a bright arc runs round the edge.
Rotating with a transform is cheap; the gradient itself is never repainted. */
.gb3::before {
content: "";
position: absolute; z-index: -1; left: 50%; top: 50%;
width: 200%; aspect-ratio: 1;
transform: translate(-50%, -50%) rotate(0deg);
background: conic-gradient(from 0deg, transparent 0 55%, #fbbf24 70%, #fb7185 82%, transparent 92%);
animation: gb3-spin 4.5s linear infinite;
}
@keyframes gb3-spin { to { transform: translate(-50%, -50%) rotate(360deg); } }
/* The card: radius is the frame radius minus the 2px border. */
.gb3-card { padding: 28px; border-radius: 20px; background: #211913; }
.gb3-kicker { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: .2em; text-transform: uppercase; color: #fcd34d; }
.gb3-title { margin: 8px 0 4px; font-size: 20px; color: #fffbeb; }
.gb3-text { margin: 0; font-size: 14px; color: #d6d3d1; }
.gb3-price { margin: 24px 0; font-size: 14px; color: #d6d3d1; }
.gb3-price b { font-size: 36px; font-weight: 600; color: #fffbeb; }
.gb3-cta { display: flex; align-items: center; justify-content: center; min-height: 46px; border-radius: 12px; background: #fbbf24; color: #0c0a09; font-size: 14px; font-weight: 600; text-decoration: none; }
.gb3-cta:hover { background: #fcd34d; }
.gb3-cta:focus-visible { outline: 2px solid #fde68a; outline-offset: 3px; }
/* Reduced motion: a still two-colour border instead of the moving arc. */
@media (prefers-reduced-motion: reduce) {
.gb3::before { animation: none; background: linear-gradient(135deg, #fbbf24, #fb7185); }
}
</style>
With reduced motion: The arc stops and the border becomes a still amber to rose gradient all the way round.
Running cost: One oversized layer rotated by a transform keyframe; the GPU composites it without repainting.
- Change the stops in the `conic-gradient` to set the arc colours and its length; widen the transparent part for a shorter comet.
- Slow or speed the loop by editing `4.5s` in the animation.
Performance notes
Static gradient borders cost one extra background layer and nothing after the first paint. Mask-composite adds a little compositing work but no script. Both are safe to use on many cards in a grid.
The rotating border spins an oversized conic layer with a transform, which stays on the compositor, instead of animating an @property angle that repaints the gradient each frame. It still keeps a layer alive, so use one or two per page, and it stops for reduced motion.
Questions
Why doesn't border-radius work with border-image?
The specification draws border-image over the border area in straight pieces and ignores the radius, so corners come out square. Use the background-clip method or a mask instead; both follow border-radius.
How do I make a gradient border with a transparent inside?
Put the gradient on a ::before pseudo-element with the same radius and a padding equal to the border width, then mask it with two linear-gradient layers and mask-composite: exclude (with -webkit-mask-composite: xor for older Safari). The middle is removed and whatever sits behind shows through.
How do I animate a gradient border?
Either rotate an oversized conic-gradient layer behind a slightly smaller inner box, or register an angle with @property and animate it inside the conic-gradient. Rotation is cheaper; the @property route keeps the gradient shape fixed while colours travel around the edge.
How do I add a gradient border in Tailwind?
Wrap the content in a div with bg-gradient-to-r from-x to-y, p-px or p-0.5 and a rounded class, then give the inner element a solid background and a radius about 1 to 2 pixels smaller. That gives a rounded gradient edge with no custom CSS.
Also in the gallery: CSS Gradient Border: 12 Techniques with Rounded Corners and Glow, Glass Tube Border Feature Card in WebGL with a Warm Bead of Light That Runs the Edge and Warms the Sunlit Table.
More collections: HTML and CSS Buttons: 8 Styles to Copy, Dark Mode Toggle Switches in CSS and React, Animated Gradient Backgrounds. All borders and glows. Build your site free to have a full site built around them.
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App