CSS gradients and shadows explained
How linear and radial gradients, hard stops, box-shadow offsets, blur and spread work, with worked examples you can copy.
By the tools4free.dev team · Last reviewed 4 December 2026
A CSS gradient is an image the browser draws for you, and a box shadow is a blurred copy of an element's box drawn behind (or inside) it. Both are written as short values: linear-gradient(angle, colour stops) for a gradient and box-shadow: x y blur spread colour for a shadow. This guide explains each part, shows the numbers behind a few common patterns, and lists the mistakes that make gradients look muddy and shadows look heavy.
How a linear gradient is built
A linear gradient needs a direction and at least two colour stops. The direction is an angle or a keyword. 0deg points to the top and angles increase clockwise, so 90deg runs left to right, 180deg runs top to bottom and 270deg runs right to left. The keywords map to the same angles: to top is 0deg, to right is 90deg, to bottom is 180deg and to left is 270deg. If you leave the direction out, the browser uses to bottom.
background: linear-gradient(135deg, #4f46e5 0%, #ec4899 100%);
Each stop is a colour with an optional position. A position can be a percentage of the gradient line or a length such as 120px. A stop with no position is placed halfway between its neighbours, so these two lines draw the same image:
linear-gradient(red, orange, yellow, green, blue)
linear-gradient(red 0%, orange 25%, yellow 50%, green 75%, blue 100%)
With five stops there are four gaps, which is why the positions step by 25%. If you add or remove a stop and forget that unpositioned stops redistribute, every other colour moves too. Writing the positions down is safer once a gradient matters to the design.
Hard stops and stripes
Put two stops at the same position and there is no blend: the colour changes on a hard line. You can also give one colour two positions, which paints a solid band between them.
background: linear-gradient(90deg, #1d4ed8 0 50%, #f59e0b 50% 100%);
In a box 600px wide this draws blue for the first 300px and amber for the rest. A stop whose position is lower than the one before it is treated as if it sat at the earlier position, which also produces a hard edge. Stripes are built the same way: #fff 0 20%, #e5e7eb 20% 40% and so on gives equal 20% bands. For repeating patterns, repeating-linear-gradient() repeats a short run of stops along the line.
Colour hints move the midpoint
A bare percentage between two stops is a colour hint. It moves the point where the two colours are an even mix. linear-gradient(red 10%, 30%, blue 90%) puts the red-blue midpoint at 30% instead of the default 50%, so the transition leans towards red early and settles into blue. Hints are a gentle way to shape a gradient without adding stops.
Radial gradients
A radial gradient spreads outwards from a point. Its shape is circle or ellipse, and ellipse is the default. The size defaults to farthest-corner, meaning the final colour is reached at the corner furthest from the centre. The position follows the word at and works like background-position, defaulting to the centre.
background: radial-gradient(circle at 30% 20%, #fde68a 0%, #f59e0b 45%, #b45309 100%);
A glow behind a heading, a soft spotlight on a card or the shading on a button all come from moving the centre away from the middle. Layer several gradients by separating them with commas; the first one listed is on top, so put the translucent highlight first and the solid base last.
Gradients and colour spaces
By default, the browser blends between stops in sRGB. Blending two saturated colours that are far apart on the colour wheel can pass through a greyish middle. Modern CSS lets you pick another space with an in clause, for example linear-gradient(90deg in oklab, blue, red). Polar spaces such as oklch also let you choose the hue path. Check browser support for the space you pick, and test the result rather than assuming it will look better.
How box-shadow is built
A shadow takes up to four lengths, then a colour: offset-x offset-y blur spread colour. Only the two offsets are required.
| Part | What it does | Default |
|---|---|---|
| offset-x | Moves the shadow right (positive) or left (negative) | required |
| offset-y | Moves the shadow down (positive) or up (negative) | required |
| blur radius | Larger values give a wider, softer, lighter edge. Negative values are not allowed | 0 (sharp) |
| spread radius | Grows (positive) or shrinks (negative) the shadow before blurring | 0 |
| colour | The shadow colour, usually semi-transparent black | the element's text colour |
inset | Draws the shadow inside the box instead of outside | outer shadow |
A shadow follows the element's border-radius, so a rounded card gets a rounded shadow. It does not change the element's size or push other content around, which also means it can be clipped by a parent with overflow: hidden.
Why one big shadow looks heavy
A single shadow such as 0 20px 40px rgba(0,0,0,0.4) is dark right at the edge and fades slowly. Real light is softer: contact shadows are tight and faint, and the wider shadow is fainter still. Layering two or three shadows gets closer. The first one listed is on top:
box-shadow:
0 1px 2px rgb(0 0 0 / 12%),
0 4px 8px rgb(0 0 0 / 10%),
0 12px 24px rgb(0 0 0 / 8%);
Each layer roughly doubles the offset and blur of the one before and lowers the opacity. That progression is a design habit, not a rule in the specification, but it is an easy way to get consistent elevation levels across a design system.
Spread, inset and focus rings
A shadow with zero blur and a spread value draws a solid ring, which is a handy way to make a border that does not affect layout: box-shadow: 0 0 0 3px #2563eb. A negative spread with a positive y offset, such as 0 10px 15px -8px, keeps the shadow narrower than the box so it appears only below it. An inset shadow sits above the background but below the content, which suits pressed buttons and text fields.
Worked example: a button with a gradient and shadow
.btn {
background: linear-gradient(180deg, #6366f1 0%, #4f46e5 100%);
box-shadow: 0 1px 2px rgb(0 0 0 / 20%), inset 0 1px 0 rgb(255 255 255 / 25%);
border-radius: 8px;
}
The gradient runs top to bottom from a lighter to a darker indigo, which gives a slight curve. The first shadow is a tight, faint drop shadow. The second is an inset one-pixel highlight along the top edge. Keep gradients subtle on controls: a small difference between the two ends reads as depth, while a large one reads as decoration.
Gradients, text and accessibility
Text on a gradient has to pass contrast everywhere it sits, not only at the start colour. Measure it against the part of the gradient that is closest in lightness to the text. Our guide to WCAG colour contrast shows how the ratio is calculated. Shadows are not exposed to assistive technology, so do not rely on a faint shadow alone to separate a field from the page. Give inputs a border that meets the 3:1 non-text contrast ratio.
Common mistakes
- Forgetting that unpositioned stops are redistributed when you add another stop.
- Expecting
to rightand90degto differ. They are the same direction. - Using a high-opacity black shadow with a large blur. It looks dirty on light pages. Lower the opacity and layer instead.
- Putting text over a photo gradient without checking the lightest spot behind the words.
- Animating
box-shadowon many elements at once. Large blurred shadows are costly to repaint, so test on a modest device before using it on a long list. - Clipping shadows with
overflow: hiddenon a parent, then wondering why the edge is cut off. - Gradients between two very different hues passing through a dull grey middle. Add a mid stop or try another colour space.
Frequently asked questions
What is the difference between box-shadow and drop-shadow()?
box-shadow follows the rectangular box of an element, including its rounded corners. The drop-shadow() filter follows the actual painted shape, including transparent areas in a PNG or SVG, so it suits irregular shapes.
Can I animate a gradient?
Browsers do not interpolate between two different gradients in the background property in the same way as they do numbers. A common approach is to animate background-position on an oversized gradient, or to fade between two layers using opacity.
Why does my gradient look banded?
A smooth change over a large area with only a small difference in colour has few distinct values to use, so steps can be visible on some screens. Increasing the colour difference or adding a very subtle noise image helps.
How many shadows can one element have?
The syntax accepts a comma-separated list with no fixed limit, but each layer costs rendering time. Two or three well-chosen layers are usually enough.