1 Thing A Week 1 Thing
A Week

Smoother CSS gradients

Week 226.0 — 21st February '22

Gradients in CSS can be frustrating. It's not that easy to gently go from one colour to another.

A gradient fading from a vivid pink in to a vivid blue.

In Safari, the above example has a lot more blue towards the bottom than pink at the top. It just doesn't fade very well. We can fix this by introducing more stops.

Looks much better, but it's a lot of work to define each stop. If you're not precious about having a linear gradient and can cope with something more organic looking, you can use conic gradients. You can create a gradient that sweeps across the area and by playing with it's position and size you can create some really smooth gradients.

A sweeping gradient that fades from vivid pink in to a vivid blue, although what you can see is mostly blue.

While you could complain that we actually have a worse result as it is so blue. The gradient is subtler, but to me it's a far smoother and pleasing gradient. Thankfully, we can play with some values can can bring some of the pink back into play.

A sweeping gradient that fades from vivid pink in to a vivid blue.

This time, the corner is pretty harsh, so playing with the size, we can try and soften it a bit more.

A sweeping gradient that fades from vivid pink in to a vivid blue with a softer corner.
.grad-4 {
    background-image: conic-gradient(deeppink 70%, deepskyblue);
    background-size: 220%;
}

I really like how these gradients work. To me they appear much softer. Using gradients to apply a subtle block of colour behind some white text that appears on top of a photo is a problem I've had to solve many times but I've never been happy with the end result and it usually ends up meaning the entire photo needs some degree of shading or you have an unnatural cut-off point.

For that particular scenario, more steps in the gradient are more appropriate but for a lot of cases now, we can also look at considering other techniques.

More examples of smooth CSS gradients

Adam Argyle shard a link to a conic gradient gallery recently over at conic.style which is well worth checking out for examples and sample code.

There's a lot to look forward to when it comes to colours and gradients in CSS. There are going to be a lot more colour spaces to work with which will be a dream for designers (as if 16.7 million colours was't enough!).

More from 1 Thing A Week
« Web Development History Why Apple need to bring lux level support to HomeKit 4 Comedies worth watching twice »

Advertisement

Notable

Quick links and commentary on interesting articles, videos and more throughout the week.

  1. 226.2 – Tuesday
    1. Antti Laitinen's Broken Landscape series gessato.com

Jump to notable items for Week #225, Week #227 , view the notable archive or my favourites.

About 1 Thing A Week

A website curated by @cchana bringing you 1 Thing A Week, delivered every Monday morning.

You can find a list of all articles in the archive, or read the most popular.

Find out more about 1 Thing A Week or read the privacy policy.

Market

Visit the market and support 1 Thing A Week.

You can find 1 Thing A Week on Twitter, Facebook, Instagram & Reddit