How to Create CSS Gradients: A Practical Guide
A gradient is a smooth blend from one colour to another. Used well, it adds depth to a background and makes a button feel more tactile. Used badly, it makes text hard to read.
This guide covers linear, radial and conic gradients, angles and colour stops, mesh gradients, and when a gradient helps — or hurts readability. Every example is ready to copy and paste.
What Is a CSS Gradient?
In CSS, a gradient is not an image file. The browser generates it from a background-image value, so nothing extra downloads.
The three main types are linear, radial and conic. Linear changes colour along a straight line, radial spreads outwards from a point, and conic rotates colours around a point, like slices of a pie. A gradient needs at least two colours, called colour stops. Add a plain background colour before the gradient as a fallback for older browsers.
Linear Gradients: Direction, Angles and Colour Stops
Linear gradients are the most common type, and the best place to start:
.hero { background-image: linear-gradient(135deg, #5B4BFF 0%, #FF5D8F 100%); }
This runs from violet #5B4BFF to pink #FF5D8F at 135 degrees. 0deg points up, 90deg points right, 180deg points down, and 270deg points left. An angle of 135deg runs diagonally from top-left to bottom-right. You can also use words, such as linear-gradient(to right, #5B4BFF, #00C2A8).
Colour stops control where each colour begins and ends. By default the first starts at 0% and the last ends at 100%, but percentages let you shape the blend:
.card { background-image: linear-gradient(135deg, #5B4BFF 0%, #FF5D8F 50%, #5B4BFF 100%); }
That symmetrical return suits banners. Two or three stops are enough; each extra stop makes the result busier. For combinations, try the color palette generator first.
A hard stop gives two colours the same position, producing a sharp edge instead of a blend:
.stripes { background-image: linear-gradient(to right, #5B4BFF 0 50%, #FFD166 50% 100%); }
The first half is violet and the second half is yellow. Hard stops suit two-tone panels and stripes.
Radial Gradients: Shape and Position
A radial gradient spreads out from a point, usually the centre. It suits a soft glow behind a heading, a spotlight effect, or a circular badge that looks lit from above:
.spotlight { background-image: radial-gradient(circle at center, #FFD166 0%, #5B4BFF 100%); }
The shape can be a circle or an ellipse. A circle stays round; an ellipse stretches to fill the element, which suits wide banners. Position is set with at, followed by keywords such as center, or percentages such as at 30% 20%. Use radial gradients sparingly: one glow can lift a section.
Conic Gradients: Angles, Pie Charts and Colour Wheels
A conic gradient rotates colours around a centre point, making it the natural choice for pie-chart segments, dials, progress rings and colour wheels:
.pie { background-image: conic-gradient(from 0deg, #5B4BFF 0deg 120deg, #00C2A8 120deg 240deg, #FFD166 240deg 360deg); }
Each colour gets a start and end angle, so the segments have hard edges; from 90deg starts at the top. For a smooth colour wheel, leave out the end angles:
.wheel { background-image: conic-gradient(#FF5D8F, #FFD166, #00C2A8, #5B4BFF, #FF5D8F); }
Repeating the first colour at the end closes the wheel cleanly, without a seam.
Mesh Gradients: What They Are and When to Use Them
A mesh gradient is a multi-colour, organic blend that looks like soft, overlapping washes of colour. It is not a single CSS function; it is usually built by layering several radial gradients over a base colour:
.mesh { background-image: radial-gradient(at 20% 20%, #5B4BFF 0, transparent 55%), radial-gradient(at 80% 30%, #FF5D8F 0, transparent 55%), radial-gradient(at 50% 90%, #00C2A8 0, transparent 55%); background-color: #12153A; }
Mesh gradients suit large hero areas. They are a poor choice behind body text, because the colour under the words changes from place to place. Keep them to three or four colours in a similar family. For a starting point, try the gradient color generator.
Step by Step: Build a Gradient for a Hero Section
- Pick two colours that work together. Start with your brand colour and a neighbour on the colour wheel. Violet #5B4BFF and pink #FF5D8F work well; violet and teal #00C2A8 are cooler and calmer.
- Choose the type. Use linear for most sections and buttons, radial for a glow, and conic only for a chart, dial or wheel.
- Set the direction. For a hero, try 135deg. For a button, to right usually reads best.
- Add a fallback colour. Put a plain background-color before the gradient, using the first or darkest colour in the blend.
- Test at full width. Check desktop and mobile widths.
- Add an overlay for text. A semi-transparent dark layer evens out the brightness under white text.
- Check the contrast. Run the result through the color contrast checker. If it fails against the lightest part, darken the overlay.
A finished hero rule looks like this:
.hero { background-color: #5B4BFF; background-image: linear-gradient(rgba(18, 21, 58, 0.45), rgba(18, 21, 58, 0.45)), linear-gradient(135deg, #5B4BFF 0%, #FF5D8F 100%); color: #FFFFFF; }
The first layer is the overlay, the second is the colour gradient, and the fallback sits underneath both.
Gradients for Backgrounds, Buttons and Cards
A background can carry a strong blend because it sits behind everything else. A button should be subtler, because its job is to be recognised and clicked. A card usually needs the least gradient of all — a faint tint is enough.
For buttons, try a gentle linear gradient with two close colours, with white text and a hover state that deepens both colours slightly. For cards, use a gradient only on a small accent, such as a top border, and keep the body plain. For page backgrounds, a very light gradient adds warmth; a strong, saturated one quickly tires the eye.
Text on Gradients and Readability
Text on a gradient is where most gradient designs fail. A gradient changes colour across the element, so text readable at one end may be unreadable at the other. White text over violet-to-pink, for example, reads well over the violet but can fade over a pale pink middle.
Fix it in three ways: add an overlay to even out brightness, keep text on the darkest or most consistent part of the gradient, and test the text colour against both the lightest and darkest parts, not just the average.
Performance and File Size
Gradients are cheap: the browser generates them, so they add no image download beyond a line or two of CSS.
Complex layered mesh backgrounds can take a little longer to paint on lower-powered devices, but a handful of gradients on a page is fine. If a page feels slow, check large images, fonts and scripts first.
Common Mistakes to Avoid
- Too many colour stops. Four or five stops usually produce a rainbow effect. Start with two.
- Clashing colours. Two saturated colours from opposite sides of the wheel can produce a muddy middle. Pick colours closer together.
- Unreadable text. Test text over the full gradient and add an overlay if the contrast is borderline.
- Gradients everywhere. A gradient on the header, hero, every button and every card stops being a highlight. Use it in one hero area, plus buttons.
- Forgetting the fallback. A missing background-color leaves older browsers with a blank background.
- Copying without checking. A gradient from a tutorial may clash with your palette. Paste it in, then adjust and test it.
Frequently Asked Questions
What is the simplest CSS gradient?
A two-colour linear gradient, such as linear-gradient(to right, #5B4BFF, #FF5D8F).
How do I make a gradient go diagonally?
Use an angle, such as linear-gradient(135deg, #5B4BFF, #FF5D8F), which runs from top-left to bottom-right.
What is the difference between a hard stop and a smooth blend?
A smooth blend changes gradually, which is the default. A hard stop gives two colours the same position, producing a sharp edge.
Can I use a gradient on text?
Yes, for short headings, using background-clip: text with transparent text colour and a solid-colour fallback.
How do I make text readable on a gradient?
Add a semi-transparent overlay and check contrast against both the lightest and darkest parts.
Closing
Gradients are a small tool with an outsized effect: a line or two of CSS can give a page depth, or quietly make it harder to read. The difference is usually restraint — two colours instead of five, one gradient area instead of six, and text that has been checked.
Start with a simple linear gradient, test it at full width, and check the contrast before you publish. To experiment, the CSS gradient generator and the linear gradient generator let you build and preview one first.