CSS Backgrounds

Discover how to style the background of HTML elements with colors, images, repeat patterns, and more.

Understanding CSS Backgrounds

CSS background properties allow you to control the background of an element, including its color, image, how the image is positioned, and how it repeats. These properties are fundamental for creating visually appealing web designs.

You can apply backgrounds to almost any HTML element, from the entire `` to individual `

`s, buttons, and more.

🎨 Design Tip

Well-chosen backgrounds can significantly impact the mood and professionalism of your website. Use them strategically to draw attention or provide visual context.

Background Color

The `background-color` property sets the solid color of an element's background. You can use named colors, hexadecimal codes, RGB values, RGBA values (including alpha for transparency), or HSL/HSLA values.

.colored-box {
    background-color: lightblue;
}
.hex-color-box {
    background-color: #FFD700; /* Gold */
}
.rgba-color-box {
    background-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
}
Example: Background Color
This div has a primary background color.
This div has a success background color.

Background Image

The `background-image` property allows you to use an image as an element's background. You provide the URL to the image file. By default, background images repeat to cover the entire element.

.image-background {
    background-image: url('../images/pattern.png');
}
/* You can also use multiple background images */
.multiple-images {
    background-image: url('image1.png'), url('image2.png');
}
Example: Background Image
Image background (repeats horizontally)

Background Repeat

The `background-repeat` property controls how a background image repeats.

  • `repeat` (default): Repeats horizontally and vertically.
  • `repeat-x`: Repeats horizontally only.
  • `repeat-y`: Repeats vertically only.
  • `no-repeat`: Displays the image only once.
  • `round`: Repeats the image to fit the area, scaling it if necessary without cropping.
  • `space`: Repeats the image as many times as possible without clipping, distributing space evenly.
.no-repeat-bg {
    background-image: url('../images/logo.png');
    background-repeat: no-repeat;
}
Example: Background Repeat
No repeat
Repeat X

Background Position

The `background-position` property sets the starting position of a background image. You can use keywords (`top`, `bottom`, `left`, `right`, `center`) or length/percentage values (e.g., `20px 30px`, `50% 50%`).

.centered-bg {
    background-image: url('../images/icon.png');
    background-repeat: no-repeat;
    background-position: center center;
}
.top-right-bg {
    background-position: top right;
}
Example: Background Position
Image centered

Background Size

The `background-size` property specifies the size of the background image.

  • `auto` (default): The background image retains its original size.
  • `cover`: Scales the background image to be as large as possible so that it completely covers the background area. Some parts of the image may not be in view.
  • `contain`: Scales the background image to be as large as possible while ensuring both its dimensions are less than or equal to the corresponding dimensions of the background positioning area. The entire image will be visible.
  • Length/Percentage values: `100px 50px`, `50% auto`.
.cover-bg {
    background-image: url('../images/large-bg.jpg');
    background-size: cover;
}
.contain-bg {
    background-image: url('../images/small-bg.png');
    background-size: contain;
}
Example: Background Size
Background size: cover
Background size: contain

Background Attachment

The `background-attachment` property determines whether a background image scrolls with its containing block or is fixed.

  • `scroll` (default): The background image scrolls with the element's content.
  • `fixed`: The background image is fixed relative to the viewport. It won't scroll with the element.
  • `local`: The background image scrolls with the element's content, but also allows it to scroll within the element if the element has scrollbars.
body {
    background-image: url('large-image.jpg');
    background-attachment: fixed;
}

💡 Tip

Using `background-attachment: fixed;` on the `body` or `html` element can create a parallax-like scrolling effect.

Background Shorthand Property

Similar to borders, you can combine multiple background properties into a single `background` shorthand property. The order of values generally matters for specific properties, but for basic usage, you can often provide them in a flexible order.

The recommended order for the shorthand property is: `background-color background-image background-repeat background-attachment background-position / background-size`

.shorthand-bg {
    background: #f0f8ff url('logo.png') no-repeat center / 50% auto fixed;
}
/* This sets:
   background-color: #f0f8ff;
   background-image: url('logo.png');
   background-repeat: no-repeat;
   background-attachment: fixed;
   background-position: center;
   background-size: 50% auto;
*/
Example: Background Shorthand
This background uses the shorthand property.