CSS Backgrounds Guide

Backgrounds in CSS allow you to add colors, images, gradients, or a combination of these to enhance the visual appearance of elements on your website. This guide covers various background properties and techniques.

← Back to Home

1. Background Color

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

Solid Color
.bg-color {
  background-color: #3498db;
}
Color with Alpha Transparency
.bg-color-alpha {
  background-color: rgba(52, 152, 219, 0.5);
}

Color Formats

/* Different ways to specify background colors */
.bg-hex {
  background-color: #3498db;
}

.bg-rgb {
  background-color: rgb(52, 152, 219);
}

.bg-rgba {
  background-color: rgba(52, 152, 219, 0.5);
}

.bg-hsl {
  background-color: hsl(204, 70%, 53%);
}

.bg-hsla {
  background-color: hsla(204, 70%, 53%, 0.5);
}

.bg-named-color {
  background-color: dodgerblue;
}

2. Background Gradients

CSS gradients let you display smooth transitions between two or more specified colors:

Linear Gradient
.bg-linear-gradient {
  background-image: linear-gradient(to right, #3498db, #2ecc71);
}
Radial Gradient
.bg-radial-gradient {
  background-image: radial-gradient(circle, #3498db, #2ecc71);
}

More Gradient Options

/* Angled linear gradient */
.bg-angled-gradient {
  background-image: linear-gradient(45deg, #3498db, #2ecc71);
}

/* Multi-color gradient */
.bg-multi-color-gradient {
  background-image: linear-gradient(to right, #3498db, #2ecc71, #f1c40f, #e74c3c);
}

/* With color stops */
.bg-color-stops {
  background-image: linear-gradient(to right, 
    #3498db 0%, 
    #3498db 20%, 
    #2ecc71 20%, 
    #2ecc71 100%);
}

/* Conic gradient */
.bg-conic-gradient {
  background-image: conic-gradient(#3498db, #2ecc71, #f1c40f, #e74c3c, #3498db);
}

3. Background Images

The background-image property sets one or more background images for an element:

Basic Background Image
.bg-image {
  background-image: url('image.jpg');
  background-size: cover;
  background-position: center;
}
Image with Overlay
Text Over Image
.bg-multi {
  background: 
    linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
    url('image.jpg');
  background-size: cover;
  background-position: center;
}

4. Background Repeat

The background-repeat property defines how background images are repeated:

repeat (default)
.bg-image-repeat {
  background-image: url('pattern.jpg');
  background-repeat: repeat; /* Default */
}
repeat-x
.bg-image-repeat-x {
  background-image: url('pattern.jpg');
  background-repeat: repeat-x;
}
no-repeat
.bg-image-no-repeat {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: center;
}

Other Repeat Values

/* Repeat only vertically */
.bg-repeat-y {
  background-repeat: repeat-y;
}

/* Space images evenly */
.bg-repeat-space {
  background-repeat: space;
}

/* Repeat and round to fit */
.bg-repeat-round {
  background-repeat: round;
}

5. Background Position

The background-position property sets the starting position of a background image:

top left
.bg-position-top-left {
  background-position: top left;
}
center
.bg-position-center {
  background-position: center;
}
bottom right
.bg-position-bottom-right {
  background-position: bottom right;
}

Using Numeric Values

/* Using percentages */
.bg-position-percent {
  background-position: 25% 75%;
}

/* Using pixels */
.bg-position-px {
  background-position: 50px 100px;
}

6. Background Size

The background-size property specifies the size of the background images:

cover
.bg-size-cover {
  background-image: url('image.jpg');
  background-size: cover;
  background-position: center;
}
contain
.bg-size-contain {
  background-image: url('image.jpg');
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}

Using Numeric Values

/* Using pixels */
.bg-size-px {
  background-size: 200px 100px;
}

/* Using percentages */
.bg-size-percent {
  background-size: 50% 50%;
}

/* Setting just width (height auto) */
.bg-size-auto-height {
  background-size: 50% auto;
}

7. Background Attachment

The background-attachment property sets whether a background image scrolls with the rest of the page or remains fixed:

scroll (default)
.bg-attachment-scroll {
  background-image: url('image.jpg');
  background-attachment: scroll; /* Default */
  background-size: cover;
}
fixed (parallax effect)
.bg-attachment-fixed {
  background-image: url('image.jpg');
  background-attachment: fixed;
  background-size: cover;
}

Local Attachment

/* Scrolls with element content */
.bg-attachment-local {
  background-attachment: local;
}

8. Background Clip & Origin

The background-clip property defines how far the background should extend:

border-box (default)
border-box
.bg-clip-border {
  background-color: #3498db;
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-clip: border-box;
}
padding-box
padding-box
.bg-clip-padding {
  background-color: #3498db;
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-clip: padding-box;
}
content-box
content-box
.bg-clip-content {
  background-color: #3498db;
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-clip: content-box;
}

Background Origin

The background-origin property specifies where the background image is positioned:

border-box
border-box
.bg-origin-border {
  background-image: url('image.jpg');
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-origin: border-box;
}
padding-box (default)
padding-box
.bg-origin-padding {
  background-image: url('image.jpg');
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-origin: padding-box;
}
content-box
content-box
.bg-origin-content {
  background-image: url('image.jpg');
  border: 15px dashed #2c3e50;
  padding: 20px;
  background-origin: content-box;
}

9. CSS Background Patterns

You can create interesting patterns using CSS backgrounds without external images:

Striped Pattern
.bg-pattern-stripes {
  background-image: 
    linear-gradient(45deg, #3498db 25%, transparent 25%),
    linear-gradient(-45deg, #3498db 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #3498db 75%),
    linear-gradient(-45deg, transparent 75%, #3498db 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}
Polka Dot Pattern
.bg-pattern-polka {
  background-color: #3498db;
  background-image: radial-gradient(#ffffff 10%, transparent 11%),
                    radial-gradient(#ffffff 10%, transparent 11%);
  background-size: 30px 30px;
  background-position: 0 0, 15px 15px;
}
Grid Pattern
.bg-pattern-grid {
  background-image:
    linear-gradient(#3498db 1px, transparent 1px),
    linear-gradient(90deg, #3498db 1px, transparent 1px);
  background-size: 20px 20px;
}

10. Multiple Backgrounds

CSS allows you to specify multiple background layers, separated by commas:

/* Multiple background layers */
.multiple-backgrounds {
  background: 
    url('pattern.png'),
    linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
    url('main-image.jpg');
  background-position: 
    center,
    center,
    center;
  background-size: 
    auto,
    cover,
    cover;
  background-repeat: 
    repeat,
    no-repeat,
    no-repeat;
}

/* Note: Background layers are stacked with the first one on top */

11. Background Shorthand

The background shorthand property lets you set multiple background properties at once:

/* Background shorthand */
.background-shorthand {
  background: #3498db url('image.jpg') no-repeat center/cover fixed;
}

/* Equivalent to: */
.background-longhand {
  background-color: #3498db;
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  background-attachment: fixed;
}

The order of values in the shorthand property is:

  1. background-color
  2. background-image
  3. background-repeat
  4. background-attachment
  5. background-position / background-size (with slash between them)

12. Best Practices

← Back to Home