Lesson 15 of 50
cssBackgrounds – Images & Gradients
10 mins
Course progress30%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 15 of 50
css10 mins
Keep these ideas in mind as you work through the lesson.
Backgrounds are a layered paint system. You can stack multiple images, gradients, and color fills on a single element.
background-image – URL, linear-gradient(), radial-gradient(), conic-gradient(), or a combination.background-size – cover, contain, or explicit dimensions (200px 100%).background-position – center, top left, percentages, or exact coordinates.background-repeat – no-repeat, repeat-x, repeat-y, space, round.background-attachment – scroll (default) or fixed (parallax effect).background-blend-mode – blend multiple layers (multiply, screen, overlay)..hero {
background-image:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('hero.jpg');
background-size: cover;
background-position: center;
color:#fff;
padding:4rem 2rem;
text-align:center;
}
The first background (the gradient) sits on top of the image, providing a dark overlay that improves text readability.
image-set() to supply fallback formats:background-image: image-set(
url('hero.avif') type('image/avif') 1x,
url('hero.webp') type('image/webp') 1x,
url('hero.jpg') type('image/jpeg') 1x
);
Always supply a meaningful alt attribute on the <img> element if the image conveys information. Pure CSS backgrounds are decorative; they don’t need alt text.