On this page10 sections
Lecture 004: Backgrounds
| Category | 1: CSS Basics |
| Lecture | 004 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
Behind every element sits a background: a plain colour, a photo or a gradient. A few properties control which image appears, whether it repeats, how big it is, and where it sits.
Learning Objectives
- Add an image with
background-image - Control it with
background-repeat,background-sizeandbackground-position - Create gradients with
linear-gradient()andradial-gradient() - Write it all in one line with the
backgroundshorthand
1. Background Images
Example
<style>
.hero {
background-image: url("https://picsum.photos/id/1016/600/300");
height: 200px;
color: white;
padding: 16px;
}
</style>
<div class="hero">
<h2>Explore the mountains</h2>
</div>The element needs a height, or content, for the image to have room to show.
2. Repeat, Size and Position
| Property | Common values |
|---|---|
background-repeat | repeat (default, tiles the image), no-repeat, repeat-x, repeat-y |
background-size | cover (fill the box, cropping if needed), contain (show the whole image), 200px |
background-position | center, top, bottom right, 50% 20% |
Example: cover vs contain:
<style>
.box { height: 150px; margin-bottom: 8px; border: 1px solid #ccc;
background-image: url("https://picsum.photos/id/237/400/300");
background-repeat: no-repeat; background-position: center; }
.cover { background-size: cover; }
.contain { background-size: contain; }
</style>
<div class="box cover">cover: fills the box</div>
<div class="box contain">contain: whole image visible</div>For big header photos, cover plus center is the usual choice.
3. Gradients
A gradient is a smooth blend between colours, with no image file needed.
Example
<style>
div { height: 70px; margin-bottom: 8px; color: white; padding: 8px; }
.one { background: linear-gradient(to right, #ff6a00, #ee0979); }
.two { background: linear-gradient(135deg, #0175c2, #13b9fd); }
.three { background: radial-gradient(circle, #ffd43b, #f76707); }
</style>
<div class="one">linear, left to right</div>
<div class="two">linear, at 135 degrees</div>
<div class="three">radial, from the centre</div>4. Text Over a Photo
Layer a dark gradient on top of the image so white text stays readable. The first background listed is drawn on top.
Example
<style>
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.6), rgb(0 0 0 / 0.2)),
url("https://picsum.photos/id/1036/600/300") center / cover;
color: white;
padding: 50px 20px;
}
</style>
<div class="hero">
<h2>Winter sale</h2>
<p>Up to 40% off jackets this week.</p>
</div>5. The background Shorthand
These two rules do the same thing:
.hero {
background-color: #222;
background-image: url("photo.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.hero { background: #222 url("photo.jpg") no-repeat center / cover; }In the shorthand, the size goes after the position, separated by /.
Common Beginner Mistakes
1. No height. An empty <div> with a background image is 0 pixels tall, so nothing shows.
2. Accidental tiling. Small images repeat by default. Add no-repeat if you want one copy.
3. Unreadable text on photos. Add a dark overlay or a text background.
4. Wrong path in url(). In a separate CSS file, the path is relative to the CSS file,
not the HTML page.
Practice Questions
- Which property sets a background image?
- How do you stop a background image from repeating?
- What's the difference between
coverandcontain? - Write a left-to-right gradient from yellow to red.
- Two backgrounds are listed, separated by a comma. Which is drawn on top?
- Write the shorthand for a centred, non-repeating
bg.jpgthat covers the box.
Practice Answers
background-imagebackground-repeat: no-repeat;coverfills the whole box and may crop the image;containshows the whole image and may leave gaps.background: linear-gradient(to right, yellow, red);- The first one listed.
background: url("bg.jpg") no-repeat center / cover;
Key Takeaways
background-image: url(...)adds a picture; give the element some height.no-repeat,cover/containandcenterare the settings you'll use most.- Gradients need no image file:
linear-gradient()andradial-gradient(). - Layer a dark gradient over a photo to keep text readable.