Background
background
property sets an element's background.
Basic Concepts
An element can have multiple background
layers, separated by commas.
Setting background-color
will set the color of the bottom-most layer.
An element has a border-box, a padding-box, a the content-box.
(Relevant for background-origin
and background-clip
.)

Base Background Properties
background
Shorthand for the following properties (separate each layer using commas):
background-color
background-image
background-attachment
background-origin
background-clip
background-position
background-size
background-repeat
div {
background: green;
/* Using a <background-image> and <background-repeat> */
background: url('test.jpg') repeat-y;
/* Using a <background-clip> and <background-color> */
background: border-box red;
/* A single image, centered, and scaled */
/* Slash syntax: <background-position>/<background-size>*/
background: no-repeat center/80% url('/img/image.png');
}
background-color
Sets the bottom-most layer background color.
div {
background-color: red;
}
background-image
Sets background image(s).
Specify none
or images (separate multiple layers using comma).
div {
background-image: url('/res/image.jpg');
background-image: linear-gradient(to bottom, red, blue);
}
Background Config Properties
background-attachment
Sets how the browser "attaches" and scrolls with the element. Possible values:
scroll
— the background is fixed to the element's border (if you scroll through the element's content, it stays)local
— the background is to the element's content (if you scroll the through element's content, it follows)fixed
— never scrolls (even if you scroll through the whole window)
background-origin
Sets the background "origin" (where to start painting the background). Possible values:
border-box
— paint the background starting from the border-boxpadding-box
— paint the background starting from the padding-boxcontent-box
— paint the background starting from the content-box
Try changing the
background-origin
!
background-clip
Sets how to clip (mask) the background. Possible values:
border-box
— the background is visible up to the border-boxpadding-box
— the background is visible up to the padding-boxcontent-box
— the background is visible up to the content-boxtext
— the background is masked with the forground color of the text (you will need to set the transparancy of the text to make the background visible, e.g. viacolor: transparent
orcolor: rgba(...)
)
background-position
Sets the position of the background image, relative to background-origin
.
You can specify 1 to 4 values. You can use (1) a keyword (top
, bottom
, left
, right
, center
),
and/or an offset (e.g. 20%
or 12px
), max. 2 each (one for the x-axis, one for the y-axis).
div {
background-position: top;
background-position: top left;
background-position: 50% 50%;
background-position: top 20px left 20px;
}
background-size
Sets the displayed size of the background.
cover
— scales the image as large as possible to fill the container, stretching or cropping the image if necessarycontain
— scales the image as large as possible within its container without cropping or stretching the image<width> <height>
— e.g.auto auto
,auto 20px
, or50% 50%
(auto
means use the image's intrinsic width/height)
background-repeat
Sets how a background should be repeated.
You can use the one-value syntax:
repeat
— repeat on both axesrepeat-x
— repeat on the x-axis onlyrepeat-y
— repeat on the y-axis onlyno-repeat
— don't repeatspace
— repeat as much as possible, then space the background images evenlyround
— repeat as much as possible, then stretch/squish the background image to fill the remaining space
... or the two-value syntax, specifying either of the following for both the x and y axes (e.g. repeat no-repeat
): repeat
, no-repeat
, round
, space
.
Assets
- Christopher Burns (Unsplash) — https://unsplash.com/photos/dzejyfCAzIA
References
- background (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background
- background-color (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-color
- background-image (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-image
- background-attachment (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-attachment
- background-origin (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-origin
- background-clip (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
- background-position (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-position
- background-size (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
- background-repeat (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/background-repeat