CSS Background Code
The following CSS background codes demonstrate the various CSS properties you can use to style the background of any HTML element.
CSS Background Color
To set the background color of an element, use the background-color property.
| Code | Result |
|---|---|
|
|
This text has a background color applied. |
CSS Background Image
To add a background image to an element, use the background-image property.
| Code | Result |
|---|---|
|
|
This text has a background image applied. |
CSS Background Repeat
Determines whether the background image repeats (tiles) or not. For info on the possible values, see background-repeat.
| Code | Result |
|---|---|
|
|
This background image does not repeat. |
CSS Background Position
Determines the position of the background image. For more information, see background-position.
| Code | Result |
|---|---|
|
|
This background image is positioned 100 pixels in from the left. |
CSS Background Attachment
Determines whether or not the background image scrolls with the outer container. For more information, see background-attachment.
| Code | Result |
|---|---|
|
|
This background image is fixed - it doesn't scroll with its outer container. This example uses the CSS overflow property to force the box to scroll when there's too much text to fit in the box. |
Shorthand Code
You can use the background property to set all the background properties at once. For more information, see background.
| Code | Result |
|---|---|
|
|
This paragraph tag has been styled using the 'background' property, which is shorthand for setting multiple properties for an HTML element. |

