CSS gradients have been so lengthy that there’s no must rehash what they’re and use them. You might have certainly encountered them in some unspecified time in the future in your front-end journey, and in case you observe me, you additionally know that I exploit them on a regular basis. I exploit them for CSS patterns, good CSS decorations, and even CSS loaders. Besides, gradients have a tricky syntax that may get very difficult in a short time in case you’re not paying consideration.
On this article, we aren’t going to make complicated stuff with CSS gradients. As a substitute, we’re preserving issues easy and I’m going to stroll by the entire unimaginable issues we are able to do with only one gradient.
Just one gradient? On this case, studying the doc needs to be sufficient, no?
No, not likely. Comply with alongside and you will note that gradients are simple at their most simple, however are tremendous highly effective if we push them — or on this case, only one — to their limits.
CSS patterns
One of many first stuff you be taught with gradients is that we are able to set up repeatable patterns with them. You’ve in all probability seen some examples of checkerboard patterns within the wild. That’s one thing we are able to rapidly pull off with a single CSS gradient. On this case, we are able to attain for the repeating-conic-gradient()
perform:
background:
repeating-conic-gradient(#000 0 25%, #fff 0 50%)
0 / 100px 100px;
A extra verbose model of that with out the background
shorthand:
background-image: repeating-conic-gradient(#000 0 25%, #fff 0 50%);
background-size: 100px 100px;
Both manner, the end result is identical:
Fairly easy to this point, proper? You might have two colours that you would be able to simply swap out for different colours, plus the background-size
property to regulate the sq. shapes.
If we modify the colour stops — the place one coloration stops and one other begins — we get one other cool sample primarily based on triangles:
background:
repeating-conic-gradient(#000 0 12.5%, #fff 0 25%)
0 / 100px 100px;
In the event you evaluate the CSS for the 2 demos we’ve seen to this point, you’ll see that every one I did was divide the colour stops in half, 25%
to 12.5%
and 50%
to 25%
.
One other one? Let’s go!
This time I’m working with CSS variables. I like this as a result of variables make it infinitely simpler to configure the gradients by updating just a few values with out truly touching the syntax. The calculation is a bit more complicated this time round, because it depends on trigonometric features to get correct values.
I do know what you might be considering: Trigonometry? That sounds onerous. That’s actually true, significantly in case you’re new to CSS gradients. A great way to visualise the sample is to disable the repetition utilizing the no-repeat
worth. This isolates the sample to 1 occasion so that you simply clearly see what’s getting repeated. The next instance declares background-image
with no background-size
so you may see the tile that repeats and higher perceive every gradient:
I wish to keep away from a step-by-step tutorial for each instance we’re protecting in order that I can share heaps extra examples with out getting misplaced within the weeds. As a substitute, I’ll level you to a few articles you may consult with that get into these weeds and permit you to decide aside our examples.
I’ll additionally encourage you to open my on-line assortment of patterns for much more examples. A lot of the examples are made with a number of gradients, however there are lots that use just one. The aim of this text is to be taught just a few “single gradient” methods — however the final aim is to have the ability to mix as many gradients as doable to create cool stuff!
Grid strains
Let’s begin with the next instance:
You may declare that this belongs below “Patterns” — and you might be proper! However let’s make it extra versatile by including variables for controlling the thickness and the overall variety of cells. In different phrases, let’s create a grid!
.grid-lines {
--n: 3; /* variety of rows */
--m: 5; /* variety of columns */
--s: 80px; /* management the scale of the grid */
--t: 2px; /* the thickness */
width: calc(var(--m)*var(--s) + var(--t));
top: calc(var(--n)*var(--s) + var(--t));
background:
conic-gradient(from 90deg at var(--t) var(--t), #0000 25%, #000 0)
0 0/var(--s) var(--s);
}
To start with, let’s isolate the gradient to higher perceive the repetition (like we did within the earlier part).
One repetition will give us a horizontal and a vertical line. The scale of the gradient is managed by the variable --s
, so we outline the width and top as a multiplier to get as many strains as we wish to set up the grid sample.
What’s with “
+ var(--t)
” within the equation?
The grid winds up like this with out it:
We’re lacking strains on the proper and the underside which is logical contemplating the gradient we’re utilizing. To repair this, the gradient must be repeated yet one more time, however not at full measurement. Because of this, we’re including the thickness to the equation to have sufficient area for the additional repetition and the get the lacking strains.
And what a couple of responsive configuration the place the variety of columns will depend on the obtainable area? We take away the --m
variable and outline the width like this:
width: calc(spherical(down, 100%, var(--s)) + var(--t));
As a substitute of multiplying issues, we use the spherical()
perform to inform the browser to make the aspect full width and spherical the worth to be a a number of of --s
. In different phrases, the browser will discover the multiplier for us!
Resize the beneath and see how the grid behaves:
Sooner or later, we may even be capable of do that with the calc-size()
perform:
width: calc-size(auto, spherical(down, measurement, var(--s)) + var(--t));
Utilizing calc-size()
is basically the identical because the final instance, however as an alternative of utilizing 100%
we contemplate auto
to be the width worth. It’s nonetheless early to undertake such syntax. You’ll be able to take a look at the end result within the newest model of Chrome on the time of this writing:
Dashed strains
Let’s attempt one thing totally different: vertical (or horizontal) dashed strains the place we are able to management every little thing.
.dashed-lines {
--t: 2px; /* thickness of the strains */
--g: 50px; /* hole between strains */
--s: 12px; /* measurement of the dashes */
background:
conic-gradient(at var(--t) 50%, #0000 75%, #000 0)
var(--g)/calc(var(--g) + var(--t)) var(--s);
}
Can you determine the way it works? Here’s a determine with hints:

Strive creating the horizontal model by yourself. Right here’s a demo that reveals how I tackled it, however give it a attempt earlier than peeking at it.
What a couple of grid with dashed strains — is that doable?
Sure, however utilizing two gradients as an alternative of 1. The code is printed over at my assortment of CSS shapes. And sure, the responsive conduct is there as nicely!
Rainbow gradient
How would you create the next gradient in CSS?

You may begin by selecting as many coloration values alongside the rainbow as you may, then chaining them in a linear-gradient
:
linear-gradient(90deg, pink, yellow, inexperienced, /* and many others. */, pink);
Good thought, however it gained’t get you all the best way there. Plus, it requires you to juggle coloration stops and fuss with them till you get issues good.
There’s a easier resolution. We are able to accomplish this with only one coloration!
background: linear-gradient(90deg in hsl longer hue, pink 0 0);
I do know, the syntax seems unusual in case you’re seeing the new coloration interpolation for the primary time.
If I solely declare this:
background: linear-gradient(90deg, pink, pink); /* or (90deg, pink 0 0) */
…the browser creates a gradient that goes from pink to pink… pink in all places! Once we set this “in hsl
“, we’re altering the colour area used for the interpolation between the colours:
background: linear-gradient(90deg in hsl, pink, pink);
Now, the browser will create a gradient that goes from pink to pink… this time utilizing the HSL coloration area reasonably than the default RGB coloration area. Nothing adjustments visually… nonetheless see pink in all places.
The longer hue
bit is what’s attention-grabbing. Once we’re within the HSL coloration area, the hue channel’s worth is an angle unit (e.g., 25deg
). You’ll be able to see the HSL coloration area as a circle the place the angle defines the place of the colour inside that circle.

Because it’s a circle, we are able to transfer between two factors utilizing a “quick” path or “lengthy” path.

If we contemplate the identical level (pink
in our case) it signifies that the “quick” path incorporates solely pink and the “lengthy” path runs into all the colours because it traverses the colour area.
Adam Argyle printed a really detailed information on high-definition colours in CSS. I like to recommend studying it as a result of you will see that all of the options we’re protecting (this part particularly) to get extra context on how every little thing comes collectively.
We are able to use the identical method to create a coloration wheel utilizing a conic-gradient
:
background: conic-gradient(in hsl longer hue,pink 0 0);
And whereas we’re on the subject of CSS colours, I shared one other enjoyable trick that means that you can outline an array of coloration values… sure, in CSS! And it solely makes use of a single gradient as nicely.
Hover results
Let’s do one other train, this time working with hover results. We are likely to depend on pseudo-elements and further components in the case of issues like making use of underlines and overlays on hover, and we are likely to neglect that gradients are equally, if no more, efficient for getting the job accomplished.
Living proof. Let’s use a single gradient to kind an underline that slides on hover:
h3 {
background:
linear-gradient(#1095c1 0 0) no-repeat
var(--p,0) 100%/var(--p, 0) .1em;
transition: 0.4s, background-position 0s;
}
h3:hover {
--p: 100%;
}
You seemingly would have used a pseudo-element for this, proper? I feel that’s in all probability how most individuals would strategy it. It’s a viable resolution however I discover that utilizing a gradient as an alternative ends in cleaner, extra concise CSS.
You may be fascinated about one other article I wrote for CSS-Methods the place I exploit the identical method to create all kinds of cool hover results.
CSS shapes
Creating shapes with gradients is my favourite factor to do in CSS. I’ve been doing it for what looks like eternally and adore it a lot that I printed a “Fashionable Information for Making CSS Shapes” over at Smashing Journal earlier this 12 months. I hope you test it out not solely to be taught extra methods however to see simply what number of shapes we are able to create with such a small quantity of code — many who rely solely on a single CSS gradient.
A few of my favorites embrace zig-zag borders:
…and “scooped” corners:
…in addition to sparkles:
…and customary icons just like the plus signal:
I gained’t get into the main points of making these shapes to keep away from making this text lengthy and boring. Read the information and go to my CSS Form assortment and also you’ll have every little thing it’s good to make these, and extra!
Border picture methods
Let’s do yet one more earlier than we put a cap on this. Earlier this 12 months, I found how superior the CSS border-image
property is for creating totally different sorts of decorations and shapes. And guess what? border-image
limits us to utilizing only one gradient, so we’re obliged to observe that restriction.
Once more, only one gradient and we get a bunch of enjoyable outcomes. I’ll drop in my favorites like I did within the final part. Beginning with a gradient overlay:
We are able to use this system for a full-width background:
…in addition to heading dividers:
…and even ribbons:
All of those have historically required hacks, magic numbers, and different workarounds. It’s superior to see fashionable CSS making issues extra easy. Go learn my article on this subject to seek out all of the attention-grabbing stuff you can also make utilizing border-image
.
Wrapping up
I hope you loved this assortment of “single-gradient” methods. Most people I do know have a tendency to make use of gradients to create, nicely, gradients. However as we’ve seen, they’re extra highly effective and can be utilized for many different issues, like drawing shapes.
I like so as to add a reminder on the finish of an article like this that the aim is to not prohibit your self to utilizing one gradient. You should utilize extra! The aim is to get a greater deal with on how gradients work and push them in attention-grabbing methods — that, in flip, makes us higher at writing CSS. So, go forth and experiment — I’d like to see what you make!