CSS Backgrounds

Learn how to style element backgrounds using CSS background properties

What are CSS Backgrounds?

CSS background properties allow you to control the background appearance of elements. You can set background colors, images, gradients, and control how they are positioned, repeated, and sized.

Background Properties:

  • background-color - Sets the background color of an element
  • background-image - Sets one or more background images
  • background-repeat - Defines how background images are repeated
  • background-position - Sets the starting position of a background image
  • background-size - Specifies the size of background images
  • background-attachment - Sets whether background images are fixed or scroll
  • background-origin - Specifies the positioning area of background images
  • background-clip - Specifies the painting area of background
  • background-blend-mode - Defines the blending mode of background layers
  • background - Shorthand for all background properties
Color
Image
Gradient
Pattern
CSS Backgrounds

CSS Background Properties Reference

PropertyDescriptionValues
background-colorSets the background color of an elementcolor | transparent
background-imageSets one or more background imagesnone | url() | gradient
background-repeatDefines how background images are repeatedrepeat | repeat-x | repeat-y | no-repeat | space | round
background-positionSets the starting position of a background imagetop | bottom | left | right | center | length | %
background-sizeSpecifies the size of background imagesauto | cover | contain | length | %
background-attachmentSets whether background images are fixed or scrollscroll | fixed | local
background-originSpecifies the positioning area of background imagespadding-box | border-box | content-box
background-clipSpecifies the painting area of backgroundborder-box | padding-box | content-box | text
background-blend-modeDefines the blending mode of background layersnormal | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity
backgroundShorthand for all background properties[color] [image] [position] / [size] [repeat] [attachment] [origin] [clip]

CSS Backgrounds in Action

Example Code

/* CSS Background Properties */

/* Background color */
.element {
  background-color: #3498db;
  background-color: rgba(52, 152, 219, 0.8); /* With transparency */
  background-color: hsl(204, 70%, 53%); /* HSL format */
}

/* Background image */
.element {
  background-image: url('image.jpg');
  background-image: linear-gradient(to bottom, #3498db, #2c3e50);
  background-image: radial-gradient(circle, #3498db, #2c3e50);
  background-image: conic-gradient(red, yellow, green, blue, red);
}

/* Background repeat */
.element {
  background-repeat: repeat; /* Default - repeats in both directions */
  background-repeat: repeat-x; /* Repeats horizontally only */
  background-repeat: repeat-y; /* Repeats vertically only */
  background-repeat: no-repeat; /* Does not repeat */
  background-repeat: space; /* Repeats without clipping, with even spacing */
  background-repeat: round; /* Repeats without clipping, scaling if needed */
}

/* Background position */
.element {
  background-position: center; /* Keyword values */
  background-position: top left;
  background-position: 50% 50%; /* Percentage values */
  background-position: 10px 20px; /* Length values */
}

/* Background size */
.element {
  background-size: auto; /* Default - original size */
  background-size: cover; /* Scales to cover the entire element */
  background-size: contain; /* Scales to fit within the element */
  background-size: 50% 50%; /* Specific width and height */
  background-size: 300px 200px; /* Specific dimensions */
}

/* Background attachment */
.element {
  background-attachment: scroll; /* Default - scrolls with element */
  background-attachment: fixed; /* Fixed relative to viewport */
  background-attachment: local; /* Scrolls with element's content */
}

/* Background origin */
.element {
  background-origin: padding-box; /* Default - relative to padding box */
  background-origin: border-box; /* Relative to border box */
  background-origin: content-box; /* Relative to content box */
}

/* Background clip */
.element {
  background-clip: border-box; /* Default - extends behind border */
  background-clip: padding-box; /* Extends to inside of border */
  background-clip: content-box; /* Extends to content only */
  background-clip: text; /* Clips background to text (webkit) */
}

/* Multiple backgrounds */
.element {
  background: 
    url('top-image.png') top left no-repeat,
    url('middle-image.png') center no-repeat,
    url('bottom-image.png') bottom right no-repeat,
    linear-gradient(to bottom, rgba(255,255,255,0.5), rgba(0,0,0,0.5));
}

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

/* Background blend modes */
.element {
  background-blend-mode: normal; /* Default */
  background-blend-mode: multiply;
  background-blend-mode: screen;
  background-blend-mode: overlay;
  background-blend-mode: darken;
  background-blend-mode: lighten;
  background-blend-mode: color-dodge;
  background-blend-mode: color-burn;
  background-blend-mode: hard-light;
  background-blend-mode: soft-light;
  background-blend-mode: difference;
  background-blend-mode: exclusion;
  background-blend-mode: hue;
  background-blend-mode: saturation;
  background-blend-mode: color;
  background-blend-mode: luminosity;
}

Background Properties in Detail

Background Color

The background-color property sets the background color of an element. It accepts any valid CSS color value.

.element {
background-color: #3498db;
}

Background Image

The background-image property sets one or more background images on an element. Images are drawn in the order they are declared.

.element {
background-image: url('image.jpg');
/* or */
background-image: linear-gradient(red, blue);
}

Background Repeat

The background-repeat property defines how background images are repeated. By default, background images repeat both vertically and horizontally.

.element {
background-repeat: no-repeat;
/* or */
background-repeat: repeat-x;
/* or */
background-repeat: repeat-y;
}

Background Position

The background-position property sets the initial position for each background image. The position is relative to the position layer set by background-origin.

.element {
background-position: center;
/* or */
background-position: top right;
/* or */
background-position: 50% 50%;
}

Background Size

The background-size property specifies the size of the background images. The image can be left to its natural size, stretched, or constrained to fit the available space.

.element {
background-size: cover;
/* or */
background-size: contain;
/* or */
background-size: 50% 50%;
}

Background Attachment

The background-attachment property sets whether a background image's position is fixed within the viewport, or scrolls with its containing block.

.element {
background-attachment: fixed;
/* or */
background-attachment: scroll;
/* or */
background-attachment: local;
}

Background Origin

The background-origin property specifies the background positioning area. It determines where the background is positioned relative to the box model.

.element {
background-origin: border-box;
/* or */
background-origin: padding-box;
/* or */
background-origin: content-box;
}

Background Clip

The background-clip property specifies whether an element's background extends underneath its border box, padding box, or content box, or is clipped to text.

.element {
background-clip: border-box;
/* or */
background-clip: padding-box;
/* or */
background-clip: content-box;
/* or */
background-clip: text;
}

CSS Gradients

Linear Gradient

background: linear-gradient(to right, blue, purple);

Radial Gradient

background: radial-gradient(circle, blue, purple);

Conic Gradient

background: conic-gradient(blue, purple);

Advanced Gradient Examples

/* Multiple color stops */
background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);

/* Angle direction */
background: linear-gradient(45deg, red, blue);

/* Hard color stops */
background: linear-gradient(to right, red 0%, red 50%, blue 50%, blue 100%);

/* Repeating gradient */
background: repeating-linear-gradient(45deg, red, red 10px, blue 10px, blue 20px);

Practical Background Examples

Hero Section with Background Image

.hero {
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('hero.jpg');
background-size: cover;
background-position: center;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
Hero Section

Pattern Background

.pattern {
background-color: #f0f0f0;
background-image: linear-gradient(45deg, #ccc 25%, transparent 25%),
linear-gradient(-45deg, #ccc 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #ccc 75%),
linear-gradient(-45deg, transparent 75%, #ccc 75%);
background-size: 20px 20px;
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

Text with Background Clip

.text-gradient {
background: linear-gradient(to right, #3498db, #e74c3c);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
font-size: 3rem;
font-weight: bold;
}
Gradient Text Effect
< PreviousNext >