Skip to main content

CSS Colors and Backgrounds

CSS Background Image

Written by Updated CSS Background Image exercises

Add image and gradient backgrounds, control their source and fallback color, and keep decorative images separate from meaningful content.

background-image puts a picture behind an element's content. It is perfect for decoration, like a hero photo behind a heading. It is wrong for images that carry information, because a background has no alt text.

You will almost always use it with a few companion properties, for size, position and repeat. A plain color is covered in CSS Background Color, and stacking several images in Layered Backgrounds.

Syntax

css

selector {
    background-image: url("image.jpg");
}

Example

css

body {
    background-image: url("background.jpg");
}

That puts the image behind the whole page.

The properties you need with it

PropertyWhat it controlsExample
background-imageThe image itselfbackground-image: url("img.jpg");
background-repeatWhether it tiles. The default is to repeat!background-repeat: no-repeat;
background-sizeHow big it is. cover fills the box, contain shows the whole image.background-size: cover;
background-positionWhich part stays visiblebackground-position: center;
background-attachmentfixed keeps it still while the page scrollsbackground-attachment: fixed;

The background shorthand sets them all at once: background: url("img.jpg") center / cover no-repeat;. The size must come straight after the position, with a slash.

Complete CSS Example for Background Image

html

<!DOCTYPE html>
<html>
<head>
    <title>CSS Background Image</title>
    <style>
        body {
            background-image: url("https://picsum.photos/500/500?grayscale");
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center;
        }
        h1 {
            color: white;
            text-align: center;
        }
        p {
            color: yellow;
            text-align: center;
        }
    </style>
</head>
<body>

    <h1>Background Image Example</h1>
    <p>This page uses a background image.</p>

</body>
</html>

The photo fills the whole page, centered and without repeating, and the text sits on top of it.

Browser Support

Feature
Chrome
Edge
Firefox
Safari
background-image11211

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.

Keep text readable

Text on a busy photo is hard to read, and the photo changes with the screen size, so the bright part may land behind your heading. Put a dark gradient over the image:

A dark overlay for readable white text

css

.hero {
  background:
    linear-gradient(rgb(0 0 0 / 0.5), rgb(0 0 0 / 0.5)),
    url("hero.jpg") center / cover no-repeat;
  background-color: #1e293b;
  color: white;
}

The background-color is a fallback: if the image fails to load, the white text still has a dark background behind it.

Good to know

  • The image path in url() is relative to the CSS file, not to the HTML page. This catches everyone once.
  • Compress large background photos, and use modern formats like WebP. A huge hero image is a common reason pages load slowly.
  • background-attachment: fixed does not work on most phones.

CSS Background Image exercises

A gradient

Give the hero a background image that runs from red on the left to blue on the right.

The HTML your code works on
<div class="hero">Welcome</div>

Result

Check your answer to see it here.