CSS Basics

CSS Backgrounds

CSS Backgrounds are properties that control the background layer of elements, including background-color, background-image, background-position, background-size, background-repeat, and background-attachment, allowing complex layered background effects.

How do you set a background image in CSS?

Use background-image: url("photo.jpg"), then background-size: cover to fill the element without distortion and background-position: center to centre it. Add background-repeat: no-repeat so the image doesn't tile. You can layer several backgrounds, separated by commas.

.hero {
  background-image: url("/hero.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
/* Shorthand with a dark overlay */
.hero {
  background:
    linear-gradient(rgb(0 0 0 / .5), rgb(0 0 0 / .5)),
    url("/hero.jpg") center / cover no-repeat;
}

Background Types

Colors

  • • Solid colors
  • • Transparent
  • • RGBA values

Images

  • • Photos
  • • Patterns
  • • SVG graphics

Gradients

  • • Linear
  • • Radial
  • • Conic

Multiple

  • • Layered
  • • Blended
  • • Complex

CSS Backgrounds Showcase

Explore comprehensive background techniques including colors, images, gradients, positioning, and advanced effects. Click cards to change the page background!

Code Editor

<div class="hero">Background image with overlay</div>
<div class="pattern">Repeating gradient pattern</div>

Preview

Background Properties

Essential Properties

background-colorSets the background color
background-imageAdds background images
background-sizeControls image sizing
background-positionSets image position

Advanced Properties

background-repeatControls image repetition
background-attachmentFixed or scroll behavior
background-clipDefines painting area
background-originSets positioning area

Background Shorthand Property

/* Shorthand: color image position/size repeat attachment */ background: #3b82f6 url('image.jpg') center/cover no-repeat fixed; /* Multiple backgrounds (comma-separated) */ background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('hero.jpg') center/cover no-repeat;

The shorthand property combines all background properties in a single declaration.

Your Learning Path

Master CSS backgrounds and visual effects

What You've Learned

  • ▶Background colors and transparency
  • ▶Background images and patterns
  • ▶Linear and radial gradients
  • ▶Background positioning and sizing
  • ▶Multiple background layers

What's Next

  • ▶CSS Borders - Frame your content
  • ▶CSS Margins - Control outer spacing
  • ▶CSS Padding - Manage inner spacing
  • ▶CSS Box Model - Master layout fundamentals

Pro Design Tip

Use multiple background layers to create rich, complex designs. Always consider performance when using large background images or complex gradients.

CSS Backgrounds — step by step

1 / 4
1

background-color and background-image

Set solid fills with background-color or use background-image with url() for images, or gradients like linear-gradient().

1 of 4
🧠cover Crops, contain Fits
Think of background-size like a photo frame: cover fills the frame completely (photo may be cropped), contain keeps the full photo visible (frame may have empty space).

🎯 Quick Check

Which background-size value ensures the entire image is always visible?

CSS Backgrounds: summary

Key points

  • cover fills the box and may crop the image; contain shows the whole image and may leave empty space.
  • Background images aren't content: screen readers and search engines skip them. Use <img> with alt for meaningful images.
  • Gradients are background images, so they layer with photos.
  • background-attachment: fixed creates a parallax effect, but it's slow on mobile and ignored by iOS.

Frequently asked questions

›How do I make a background image cover the whole element?

Set background-size: cover; background-position: center;. The image scales to fill the element while keeping its aspect ratio.

›Why is my background image not showing?

Check the URL path (it's relative to the CSS file, not the HTML), and make sure the element has a height. An empty element with no height has nowhere to show the image.