Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Backgrounds


CSS Backgrounds

CSS allows you to style the background of HTML elements using colors, images, gradients, and positioning.


Background Color

The background-color property sets the background color of an element.

Example

Using background color:

body { background-color: lightgray; }
h1 { background-color: steelblue; color: white; }
p { background-color: lightyellow; }
Try it Yourself »

Background Image

The background-image property adds an image behind the content of an element.

Example

Adding a background image:

body {
  background-image: url('background.jpg');
}
Try it Yourself »

By default, the image repeats both horizontally and vertically.


Background Repeat

Use the background-repeat property to control repetition.

  • repeat - repeats in both directions (default)
  • repeat-x - repeats horizontally
  • repeat-y - repeats vertically
  • no-repeat - shows only one image

Example

Single background image:

body {
  background-image: url('mountain.jpg');
  background-repeat: no-repeat;
}
Try it Yourself »

Background Position

Use the background-position property to control where the image appears.

Example

Positioning a background image:

body {
  background-image: url('tree.jpg');
  background-repeat: no-repeat;
  background-position: right top;
}
Try it Yourself »

Background Size

Use background-size to scale the image.

  • auto - default size
  • cover - covers the entire area
  • contain - fits the image within the element

Example

Covering the background:

body {
  background-image: url('forest.jpg');
  background-size: cover;
  background-repeat: no-repeat;
}
Try it Yourself »

Background Attachment

Use background-attachment to control if the image scrolls or stays fixed.

  • scroll - background moves with the page (default)
  • fixed - background stays fixed when scrolling
  • local - background scrolls within the element

Example

Fixed background image:

body {
  background-image: url('sky.jpg');
  background-attachment: fixed;
  background-size: cover;
}
Try it Yourself »

Multiple Backgrounds

You can set more than one background image by separating them with commas.

Example

Multiple background images:

body {
  background-image: url('pattern.png'), url('wall.jpg');
  background-repeat: repeat, no-repeat;
  background-position: left top, center;
}
Try it Yourself »

Background Shorthand

You can combine multiple background properties into one shorthand declaration.

Example

Using shorthand:

body {
  background: url('city.jpg') no-repeat center/cover fixed;
}
Try it Yourself »

Adding Text Over the Image

You can place text or other elements inside the same <div> - the background image will stay behind them.

Example

<style>
div.hero {
  background-image: url("mountains.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  height: 300px;
  color: white;
  text-align: center;
  padding-top: 100px;
}
</style>

<div class="hero">
  <h1>Welcome</h1>
  <p>Enjoy the view</p>
</div>
Try it Yourself »

Background Gradient

CSS gradients let you create smooth color transitions without images.

Linear Gradient

Example

Linear gradient background:

div {
  background: linear-gradient(to right, red, yellow);
  height: 100px;
}
Try it Yourself »

Radial Gradient

Example

Radial gradient background:

div {
  background: radial-gradient(circle, red, yellow, green);
  height: 100px;
}
Try it Yourself »

Best Practices

  • Use gradients instead of large images for performance.
  • Set background-size: cover for full-screen backgrounds.
  • Combine properties with shorthand for cleaner code.
  • Always ensure text remains readable on background images.

Next, you'll learn about CSS Borders - how to add outlines, rounded corners, and creative border effects.

Next » CSS Borders


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.