linear-gradient()
Creates a smooth colour transition along a straight line. It is used as an image, most often in background or background-image.
Example
#.hero {
background:
linear-gradient(to top, rgb(12 12 16 / 0.95), transparent 70%),
url("/collections/covers/kurs-html-css-od-podstaw.webp") center / cover;
}<div class="hero" style="display: flex; align-items: flex-end; height: 200px; padding: 16px; border-radius: 14px;">
<div>
<strong style="font-size: 20px;">HTML and CSS course from scratch</strong>
<p style="margin: 2px 0 0; color: #d4d4d8;">11 modules · Ancient Egypt</p>
</div>
</div>The gradient darkens the bottom of the photo, so white text is legible no matter what is in the picture.
Definition and usage
#The linear-gradient() function generates an image in which colours blend smoothly into one another along a straight line. Because it is an image, not a colour, you put it where CSS accepts images: in background-image or the background shorthand, and also in border-image and mask-image. It won’t work in background-color or color.
The first, optional argument is the direction. The words to right or to bottom right indicate the edge or corner the gradient runs towards, and an angle, for example 45deg, rotates the line: 0deg points up, 90deg to the right, and the default 180deg, that is to bottom, down.
After the direction you list the colours, at least two. Each can have a position: #7c3aed 20% means that pure purple starts at 20% of the gradient’s length. Two colours at the same position give a hard edge instead of a transition, which lets you draw stripes. The repeating-linear-gradient() version repeats the pattern indefinitely.
In background you can give several layers separated by commas, for example a semi-transparent gradient over a photo so that the text on it stays readable. The most reliable way to get a smooth change of a gradient is to animate its position (background-position) or a variable registered with @property.
Syntax
#linear-gradient([<angle> | to <side>]?, <color> <position>?, <color> <position>?, …)Syntax variants
#top to bottom
Two colours with no direction. By default the gradient runs from top to bottom.left to right
A direction given in words: the gradient runs towards the right edge.diagonal
An angle rotates the gradient line. 45 degrees points towards the top right corner.transition in the middle
Pure colours up to 30% and from 70% of the length, with the transition only in between.hard edge
Two colours at the same position. Instead of a transition there is a sharp edge, like on a flag.several colours
There can be any number of colours. Without positions the browser spreads them out evenly.stripes
A pattern repeated endlessly. Hard edges every 12 px give diagonal warning stripes.darkened photo
Two background layers: the gradient lies on the photo and darkens its bottom. On such a background it is easy to place legible text.
Syntax variants in practice
##7c3aed, #0ea5e9
.demo {
background: linear-gradient(#7c3aed, #0ea5e9);
}to right
.demo {
background:
linear-gradient(to right, #7c3aed, #0ea5e9);
}45deg
.demo {
background:
linear-gradient(45deg, #7c3aed, #0ea5e9);
}#7c3aed 30%, #0ea5e9 70%
.demo {
background: linear-gradient(to right,
#7c3aed 30%, #0ea5e9 70%);
}#7c3aed 50%, #0ea5e9 50%
.demo {
background: linear-gradient(to right,
#7c3aed 50%, #0ea5e9 50%);
}#f43f5e, #f59e0b, #10b981
.demo {
background: linear-gradient(to right,
#f43f5e, #f59e0b, #10b981);
}repeating-linear-gradient()
.demo {
background: repeating-linear-gradient(45deg,
#f59e0b 0 12px, #18181b 12px 24px);
}linear-gradient() + url()
.demo {
background:
linear-gradient(transparent, #0c0c10),
url("/collections/covers/vibe-coding.webp")
center / cover;
}More examples
#.title {
background: linear-gradient(90deg, #a78bfa, #38bdf8);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}<h2 class="title" style="display: inline-block; margin: 0; font-size: 34px;">CSS World</h2>The background is clipped to the shape of the letters thanks to background-clip: text, and the transparent text colour reveals it.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- A gradient is an image, so put it in
backgroundorbackground-image. Inbackground-colorit will be ignored. - Between two colours you can put just a position, for example
#7c3aed, 20%, #0ea5e9. This is a hint for where the transition should reach its midpoint. - Draw stripes and flags with hard edges: two colours at the same position, for example
#000 50%, #fff 50%.
Related entries
#- radial-gradient()Creates a colour transition radiating outwards from a point, along a circle or an ellipse. Good for glows, vignettes, and soft lights.
- conic-gradient()Creates a colour transition around a point, like a clock hand. It draws pie charts, colour wheels, and rotating borders.
- background-imageSets an image or gradient as an element’s background.
- backgroundA shorthand that sets all background properties in one declaration.
- background-clipSets which area of the element the background extends to, including the text background effect.
- color-mix()Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.