<htmlstart/>

CSS Backgrounds

Add colors, images, gradients, and patterns as backgrounds.

Background Color

.hero {
  background-color: #111;
}

Background Image

.hero {
  background-image: url("photo.jpg");
  background-size: cover;      /* cover | contain | 100% auto */
  background-position: center;
  background-repeat: no-repeat;
}

Gradients

/* Linear gradient */
.banner {
  background: linear-gradient(135deg, #D62828, #5B3FEF);
}

/* Left to right */
.fade {
  background: linear-gradient(to right, #000, transparent);
}

/* Radial gradient */
.glow {
  background: radial-gradient(circle, #5B3FEF, #111);
}

Shorthand

.hero {
  background: url("photo.jpg") center/cover no-repeat #111;
}
Always pair background-image with a background-color fallback — shown while the image loads or if it fails.
🧩

Test Yourself

4 questions
Q1.Which background-size value makes the image cover the full element?
Q2.Which CSS function creates a linear color gradient?
Q3.Which property stops a background image from tiling?
Q4.Which value centers a background image?