Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. FilmConvert Pro 1.241 WiFi Check Quick CSS Gradient 1.0 TextMate 2.0.4 FoneDog Toolkit IOS 2.0.16 Chamberlain 1.3 Native Instruments Mallet Flux V1.0.0 ON1 Effects 2019.6 V13.6.0.7353 Crack Adobe Photoshop Lightroom Classic CC 2019 V8.1 Torrent Capto 1.2.16 Crack Habitat 1.0 Infographics Lab For PowerPoint 1.0.3.
Caution! Hello there, intrepid explorer! This post is old. The content might be out of date, and our opinions might have changed.
Ever used CSS gradients and thought “Urghh!”. We have. Here's a quick post about making them smooth as peanut butter.
We've had a couple of sites lately that use CSS gradients behind some text. This let us have a background image of any (or a mix of) colours, and still keep the text nicely readable.
The issue is, in the default state, CSS gradients can look a bit.. gross. I'm sure some of you have written gradients like this:
CSS gradients also support transparency, which can be used to create fading effects. To add transparency, we use the rgba function to define the color stops. The last parameter in the rgba function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).
Which works and you get a gradient that goes from solid black to transparent. Unfortunately you probably also get a nasty looking hard edge line going across the element. Check out this example screen shot:
In looking for a fix for this, I stumbled across this post on CSS Tricks by the clever and talented Andreas Larsen, which explains how to make them much nicer using physics, maths and diagrams of motorway intersections. Don't let that description put you off though, is an interesting read. The trick is to create a curve to ease your gradients on and then add the appropriate stops in your CSS. More like this:
This creates a gradient more like this:
It's called a Scrim. I know what your thinking, that looks complicated. How do I use that? Well Andreas created a Post CSS plugin to smooth all your gradient troubles. Pretty sweet. Inspired by this I created a SCSS mixin to achieve the same thing.
I hear you like syntax highlighting? I've got your back over here in this Gist.
Here's a comparison of the two techniques, which shows makes the difference more obvious:
More posts
CSS gradients let you display smooth transitions between two or more specified colors.
Linear Gradients (goes down/up/left/right/diagonally)
Radial Gradients (defined by their center)
CSS Linear Gradients
To create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.
If you want more control over the direction of the gradient, you can define an angle, instead of the predefined directions (to bottom, to top, to right, to left, to bottom right, etc.). A value of 0deg is equivalent to 'to top'. A value of 90deg is equivalent to 'to right'. A value of 180deg is equivalent to 'to bottom'.
CSS gradients also support transparency, which can be used to create fading effects.
To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).
Css Gradient Generator
The following example shows a linear gradient that starts from the left. It starts fully transparent, transitioning to full color red: