CSS Backgrounds

CSS backgrounds are used to control the background appearance of HTML elements. They allow you to add colors, images, gradients, and control how backgrounds are positioned and repeated.

Background Color

The background-color property sets the background color of an element.

Example
Try this code

Background Image

The background-image property is used to add an image as a background.

background-image: url("image.jpg");
Example
Try this code

Background Repeat

By default, the background-repeat property repeats an image both horizontally and vertically to cover the entire element.

Example
Try this code

Repeat Horizontally

If you want an image to repeat only from left to right, use the repeat-x value.

Example
Try this code

Repeat Vertically

If you want an image to repeat only from top to bottom, use the repeat-y value.

Example
Try this code

No Repeat

Use no-repeat if you want to show the background image only once without any repetition.

Example
Try this code

Background Position

The background-position property defines the starting position of a background image.

Left Position

The left value positions the background image on the left side of the element.

Example
Try this code

Right Position

The right value positions the background image on the right side of the element.

Example
Try this code

Top Position

The top value positions the background image at the top of the element.

Example
Try this code

Bottom Position

The bottom value positions the background image at the bottom of the element.

Example
Try this code

Center Position

The center value positions the background image exactly in the middle of the element.

Example
Try this code
Note: If you only specify one keyword, the other side will automatically be set to 'center'. For example, if you set 'left', it will be horizontally left and vertically center.

Top Right Position

The top right value positions the background image at the top-right corner of the element.

Example
Try this code

Bottom Left Position

The bottom left value positions the background image at the bottom-left corner of the element.

Example
Try this code

Background Size

The background-size property is used to specify the size of the background image. There are three most common ways to use it:

The cover Keyword

The cover value scales the background image so that the entire container is completely covered by the image. It may crop some parts of the image to ensure no empty space is left.

Example
Try this code

The contain Keyword

The contain value scales the image to the largest size such that both its width and height can fit inside the container. This ensures the entire image is visible, but it might leave some empty space in the container.

Example
Try this code

Using Percentage or Pixels

You can also set the exact width and height of the background image manually using pixels (px) or percentage (%). The first value is for width and the second is for height.

Example
Try this code

Background Attachment

The background-attachment property specifies whether the background image should scroll with the rest of the page, or be fixed.

Background Attachment Fixed

The fixed value specifies that the background image should not scroll with the rest of the page. It stays in the same position even when you scroll down.

Example
Try this code

Background Attachment Scroll

The scroll value is the default setting. It specifies that the background image will scroll along with the rest of the page content.

Example
Try this code

Shorthand Background Property

Multiple background properties can be written using a single shorthand property.

Example
Try this code

Multiple Backgrounds

CSS allows you to apply multiple background images to a single element.

.card {
  background-image: url("layer1.png"), url("layer2.png");
}

Background Gradients

CSS gradients allow smooth transitions between two or more colors without using images.

.gradient {
  background: linear-gradient(to right, #00c6ff, #0072ff);
}

Radial Gradient

Radial gradients spread outward from a center point.

.circle {
  background: radial-gradient(circle, #ffcc00, #ff6600);
}

Best Practices for Backgrounds

Common Mistakes with Backgrounds

Tip: Use background-size: cover; for full-width responsive background images.