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
CSS Background Properties Reference
Property | Description | Values |
---|---|---|
background-color | Sets the background color of an element | color | transparent |
background-image | Sets one or more background images | none | url() | gradient |
background-repeat | Defines how background images are repeated | repeat | repeat-x | repeat-y | no-repeat | space | round |
background-position | Sets the starting position of a background image | top | bottom | left | right | center | length | % |
background-size | Specifies the size of background images | auto | cover | contain | length | % |
background-attachment | Sets whether background images are fixed or scroll | scroll | fixed | local |
background-origin | Specifies the positioning area of background images | padding-box | border-box | content-box |
background-clip | Specifies the painting area of background | border-box | padding-box | content-box | text |
background-blend-mode | Defines the blending mode of background layers | normal | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity |
background | Shorthand 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.
Background Image
The background-image property sets one or more background images on an element. Images are drawn in the order they are declared.
Background Repeat
The background-repeat property defines how background images are repeated. By default, background images repeat both vertically and horizontally.
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.
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.
Background Attachment
The background-attachment property sets whether a background image's position is fixed within the viewport, or scrolls with its containing block.
Background Origin
The background-origin property specifies the background positioning area. It determines where the background is positioned relative to the box model.
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.