Lesson 2 · CSS Fundamentals

Gradients: Linear, Radial and Conic

Write CSS gradients by hand: directions and angles, colour stops and hard stops, smoother blends with OKLCH, a safe fallback and readable text on top.

  • Beginner
  • 12 min
  • ✓ Completed

What you’ll learn

  • Write linear, radial and conic gradients
  • Control colour stops and hard stops
  • Add a colour-space fallback and check text contrast over a gradient

Before this lesson: Colour and Contrast: Making Text Readable

The idea in plain language

A CSS gradient An image generated by the browser that blends two or more colours, written with linear-gradient(), radial-gradient() or conic-gradient(). Because it is an image, it goes in background-image rather than background-color, and it costs no network request. See the glossary is an image the browser paints for you: a smooth blend between colours, drawn at any size with no file to download. There are three families – linear (along a line), radial (outwards from a point) and conic (around a point, like a clock hand) – and each has a repeating version.

Writing the three kinds

.linear  { background-image: linear-gradient(135deg, #1D4ED8, #FACC15); }
.radial  { background-image: radial-gradient(circle at 30% 30%, #ffffffaa, transparent 40%), linear-gradient(#1D4ED8, #1E3A8A); }
.conic   { background-image: conic-gradient(#A34A28 0 40%, #E9B496 0 75%, #2F6B4F 0); border-radius: 50%; }
  • Linear: with no direction it runs top to bottom; 0deg points up and angles turn clockwise.
  • Radial: by default an ellipse from the centre that reaches the farthest corner.
  • Conic: starts at the top and sweeps clockwise; perfect for pie charts and colour wheels.

Colour stops and hard stops

Each Colour stop A colour placed at a position along a CSS gradient, for example red 40%. The browser blends between neighbouring stops; two stops at the same position create a hard edge instead of a fade, which is how stripes and pie charts are drawn. See the glossary is a colour at a position. Without positions, stops are spread evenly. Giving two neighbouring stops the same position produces a hard stop – a sharp line – which is how the conic pie chart above gets its slices: #A34A28 0 40% fills 0–40%, then the next colour starts at the previous stop.

Worked example: a blend, its fallback and its text

We built a 135° blue-to-yellow gradient in our CSS Gradient Generator, blending in the OKLCH A CSS colour space that describes a colour by perceptual lightness (L), chroma (C, colourfulness) and hue (H). Because equal steps look roughly equal to the eye, it is useful for building colour palettes and smooth gradients, written as oklch(70% 0.15 250). See the glossary colour space. The generator exported:

.hero {
  background-image: linear-gradient(135deg, #1D4ED8 0%, #FACC15 100%);          /* fallback */
  background-image: linear-gradient(135deg in oklch, #1D4ED8 0%, #FACC15 100%); /* modern */
}

Gradients between hex colours blend in sRGB by default, which can look dull in the middle between distant hues. in oklch blends perceptually and keeps the middle vivid; browsers that do not understand it keep the first line.

Then the contrast check. The generator measures text contrast across the whole gradient, and for white text the worst point is only 1.53:1 – the yellow end. A darker gradient behaves very differently: cream text #F7F4EC over #3A2B23 → #2A1F1A has a worst case of 12.33:1, comfortably above the 4.5:1 that body text needs.

Gradient Text Worst contrast Verdict
#1D4ED8 → #FACC15 white 1.53:1 Fails – darken the yellow end or add a scrim
#3A2B23 → #2A1F1A cream #F7F4EC 12.33:1 Passes AA and AAA

Common mistakes

  • Using background-color – the gradient is silently ignored.
  • No solid fallback colour under the gradient, so text disappears if the image fails.
  • Checking contrast against the average colour instead of the weakest point.

Our blog has a longer complete guide to CSS gradients and a list of gradient mistakes and fixes once they are published.

Try it

Open the CSS Gradient Generator, set the angle to 135°, choose two colours and switch the colour space between sRGB and OKLCH to see the difference. Type your text colour into the checks panel to see the worst-case contrast.

Check your understanding

Five questions. Choose an answer for each, then check. You can try again as often as you like.

Question 1 Where does a CSS gradient go?
Question 2 With no direction given, linear-gradient() runs…
Question 3 What does linear-gradient(90deg, #fff 50%, #000 50%) draw?
Question 4 Why put a plain linear-gradient() line before one that uses 'in oklch'?
Question 5 White text over a blue-to-yellow gradient has a worst-case contrast of 1.53:1. What should you do?

Quiz progress is saved in this browser only. Nothing is sent to us.

Sources

  1. Using CSS gradients – MDN Web Docs
  2. CSS Images Module Level 3 – W3C
  3. CSS Color Module Level 4 – W3C

Educational content only. It explains how things work in general and is not financial, tax or legal advice for your situation. The lessons are free and educational. We don't issue certificates or credentials, and nothing here is personal financial advice.