Aral Code

Colors & the Box Model Lesson 6 of 33

CSS Backgrounds — Colors, Images, Size and Position

Add background colors and images with CSS, control repeat, size and position, and build a hero banner with text over an image.

Every element has a background, and by default it's transparent. CSS lets you fill it with a color, an image, a gradient, or several of these at once. Backgrounds are how designers create banners, section colors and patterned headers.

background-color

You've already seen this one. It fills the element's whole box, including the padding, with one color.

Example Section colors
<style>
  section { padding: 16px; }
  .s1 { background-color: #e3f1ec; }
  .s2 { background-color: #fdf3dc; }
  .s3 { background-color: #fbe9e6; }
</style>
<section class="s1">Section 1: About Us</section>
<section class="s2">Section 2: Our Products</section>
<section class="s3">Section 3: Contact</section>

background-image

Use url() with the path to an image file:

header {
  background-image: url("images/rice-terraces.jpg");
}

The path works like the src of an <img>, but it's relative to the CSS file, not the HTML page. If your CSS is in a css/ folder and the image is in images/, write url("../images/rice-terraces.jpg"). The ../ means "go up one folder".

The examples on this page use images from this website. Here's one, a small SVG picture of a mango:

Example A background image
<style>
  .box {
    height: 180px;
    background-image: url("/img/mango.svg");
    border: 2px solid #ccc;
  }
</style>
<div class="box"></div>

Notice the mango is repeated like tiles. That's the default. Let's control it.

background-repeat

  • repeat — tile in both directions (default)
  • no-repeat — show the image once
  • repeat-x — tile only horizontally
  • repeat-y — tile only vertically
Example Repeat options
<style>
  .b { height: 120px; margin-bottom: 10px; border: 1px solid #ccc; background-image: url("/img/mango.svg"); background-size: 60px; }
</style>
<div class="b" style="background-repeat: no-repeat;"></div>
<div class="b" style="background-repeat: repeat-x;"></div>

background-size

This is the most important background property for photos:

  • cover — scale the image to fill the whole box. Parts may be cut off, but there are no empty spaces. Best for banners.
  • contain — scale the image so the whole image fits inside. There may be empty space.
  • A size like 100px or 50% — an exact size.
Example cover vs. contain
<style>
  .b { width: 45%; height: 180px; display: inline-block; border: 2px solid #0f5c4d; background-image: url("/img/jeepney.svg"); background-repeat: no-repeat; background-color: #fdf3dc; }
</style>
<div class="b" style="background-size: cover;"></div>
<div class="b" style="background-size: contain;"></div>

background-position

Where the image sits in the box: center, top, bottom, left, right, or combinations like top right, or exact values like 20px 40px. With cover, center is the usual choice, so the middle of the photo stays visible on every screen size.

The background shorthand

You can write several background properties in one line with background:

/* longhand */
background-color: #fdf3dc;
background-image: url("/img/jeepney.svg");
background-repeat: no-repeat;
background-position: center;
background-size: contain;

/* shorthand: position / size must be written with a slash */
background: #fdf3dc url("/img/jeepney.svg") no-repeat center / contain;

Project: a hero banner

A "hero" is the big banner at the top of a home page. The classic recipe: a photo with cover, a dark see-through layer on top so the text is readable, and centered text.

Example Hero banner with a dark overlay
<style>
  .hero {
    min-height: 260px;
    display: grid;
    place-items: center;
    text-align: center;
    color: white;
    padding: 20px;
    background:
      linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.55)),
      url("/img/halo-halo.svg") center / cover no-repeat;
  }
  .hero h1 { margin: 0 0 8px; font-family: Arial, sans-serif; font-size: 34px; }
  .hero p { margin: 0; font-size: 18px; }
</style>
<div class="hero">
  <div>
    <h1>Halo-Halo ni Lola</h1>
    <p>Pinakamasarap na panghimagas sa tag-init. ₱60 lang!</p>
  </div>
</div>

The trick is having two backgrounds separated by a comma. The first one listed is on top: a gradient that is the same see-through black at both ends, which darkens the image. The second is the picture itself. You'll learn more about gradients in the Gradients and Shadows lesson.

background-attachment

background-attachment: fixed keeps the image still while the page scrolls, creating a "parallax" effect. It looks nice on laptops, but many phones ignore it, so don't depend on it.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. By default, what does a background image do if it's smaller than the box?
2. Which value makes a background image fill the whole box, even if some of it is cut off?
3. When you list two backgrounds separated by a comma, which one appears on top?
4. A product photo shows the price on it and must be read by screen readers. What should you use?

By Sir Reyel · Last updated 2026-12-04