CSS Docs

CSS Backgrounds

Learn how to use CSS background properties to style your web pages. This guide covers background colors, images, sizes, positions, and advanced techniques like parallax scrolling.

Backgrounds are an essential part of web design. CSS allows you to style backgrounds using various properties to create visually appealing and dynamic layouts. This guide will take you through the basics and advanced background techniques.

🖼️ What are CSS Backgrounds?

CSS background properties are used to set the background of an element. These properties can control background colors, images, positioning, sizing, and even advanced techniques like parallax scrolling. Properly managing backgrounds can greatly enhance the visual appeal of a webpage.


🎨 Background Properties

background-color

This property sets the background color of an element.

div {
  background-color: #f0f0f0;
}

background-image

This property sets an image as the background. You can use it with a URL to link to an image file.

div {
  background-image: url('path-to-image.jpg');
}

background-size

This property controls the size of the background image. It can take values like cover, contain, or specific pixel values.

  • cover: Scales the background image to cover the entire element, possibly cutting off parts of the image.
  • contain: Scales the background image to fit the entire element, maintaining its aspect ratio.
div {
  background-size: cover;
}

background-position

This property sets the position of the background image. It can take values such as top, center, bottom, or specific pixel/percentage values.

div {
  background-position: center;
}

Combining Background Properties

You can combine multiple background properties into a single background shorthand rule:

div {
  background: #f0f0f0 url('image.jpg') no-repeat center/cover;
}

In this example:

  • The background-color is #f0f0f0.
  • The background-image is set to 'image.jpg'.
  • The no-repeat value ensures the image does not repeat.
  • The center/cover part defines the position and size.

🔢 Multiple Backgrounds

CSS allows you to set multiple background images on a single element. You can separate each background with commas.

Syntax:

div {
  background-image: url('image1.jpg'), url('image2.jpg');
  background-position: top left, bottom right;
  background-size: 50%, 100%;
}

In this example:

  • The first image is positioned at the top left and is sized at 50% of the element's size.
  • The second image is positioned at the bottom right and covers the full size of the element.

You can layer background images in this way to create complex visual effects.


🌟 Parallax Scrolling with Background Images

Parallax scrolling is a technique where the background image moves at a different speed than the page content as the user scrolls. This creates a 3D-like effect.

CSS for Parallax Effect:

.parallax {
  background-image: url('background.jpg');
  height: 100vh;
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

Explanation:

  • background-attachment: fixed makes the background image stay in place while the content scrolls.
  • background-size: cover ensures that the background image covers the entire viewport.
  • background-position: center centers the image.

HTML Example:

<div class="parallax">
  <h1>Welcome to Parallax Scrolling</h1>
</div>

🧑‍🎨 Key Takeaways

Use background properties like background-color, background-image, background-size, and background-position to control the appearance of your element's background.


🧪 Try Yourself

// No additional functionality needed for CSS background effects.
console.log('CSS Backgrounds Example Loaded');