agyanadda@gmail.com

CSS Backgrounds

Styling Elements 1 min read Updated Dec 1, 2026 Topic 8 of 39

Background properties style the area behind an element's content.

Property Example
background-color #f0f6ff
background-image url("photo.jpg")
background-repeat no-repeat, repeat-x
background-position center, top right
background-size cover, contain, 100px
background-attachment fixed

Shorthand

background: #eee url("bg.png") no-repeat center / cover;

Gradients

background: linear-gradient(to right, #1f386b, #4a90e2); blends colours without any image.

Example

Example — Gradient backgrounds HTML
<style>
  .box {
    height: 100px;
    margin-bottom: 10px;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
  }
  .linear { background: linear-gradient(to right, #1f386b, #4a90e2); }
  .radial { background: radial-gradient(circle, #ffb347, #ff5f6d); }
</style>

<div class="box linear">Linear gradient</div>
<div class="box radial">Radial gradient</div>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which value makes a background image fill the box (cropping if needed)?

Exercise 2 Fill in the Blank

To stop an image from tiling, use background-repeat: ___;

Exercise 3 True / False

A gradient needs an image file.

Frequently Asked Questions

cover fills the whole box and may crop the image; contain shows the whole image and may leave empty space.