On this page10 sections
  1. Learning Objectives
  2. 1. Background Images
  3. 2. Repeat, Size and Position
  4. 3. Gradients
  5. 4. Text Over a Photo
  6. 5. The background Shorthand
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 004: Backgrounds

Category1: CSS Basics
Lecture004 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

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-size and background-position
  • Create gradients with linear-gradient() and radial-gradient()
  • Write it all in one line with the background shorthand

1. Background Images

Example

html
<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

PropertyCommon values
background-repeatrepeat (default, tiles the image), no-repeat, repeat-x, repeat-y
background-sizecover (fill the box, cropping if needed), contain (show the whole image), 200px
background-positioncenter, top, bottom right, 50% 20%

Example: cover vs contain:

html
<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

html
<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

html
<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:

css
.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

  1. Which property sets a background image?
  2. How do you stop a background image from repeating?
  3. What's the difference between cover and contain?
  4. Write a left-to-right gradient from yellow to red.
  5. Two backgrounds are listed, separated by a comma. Which is drawn on top?
  6. Write the shorthand for a centred, non-repeating bg.jpg that covers the box.

Practice Answers

  1. background-image
  2. background-repeat: no-repeat;
  3. cover fills the whole box and may crop the image; contain shows the whole image and may leave gaps.
  4. background: linear-gradient(to right, yellow, red);
  5. The first one listed.
  6. background: url("bg.jpg") no-repeat center / cover;

Key Takeaways

  • background-image: url(...) adds a picture; give the element some height.
  • no-repeat, cover/contain and center are the settings you'll use most.
  • Gradients need no image file: linear-gradient() and radial-gradient().
  • Layer a dark gradient over a photo to keep text readable.