It is not possible to create rounded gradient borders directly with the border property, so we have to resort to a few tricks.
There are generally three ways of doing it:
- Using a wrapper element
- Using
background-clip - Using a pseudo-element with a
mask
The first one gives you an understanding of what’s going on. The second and third ones are generally what I use on a day-to-day basis.
Using a wrapper element
The idea here is simple.
- First, you have the
gradienton theouterelement. - The outer
elementshould have somepadding(that acts as theborder). - Then you need a
backgroundon the inner element
<!-- Here's a simple Tailwind version of this at work. --><div class="bg-linear-to-br from-purple-300 to-orange-500 p-[2px]"> <div class="bg-purple-50"></div></div>/* Or CSS, if that's what you prefer */.outer { background-image: linear-gradient( to bottom right, var(--color-purple-300), var(--color-orange-300) );}
.inner { background: white;}Using background-clip
background-clip uses the same concept as above on the same element.
The difference is we’re going to use padding-box for the inner background and border-box for the outer background. This way, we can still use padding to create whitespace for the “inner element”.
.card { padding: 1rem; border: 2px solid transparent; background-image: linear-gradient(to bottom, white, white), linear-gradient( to bottom right, var(--color-purple-300), var(--color-orange-300) ); background-origin: padding-box, border-box; background-clip: padding-box, border-box;}Some content here
The background-clip method works for most cases — and can be extended to include Temani Afif’s highly advanced method of generating tooltip arrows to achieve the following effect.
Top arrow
Linear gradient
Right arrow
Radial gradient
Bottom arrow
Conic Gradient
Left arrow
Radial Gradient
The code to create everything here is way too complicated to show, but the idea here isn’t to inundate you with code. It is to show you that once you have grasped an idea, you can put it into a utility (or a class), and you can use it easily from that point onwards.
I managed to put this into a bezel utility. You can find it in Splendid Effects.
When using bezel, you just need to define:
- The inner background color or inner gradient.
- The outer gradient.
And if you want an arrow, you use the bezel-arrow-*.
.card { /* Use both bezel-arrow and arrow-left utilities */ @apply bezel-arrow-left; /* Use radial gradient for both inner and outer gradients */ @apply inner-radial; @apply outer-radial; --bg-color: var(--color-neutral-950); /* Base card color */ --border-width: 2px;
/* Defining the inner and outer gradients */ --gradient-shape: ellipse; --gradient-position: 0 50%; --inner-size: 120% 120%; --outer-size: 140% 140%; --inner-stops: rgb(34 197 94 / 0.3), transparent 100%; --outer-stops: rgb(34 197 94 / 0.95), transparent 100%;
/* Defining the arrow position */ --arrow-position: 50%;}Right arrow
Radial gradient
Using a pseudo element
The background-clip method works fabulously, but it does not allow you to create an inner background that is transparent.
If you want the inner element to be translucent (to add a frosted-glass effect or simply let transparency shine through), you need to put the outer gradient on a pseudo-element. Then you apply a mask so the pseudo-element only colours the gradient.
I know this sounds really complex, but the whole idea has been captured in the paragraph above. If you want a step-by-step tutorial, Kevin Powell has one, and I highly recommend you check it out!
The long story short, the utility can look something like this.
@utility bezel-frost { --frost: 16px; position: relative; border: var(--border-width) var(--border-style) transparent; border-radius: var(--radius); backdrop-filter: blur(var(--frost));
&::before { content: ''; pointer-events: none; position: absolute; inset: calc(-1 * var(--b)); border-width: var(--border-width); border-radius: inherit; background: var(--outer-gradient, none); mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0); mask-composite: exclude; }}Then we can use bezel-frost to build a gradient frame with a frosted glass content area!
.card { /* Use the utility */ @apply bezel-frost;
/* Define the gradients */ @apply gradient; @apply outer-linear; --gradient-angle: to bottom right; --gradient: oklch(100% 0.3 250deg / 50%), transparent 100%; --outer-stops: oklch(100% 0.3 250deg / 80%), transparent 100%;}You have to be mindful about contrast when doing this!
That’s it for this lesson!
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson