Colour, depth and texture make a page feel finished. CSS can paint smooth gradients, layer several backgrounds, and add shadows to boxes and text, all without a single image file. That means faster pages and designs that are easy to change. This lesson covers background images and their sizing, linear, radial, conic and repeating gradients, and box and text shadows.
In this lesson you will learn
background-image,background-sizeandbackground-position- Linear, radial, conic and repeating gradients
- Layering a gradient over an image for readable text
box-shadow, including inset and multiple shadowstext-shadow
This lesson builds on CSS Colours, Units and Fonts.
Gradients
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gradients</title>
<style>
body { font-family: sans-serif; margin: 8px; display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
div { height: 80px; border-radius: 8px; color: white; padding: 8px; font-size: 13px; text-shadow: 0 1px 2px rgba(0,0,0,.5); }
.linear { background: linear-gradient(to right, #00695c, #26a69a); }
.angle { background: linear-gradient(135deg, #6a1b9a, #ec407a, #ffa726); }
.radial { background: radial-gradient(circle at top left, #ffeb3b, #f57c00); }
.stripes { background: repeating-linear-gradient(45deg, #1565c0 0 10px, #42a5f5 10px 20px); }
.overlay { background: linear-gradient(rgba(0,0,0,.6), rgba(0,0,0,.1)), #26a69a; }
.conic { background: conic-gradient(#c62828 0 40%, #1565c0 40% 75%, #2e7d32 75%); border-radius: 50%; width: 80px; }
</style>
</head>
<body>
<div class="linear">linear, to right</div>
<div class="angle">135deg, 3 colours</div>
<div class="radial">radial</div>
<div class="stripes">repeating stripes</div>
<div class="overlay">dark overlay for text</div>
<div class="conic"></div>
</body>
</html>
Result
- Gradients are images generated by the browser, so they go in
backgroundorbackground-image, notbackground-color. linear-gradient(to right, A, B)blends along a line. A direction can be a keyword (to bottom right) or an angle (135deg), and you can list as many colours as you like.radial-gradient(circle at top left, A, B)spreads outwards from a point.repeating-linear-gradientwith hard colour stops (#1565c0 0 10px, #42a5f5 10px 20px) draws stripes.- Several backgrounds can be stacked, separated by commas; the first is on top. A semi-transparent dark gradient over a photo keeps white text readable.
conic-gradientsweeps colours around a centre, which with hard stops andborder-radius: 50%makes a pie chart.
Background Images
.hero {
background-image: linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,.2)), url("campus.jpg");
background-size: cover; /* fill the box, cropping if needed */
background-position: center; /* keep the middle of the photo visible */
background-repeat: no-repeat;
min-height: 300px;
color: white;
}
| Property | Common values |
|---|---|
background-size | cover (fill, may crop), contain (fit, may leave gaps), 200px auto |
background-position | center, top right, 50% 20% |
background-repeat | no-repeat, repeat-x, repeat |
background-attachment | scroll, fixed (parallax-like effect) |
Shadows
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shadows</title>
<style>
body { font-family: sans-serif; margin: 14px; display: flex; gap: 18px; flex-wrap: wrap; background: #fafafa; }
.card { width: 130px; height: 70px; background: white; border-radius: 8px; padding: 8px; font-size: 13px; }
.s1 { box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.s2 { box-shadow: 0 6px 16px rgba(0,0,0,.2); }
.s3 { box-shadow: inset 0 2px 6px rgba(0,0,0,.25); }
.s4 { box-shadow: 0 0 0 3px #1565c0, 0 4px 10px rgba(21,101,192,.4); }
h2 { margin: 0; font-size: 26px; color: #1565c0; text-shadow: 2px 2px 0 #bbdefb; }
</style>
</head>
<body>
<div class="card s1">soft shadow</div>
<div class="card s2">raised card</div>
<div class="card s3">inset shadow</div>
<div class="card s4">ring + glow</div>
<h2>Text shadow</h2>
</body>
</html>
Result
box-shadow: x y blur colour:0 1px 3px rgba(0,0,0,.25)is a soft, close shadow;0 6px 16pxmakes a card look raised higher.- An optional fourth length, the spread, grows the shadow.
0 0 0 3px #1565c0uses spread with no blur to draw a solid ring that, unlike a border, does not change the element’s size. insetdraws the shadow inside the box, so it looks pressed in.- Multiple shadows are separated by commas.
text-shadow: x y blur colourworks the same way for text, without spread or inset.- Use semi-transparent black (
rgba) rather than grey for shadows that look natural on any background.
Common Mistakes
- Putting a gradient in
background-color, which only accepts a plain colour. - White text on a busy photo without an overlay, which fails contrast checks.
- Very dark, hard shadows that look dated; subtle and soft usually works better.
- Using the
backgroundshorthand afterbackground-size, which silently resets the size to its default.
Practice Exercises
Exercise 1. Create a button with a top-to-bottom gradient from #42a5f5 to #1565c0 and a soft shadow.
Show answer
.btn {
background: linear-gradient(#42a5f5, #1565c0);
color: white; border: 0; padding: 10px 18px; border-radius: 6px;
box-shadow: 0 3px 8px rgba(21, 101, 192, .35);
}
Exercise 2. Draw a progress bar that is 70% green and 30% grey using one gradient.
Show answer
.progress { height: 12px; border-radius: 6px;
background: linear-gradient(to right, #2e7d32 70%, #e0e0e0 70%); }
Two colours meeting at the same stop make a hard edge instead of a blend.
Frequently Asked Questions
Are CSS gradients faster than gradient images?
Usually yes: there is no file to download, they stay sharp at any size and screen density, and they are easy to change in one line of CSS.
What is the difference between box-shadow and filter: drop-shadow()?
box-shadow follows the element’s rectangular box. filter: drop-shadow() follows the actual visible shape, such as a transparent PNG or an SVG icon.
Summary
- Gradients are background images: linear, radial, conic and repeating.
- Stack backgrounds with commas; an overlay gradient keeps text readable on photos.
background-size: coverandbackground-positioncontrol photos.box-shadowandtext-shadowadd depth; keep them subtle.
