Premium CSS Course

CSS Backgrounds

Backgrounds are one of the most important visual elements in web design. CSS allows you to set various background properties, including colors, images, and positioning. Understanding how to use backgrounds effectively will improve the look and feel of your website.

Background Color

Setting a background color is simple in CSS and can be done using the background-color property. You can define the color using color names, HEX, RGB, RGBA, HSL, or HSLA.


        div {
            background-color: #f0f0f0; /* HEX */
            color: #333; /* Text color */
        }
    

This example sets a light grey background color and dark text color.

Background Image

You can add background images using the background-image property. The image is usually defined by the URL pointing to the image file.


        div {
            background-image: url('background.jpg');
            background-size: cover; /* Ensures the image covers the entire element */
            background-position: center; /* Centers the image */
        }
    

In this example, the background image will cover the entire div element, and it will be centered.

Background Repeat

By default, background images repeat both horizontally and vertically to fill the element. You can control this behavior using the background-repeat property:


        div {
            background-image: url('background.jpg');
            background-repeat: no-repeat; /* Prevents the image from repeating */
            background-position: top right; /* Position the image in the top right */
        }
    

Background Position

The background-position property defines the starting position of the background image. You can specify values using keywords (like top, center, or bottom), or using specific lengths or percentages.


        div {
            background-image: url('background.jpg');
            background-position: top left; /* Positioned at the top left of the element */
        }
    

This positions the image in the top-left corner of the element.

Background Size

The background-size property allows you to control the size of the background image. Common values include:


        div {
            background-image: url('background.jpg');
            background-size: cover; /* The image covers the entire div */
        }
    

Multiple Backgrounds

CSS allows you to use multiple background images on a single element. This can be achieved by separating each background with a comma in the background-image property.


        div {
            background-image: url('background1.jpg'), url('background2.jpg');
            background-position: top left, bottom right;
            background-repeat: no-repeat;
        }
    

This example sets two background images, one positioned at the top-left and the other at the bottom-right.

Background Attachment

The background-attachment property controls whether the background image scrolls with the page or remains fixed when the user scrolls.


        div {
            background-image: url('background.jpg');
            background-attachment: fixed; /* The image will stay fixed while the page scrolls */
        }
    

Shorthand Property for Background

To define multiple background properties in a more concise way, you can use the shorthand background property. This combines background-color, background-image, background-repeat, background-position, and background-size into one declaration.


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

Best Practices for Using Backgrounds

Example of CSS Backgrounds

Here’s an example that combines multiple background techniques:


        div {
            background: linear-gradient(to right, #ff7e5f, #feb47b), url('background.jpg');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
        }
    

This example uses a linear gradient with a background image and sets the image to cover the element and remain fixed on scroll.