CSS Backgrounds

The flexibility to manipulate the The flexibility to manipulate the background of elements with CSS is one of the most creative aspects of any web designer’s toolset. Be it a solid color, a gradient, an image, a pattern or even multiple layer, backgrounds can help communicate the look and feel of a web page.

CSS's ability to control the look of background elements is one of the most creative capacities of a web designer. Either a solid color, a lovely gradient, an image, a pattern, or multiple layers, a background can be used to create a mood or personality for a website or web page.

Common CSS Background Properties

Let’s explore the crucial properties used for controlling backgrounds:

background-color

Sets the background color of an element.


     div {
         background-color: lightblue ;
     }
            

You can use color names, HEX colors, RGB, HSL or even CSS variables.

background-image

This property sets an image to be the background of an element.


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

However, it will repeat by default if the image is smaller than the element.

background-repeat

Controls whether and how the background image repeats.


        div {
            background-repeat: no-repeat;
        }
            

Options include:

background-position

Sets the starting position of the background image.


    div {
        background-position: center center;
    }
        

Examples: top left, right bottom, center, pixel values, percentages.

background-size

Defines the size of the background image.


    div {
        background-size: cover;
    }
            

Common values:

background-attachment

Controls whether the background image scrolls with the page or stays fixed.


    div {
        background-attachment: fixed;
    }
     
            

Values:

background (Shorthand)

You can combine all background properties into one using the shorthand property:


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

This saves time and keeps your CSS clean and efficient. The order of values matters for some properties.

Using Multiple Backgrounds

CSS allows applying more than one background image:


     div {
         background-image: url('pattern.png'), url('bg.jpg');
         background-repeat: repeat, no-repeat;
         background-position: top left, center;
         background-size: auto, cover;
     }
            

Layering backgrounds adds depth and complexity to your designs.

CSS Gradients as Backgrounds

Gradients are smooth transitions between two or more colors and can replace background images.

Linear Gradient


     div {
        background: linear-gradient(to right, #00f, #0ff) ;
     }
            

Radial Gradient


     div {
         background: radial-gradient(circle, red, yellow, green);
     }
            

No need for images — gradients are light and scalable.

Using Backgrounds Responsively

Use background-size: cover and background-position: center to ensure the background looks good on all screen sizes.


     div {
        background: url('banner.jpg') no-repeat center center ;
        background-size: cover;
     }
            

If you are able to use media queries to control backgrounds associated with various formats of displays.

Stylish Practices for CSS Backgrounds

Conclusion: Make Backgrounds Help your Design

CSS Backgrounds can offer incredible creative flexibility—not limited to simple color fills, but also to complex stacked images and gradients as well, all of which help establish a tone in your designs, designate focal areas of the page, and provide rhythm visually for your website.

If you understand your background properties, you will very quickly transition from prescribed blank spaces, to incredible, visual deliverables.

Try it Yourself