CSS Background


CSS background properties are like tools to create backgrounds for things on a webpage.


In these sections, you'll discover different CSS background characteristics:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
  • background (shorthand property)

CSS background-color

The background-color property tells us the color of an element's background.

Using CSS, we usually describe a color by:

  • An acceptable color name - such as "red."
  • a HEX value - like "#ff0000"
  • an RGB value - like "rgb(255,0,0)"

Other Elements

In HTML code, you can pick the color for different sections' backgrounds.


Opacity / Transparency

The opacity property tells how see-through something is. It can be a number from 0.0 to 1.0. If the number is lower, that means it's more see-through:

opacity 1

opacity 0.6

opacity 0.3

opacity 0.1


Note:When you use the opacity property to make something see-through in the background of a box, everything inside that box also becomes see-through. This can make it difficult to read the text inside a box that's completely see-through.


Transparency using RGBA

If you don't want to make things inside something see-through (like what we showed before), you can use RGBA colors. This next example changes how see-through the background is, not the words on top of it.

100% opacity

60% opacity

30% opacity

10% opacity



CSS background-image

The background-image thing lets you pick a picture to use as the background of something.Normally, the picture is used over and over again to cover the whole thing.

Note: When you choose a background picture, make sure it doesn't make the text hard to read.

You can also choose a background image for certain parts of a page, such as the "p" element.


CSS background-repeat

Normally, the background-image feature makes an image repeat in both horizontal and vertical directions.

A special aspect that causes an image to appear multiple times in both sideways and up-and-down directions.

If the picture is copied side by side in a horizontal direction(background-repeat: repeat-x;), the background will appear nicer.

Tip: To make a picture repeat up and down, use background-repeat: repeat-y;


CSS background-repeat: no-repeat

The background-repeat property also tells the system to display the background image just one time.

In the example we saw, the picture behind it was put exactly where the words are. But we want to move the picture somewhere else so that it doesn't bother the words too much.


CSS background-position

The background-position property is used to specify the position of the background image.


CSS background-attachment

The background-attachment property decides if the background image should move when you scroll or stay in one place while the rest of the page moves.



CSS background - Shorthand property

You can make your code shorter by putting all the background settings into one property. This is known as a shorthand property.

When you use the shorthand property, remember that the order of the property values matters.

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

If one of the property values is not there, it's okay as long as the others are in the right order. Remember, we're not using the background-attachment property in the examples because it doesn't have a value.