Interactive online courses HTML Academy
2026-03-09 10:56 Diff

The background of an element can be set using separate properties: background-color, background-image, and so on. All of this can be quite cumbersome to code.

You can set the background using the shorthand property background, for which you can specify its components separated by spaces:

background: [bc] [bi] [br] [bp] [ba]; /* Legend: [bc] — background-color [bi] — background-image [br] — background-repeat [bp] — background-position [ba] — background-attachment */

If any component is not specified, then the default value will be used. Below are examples.

background: #e74c3c; background: url("img.png") no-repeat; background: url("img.png") 10px 20px;

In the first example, the background color is assigned simply.

In the second example, a non-repeating background image is specified together with a default transparent background color. The image is positioned in the upper left corner.

In the third example, a background image is specified together with its position. It is also assigned a default transparent background color and the specification to repeat in all directions.