CSS course menu
CSS Gradients: linear-gradient, radial-gradient and conic-gradient
Create CSS gradients without images: linear-gradient with angles and colour stops, hard stops for stripes, radial-gradient, conic-gradient for pie charts, repeating gradients, and gradient text.
On this page
A gradient is a smooth transition between colours. In CSS, gradients are images generated by the browser, so they are used wherever an image can go, usually background or background-image. They scale to any size and need no download.
linear-gradient#
.g { height: 60px; margin-bottom: 8px; border-radius: 8px; color: white; padding: 6px; font: 13px monospace; }<div class="g" style="background: linear-gradient(#2f5fe0, #9b5de5)">top to bottom (default)</div>
<div class="g" style="background: linear-gradient(to right, #ff7e5f, #feb47b)">to right</div>
<div class="g" style="background: linear-gradient(135deg, #11998e, #38ef7d)">135deg</div>- The direction is a keyword (
to right,to bottom left) or an angle (0degpoints up,90degpoints right). - Then list two or more colours.
Colour stops#
Each colour can have a position:
.g { height: 50px; margin-bottom: 8px; border-radius: 8px; }<div class="g" style="background: linear-gradient(to right, red, orange 20%, yellow 40%, green 60%, blue 80%, violet)"></div>
<div class="g" style="background: linear-gradient(to right, #2f5fe0 50%, #ffd34d 50%)"></div>When two stops share a position, the change is instant: a hard stop. That is how you draw stripes and split backgrounds.
radial-gradient#
Spreads out from a point:
.r { display: inline-block; width: 140px; height: 120px; margin: 4px; border-radius: 8px; }<div class="r" style="background: radial-gradient(#ffd34d, #e8590c)"></div>
<div class="r" style="background: radial-gradient(circle at 30% 30%, white, #2f5fe0 60%)"></div>
<div class="r" style="background: radial-gradient(circle, transparent 40%, #11998e 41%)"></div>The default shape is an ellipse that fits the box; circle and at x y change the shape and centre.
conic-gradient#
Rotates colours around a centre point, like a colour wheel. With hard stops, it draws a pie chart:
.pie {
width: 140px;
height: 140px;
border-radius: 50%;
background: conic-gradient(#2f5fe0 0 45%, #ffd34d 45% 75%, #e8590c 75% 100%);
}<div class="pie" role="img" aria-label="Pie chart: 45% blue, 30% yellow, 25% orange"></div>A chart drawn with CSS is just a picture to assistive technology, so give it a text alternative, as with aria-label here.
Repeating gradients#
repeating-linear-gradient repeats its stops to fill the box, which makes patterns:
.stripes {
height: 60px;
background: repeating-linear-gradient(45deg, #ffd34d 0 12px, #222 12px 24px);
}<div class="stripes"></div>Gradient text#
Clip the background to the shape of the text:
.fancy {
font: 800 40px system-ui, sans-serif;
background: linear-gradient(90deg, #2f5fe0, #e8590c);
background-clip: text;
color: transparent;
}<h1 class="fancy">Laviyx Learn</h1>Check that gradient text still has enough contrast with the page background across its whole range.
Practice#
Practice exercise
Create a progress bar that is 70% complete using a single linear-gradient with hard stops: green for the first 70% and light grey for the rest, with rounded ends.
Show solution
.progress {
height: 16px;
border-radius: 999px;
background: linear-gradient(to right, #2f9e44 70%, #e9ecef 70%);
}<div class="progress" role="img" aria-label="70% complete"></div>For a real progress indicator, the HTML <progress value="70" max="100"> element is the semantic choice; it can be styled too.
Key takeaway#
Finished this lesson?