Backgrounds and Gradients
How an element's background layers stack, how background-size and positioning work, and how linear, radial and conic gradients are just generated images.
Backgrounds and Gradients
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A gradient is an image, not a color. It belongs in `background-image` (or `background`), anywhere an `url()` image would go, and it can be sized, positioned, repeated and layered.
- `background` takes a comma-separated list of layers. The first layer paints on top, and `background-color` is the only part that can't be layered because it always sits at the bottom.
- Two color stops at the same position make a hard edge: `linear-gradient(red 50%, blue 50%)` is two flat halves with no blend. This is how stripes, patterns and progress bars are drawn.
- `linear-gradient`, `radial-gradient` and `conic-gradient` differ only in how they map a position to a point on the color line: along a line, outward from a center, or around a center.
- `background-clip: text` with a transparent text color paints the background through the letter shapes. Always keep a readable `color` fallback.
One box, several layers
An element’s background is not one thing. It is a color, plus any number of image layers painted on top of it. Think of it as a stack of transparent sheets on a light table: the color is the bottom sheet, and each image is a sheet laid above, with the first one you list at the very top.
.hero {
background:
linear-gradient(to bottom, rgb(0 0 0 / 0.6), transparent 60%), /* top layer */
url("city.jpg") center / cover no-repeat, /* below it */
#1b1b1b; /* color */
}
That is the standard “text over a photo” trick: the gradient sits above the photo and darkens just the edge the text sits on. The last layer may include a color, which paints underneath all of them and shows while the image loads.
The properties behind the shorthand
background is a shorthand, and each layer is controlled by its own longhand, which also takes comma-separated lists:
background-image: the image or gradient.background-positionandbackground-size: where it sits and how big it is. In the shorthand, the two are written together with a slash,center / cover.background-repeat:repeatis the default, which tiles.no-repeatplaces it once.background-originandbackground-clip: which box (border-box,padding-box,content-box) the positioning and painting use.
cover and contain are the two sizes you will use most. cover fills the box and crops, contain fits the whole image and may leave gaps. A reminder: the shorthand resets every longhand you don’t write, so a background: url(x) further down the cascade silently resets a size you set above it.
A gradient is an image generated from a recipe
The core idea is that the browser draws a gradient at the size of the box, with no file to download. That is why it goes anywhere an image goes:
.a { background: linear-gradient(to right, #ff6b6b, #4ecdc4); }
.b { background: radial-gradient(circle at 30% 20%, white, transparent 50%); }
.c { background: conic-gradient(from 0deg, red, yellow, lime, cyan, blue, magenta, red); }
The three differ in how they place the color line:
linear-gradientruns along a straight line at an angle or toward a side:to right,135deg.radial-gradientspreads outward from a center, as a circle or ellipse.conic-gradientsweeps around a center like a clock hand, which is how pie charts and color wheels are made.
Each takes color stops, optionally with positions: red 0%, red 30%, blue 30%, blue 100%. When two stops share a position, there is no distance to blend across, so you get a hard edge. That one idea gives you stripes and pattern backgrounds:
.stripes {
background: repeating-linear-gradient(45deg, #222 0 10px, #333 10px 20px);
}
.split { background: linear-gradient(to right, red 50% , blue 50%); }
Between colors written as rgb(), hex or names, a gradient blends in sRGB by default, so red to blue passes through a flat rgb(128 0 128). You can name another space in the function, like linear-gradient(in oklch, red, blue), and the blend follows the hue wheel with a more vivid midpoint. That pairs with color spaces.
Text and borders
background-clip: text clips the background to the shape of the glyphs. With a transparent text color, the gradient shows through the letters:
.title {
color: #5b21b6; /* fallback */
background: linear-gradient(90deg, #5b21b6, #db2777);
background-clip: text;
color: transparent;
}
Keep the fallback color visible when the gradient fails to apply. Gradients are also how gradient borders are drawn: layer a gradient on border-box under a solid fill clipped to padding-box.
What the video covers
CSSnippets’ “Modern CSS Gradients Tutorial — From Basics to Advanced Design Effects” is about ten and a half minutes, from October 2025. Its description says it covers linear and radial gradients, conic gradients, gradient borders, gradient text, overlays and animated gradients. I could not read a transcript, so I do not claim how each is demonstrated. The layered background explanation, background-size details and hard-stop pattern work above are the prose’s own. The deeper link is Kevin Powell on using gradients with a single color for layering effects.
Where this fits
Gradient stops are the color values from colors and color spaces. Backgrounds paint under borders and inside the box you meet in the box model and box-sizing, and the next lesson, borders, radius and outlines, covers what is drawn around that area. For where the frontend roadmap introduces CSS basics, see the box model and the cascade.
Check yourself
You write `background: url(photo.jpg), linear-gradient(black, transparent)`. Which one is on top?
The photo. In a multi-layer `background`, the first listed layer is painted on top. To darken the photo with a gradient overlay, list the gradient first: `background: linear-gradient(...), url(photo.jpg)`.
How do you draw a hard-edged 50/50 split with a gradient instead of a smooth blend?
Give both stops the same position: `linear-gradient(to right, red 50%, blue 50%)`. With no distance between the stops, there is nothing to interpolate across, so the color changes abruptly.
What's the difference between `background-size: cover` and `contain`?
`cover` scales the image until it fills the whole box, cropping whatever overflows. `contain` scales it until the whole image fits inside the box, leaving empty space if the shapes differ. Both keep the aspect ratio.
Why does a gradient on `background-color` not work?
`background-color` accepts only a single color. A gradient is an `<image>` value, so it must go in `background-image` or in the `background` shorthand, which sets the image layer.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.