Tailwind’s linear gradients work, but there are a few things I do not like about them:
- Linear gradients:
- I don’t like to be limited to three colours
- Adding colour stops bloats up the HTML with lots of utilities
- The experience of creating a gradient is not that great
- Radial gradients:
- It requires a person to have to almost memorize the radial gradient syntax by heart, which is very difficult to do.
So I went ahead and created a few gradient utilities that are much easier to use.
Linear Gradient
The most common gradient is a linear gradient, so I call it gradient. I also have a linear-gradient version that does the same thing.
@utility gradient { background-image: linear-gradient( var(--gradient-angle) in var(--color-space) var(--hue-interpolation), var(--gradient) );}At a glance, you can see how this can work. There are only 4 variables here and they’re quite straightforward:
--gradient: Where we define the actual gradient--gradient-angle: The gradient’s angle--color-space: The color space (defaults tooklch)--hue-interpolation: Changes in hue interpolation if you want —longer-hueorshorter-hue—shorter-hueby default.
Then all I have to do is:
- Set the gradient’s angle.
- Set the gradient (where I can just pick colors).
.selector { @apply gradient; --gradient: var(--color-blue-500), var(--color-pink-500); --gradient-angle: 120deg;}You can use any number of colours — so go crazy if you want!
.selector { @apply gradient; --gradient: var(--color-blue-500), var(--color-pink-500), var(--color-red-500), var(--color-blue-500), var(--color-orange-500); --gradient-angle: 120deg;}Adding colour stops is also easy with this approach because we can just add them directly next to each other.
.selector { @apply gradient; --gradient: var(--color-blue-500) 20%, var(--color-pink-500) 75%, var(--color-red-500); --gradient-angle: 120deg;}These can work directly in the HTML too. Just set --gradient through an inline style and you don’t have to deal with Tailwind’s underscores_to_denote_spaces syntax.
<div class="gradient" style="--gradient: var(--color-blue-500), var(--color-pink-500), var(--color-red-500); --gradient-angle: 120deg"></div>Radial gradients
I used the same ideas above to create a radial gradient as well.
@utility radial-gradient { background-image: radial-gradient( var(--gradient-shape) var(--gradient-size) at var(--gradient-position) in var(--color-space) var(--hue-interpolation), var(--gradient) );}We have a few more variables here in this case:
--gradient-shape: The shape of the gradient, eitherellipseorcircle--gradient-size: The size of the gradient, whose value is one ofclosest-side,closest-corner,farthest-side, orfarthest-corner--gradient-position: Which lets you determine where the center of the gradient should be positioned.
When we have this, we just need to know a few memorable chunks instead of remembering the whole radial gradient syntax. Pretty cool, huh?
.selector { @apply radial-gradient; --gradient: var(--color-blue-500), var(--color-red-500); --gradient-shape: circle; --gradient-size: farthest-corner; --gradient-position: 25% 75%;}Text gradients
Strictly speaking, I consider text gradients more of an effect rather than a theming thing. But since we’re already talking about gradients, I thought it would be fitting to mention it here.
Creating these utilities is not that hard — you don’t have to be original and think up the techniques yourself, because most of the answers are already out there.
In this case, I simply used Chris Coyier’s solution for gradient text, plus my @gradient utility (and added a bugfix) to form the eventual gradient-text utility.
@utility gradient-text { @apply gradient; display: inline-flex; -webkit-text-fill-color: transparent; background-clip: text;
/* Allows multiline gradients in Safari */ box-decoration-break: clone;}Using this is simple too. You’ve seen everything you need from the examples above:
<h2 class="gradient-text" style="--gradient: var(--color-blue-500), var(--color-purple-500); --gradient-angle: 120deg"> Gradient text</h2>Gradient text
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