CSS Backgrounds

1. Background Color

The background-color property sets the background color of an element. You can use color names, HEX codes, RGB, RGBA, HSL, or HSLA values to define the color.

CSS

You can also apply transparency using RGBA or HSLA:

CSS




Tutorials dojo strip

2. Background Image

To use an image as the background of an element, you can apply the background-image property. Make sure to specify the image URL.

CSS




3. Background Repeat

By default, a background image repeats itself to fill the element. The background-repeat property allows you to control this behavior.

  • repeat: The default behavior, repeating the image both horizontally and vertically.
  • no-repeat: Prevents the image from repeating.
  • repeat-x: Repeats the image only horizontally.
  • repeat-y: Repeats the image only vertically.
CSS




4. Background Position

The background-position property sets the starting position of the background image. You can define it using keywords (e.g., top, center, bottom) or coordinates (e.g., 50% 50%).

CSS

Coordinates allow more precise positioning:

CSS




5. Background Size

The background-size property controls the size of the background image. You can specify the size using pixels, percentages, or keywords.

  • cover: Scales the image to cover the entire element (may crop parts of the image).
  • contain: Scales the image to fit inside the element (may leave empty space).
CSS

You can also define custom sizes:

CSS




6. Background Attatchment

The background-attachment property controls whether the background scrolls with the page or remains fixed.

  • scroll: The background moves when the user scrolls the page (default).
  • fixed: The background stays fixed in place.
  • local: The background scrolls with the content inside the element.
CSS




7. Background Gradient

CSS also allows you to create gradients as backgrounds using the linear-gradient and radial-gradient functions.

  • Linear Gradient:
CSS
  • Radial Gradient:
CSS




8. Shorthand for Background Properties

You can combine multiple background properties into one shorthand declaration. This shorthand sets the background image, no-repeat, center position, and cover size all in one line.

CSS




CSS Backgrounds Example Code

CSS




CSS Labs

Tutorials dojo strip