CSS Backgrounds
background-color Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-color | Used to change the background color behind text and potentially behind an entire region of the page, if applied to a block-level tag.
Applying this to body changes the background color for the entire browser window.
|
|
The same result can be achieved more efficiently with the background shorthand property, so that is preferred.
Typically you should pair a background color with a text color, if that area of the layout is expected to contain text content. Or you can rely on color inheritance from |
background-color Example
See the Pen background-color by Jason Withrow (@jwithrow) on CodePen.
background-image Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-image | Used to insert an image as a background for an element (or for the entire browser window, if applied to body).
Background images have no effect on the height/width of the element they are applied to, so users will only see as much of the background image as the element's height and width allow. |
|
Only make backgrounds of non-essential images, because CSS support could be altered or disabled and then the image would not be there.
Background images will not print by default. Essential images should be coded into the document via the <img /> element.
|
background-image Example
See the Pen background-image by Jason Withrow (@jwithrow) on CodePen.
The smile.png background image is:

background-repeat Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-repeat | Instructs a background image whether it should tile/repeat and, if so, in what direction (x-axis which is horizontal or y-axis which is vertical). |
|
For many background images you will want to instruct them to no-repeat, if nothing else, in order to prevent them from appearing multiple times should the element that they are applied to become larger. |
no-repeat Example
See the Pen background-repeat by Jason Withrow (@jwithrow) on CodePen.
repeat-x Example
See the Pen background-repeat: repeat-x by Jason Withrow (@jwithrow) on CodePen.
repeat-y Example
See the Pen background-repeat: repeat-y by Jason Withrow (@jwithrow) on CodePen.
background-position Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-position | Used to place the background image within the specified element (or within the browser window, if applied to body). |
Keyword Values:
|
If applying the background image to body then for cross-browser compatibility set html to a height of 100% to render the background in the correct place. |
Top Right Background Position Example
See the Pen background-position: top right by Jason Withrow (@jwithrow) on CodePen.
Percentage-Based Background Position Example 1
See the Pen background-position: percentage position example 1 by Jason Withrow (@jwithrow) on CodePen.
Percentage-Based Background Position Example 2
See the Pen background-position: percentage position example 2 by Jason Withrow (@jwithrow) on CodePen.
background-attachment Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-attachment | Determines whether a background image scrolls with the document or remains stationary. |
|
Watch out for the image intersecting with text and compromising the text's readability. |
background-attachment Example
See the Pen background-attachment by Jason Withrow (@jwithrow) on CodePen.
Be sure to scroll the rendered output to see the background image remain in the same place relative to the window.
background-size Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background-size | Sets the dimensions of the background image. |
|
Most of the time this is set to cover, because usually you want the background image filling the entire element.
Raster images (jpg, gif, png, webp, etc.) will have some distortion if they are stretched from smaller to larger sizes, as the rendering is guessing at the pixel values. |
background Shorthand Property
| Property | Usage and Effect | Values Accepted | Recommendations |
|---|---|---|---|
| background | Combines the other background-related properties into one sequence. |
Sequence of Values:
|
Highly recommended and should be used instead of background-color.
Makes code more efficient and also sets all properties not listed to their defaults (so be aware that you might be overriding some other non-default styling for the element elsewhere in your CSS with a default). The upside is that falling back to a default eliminates concerns about user settings or browser settings that might negatively impact rendering. Pair a background color with a text color in cases where there is no higher-level text color being inherited and text content will be involved. |
background Shorthand Example
See the Pen background shorthand by Jason Withrow (@jwithrow) on CodePen.
Multiple Background Images on an Element
- If the
backgroundshorthand is used, commas separate each block of shorthand code. - If the individual (non-shorthand) properties are used, then the
background-imageurls are separated by commas and thebackground-positionvalues are also separated by commas, and so on through all the various individual background-related properties.The browser matches the url to its position based on the sequence of values.
background-origin and background-clip Properties
| Property | Usage and Effect | Values Accepted |
|---|---|---|
| background-origin | Determines the reference point for the background-position placement. |
|
| background-clip | Determines whether a background extends into the border box. |
|
background-origin, background-clip, and background-size Example
See the Pen background-origin, background-cip, and background-size by Jason Withrow (@jwithrow) on CodePen.