CSS: Backgrounds & Images

Control every aspect of backgrounds and image presentation.

background-color

background-color fills the content and padding area of an element with a solid color. It accepts any CSS color value.

background-color

background-color fills the element's background with a solid color. It sits behind background-image when both are set.

  • Accepts any CSS color: named, hex, rgb(), hsl()
  • transparent, the default value; no background color
  • Always pair a background-color with text to ensure readable contrast
  • background-color shows through if background-image fails to load

background-color

Named, hex, and hsl color values for backgrounds.

background-image

background-image sets an image or gradient as the background. It accepts a url() for image files or gradient functions like linear-gradient().

background-image

background-image layers on top of background-color. If the image fails to load, the background-color shows as a fallback.

  • url('image.jpg'), loads an image file as the background
  • linear-gradient(), generates a color gradient as the background
  • none, removes any background image
  • Always set a background-color alongside background-image as a fallback

background-image

Gradient as background-image; background-color as fallback.

background-repeat

By default, background images tile to fill the element. background-repeat controls whether and how tiling happens.

background-repeat

background images tile in both directions by default. Use no-repeat for photos and hero images; repeat for patterns and textures.

  • repeat, tiles in both directions (default)
  • no-repeat, shows the image only once
  • repeat-x, tiles only horizontally
  • repeat-y, tiles only vertically
  • space, tiles without clipping, adding space between copies

background-repeat

no-repeat shows the image once; repeat-x tiles it horizontally.

background-position

background-position sets where the background image is placed within the element. Used together with no-repeat to control the focal point of the image.

background-position

background-position takes keywords, percentages, or pixel values. Two values set horizontal and vertical position respectively.

  • center, centers the image both horizontally and vertically
  • top right, places image at the top-right corner
  • 50% 50%, same as center using percentages
  • 20px 10px, 20px from left, 10px from top
  • Combined with background-size: cover for hero sections: center the image and crop equally

background-position

Top-left, center, and bottom-right positioning.

background-size

background-size controls how large the background image is rendered inside the element.

background-size

cover and contain are the two most useful values. cover fills the box completely (may crop); contain fits the whole image inside (may leave gaps).

  • cover, scales image to fill the container; may crop edges
  • contain, scales image to fit fully inside the container; may leave empty space
  • auto, default; displays image at its natural size
  • 200px 100px, explicit width and height
  • 50%, relative to the container size

background-size: cover vs contain

cover crops to fill; contain fits the whole image.

background-attachment

background-attachment controls whether the background image scrolls with the page or stays fixed to the viewport.

background-attachment

fixed creates a parallax-like effect where the background stays in place while the page content scrolls over it.

  • scroll, default; background moves with the element when the page scrolls
  • fixed, background is fixed to the viewport; content scrolls over it
  • local, background scrolls with the element's own scroll, not the page
  • background-attachment: fixed is a simple way to create a parallax hero effect
  • fixed may have performance issues on mobile, use scroll as a fallback

background-attachment: fixed

Background stays fixed to the viewport while content scrolls.

Multiple Backgrounds

CSS allows multiple background layers on a single element by separating values with commas. The first layer listed is on top; the last is at the bottom.

Multiple Backgrounds

Multiple backgrounds are comma-separated. Each layer can have its own image, position, size, and repeat. The last background-color (set separately) is always the bottommost layer.

  • Layers are stacked: first value in the list = topmost layer
  • Each layer can have independent position, size, and repeat settings
  • background-color is always the bottom-most layer regardless of order
  • Useful for overlaying gradients on top of images

Multiple Backgrounds

A dark gradient overlay layered on top of a repeating pattern.

Linear Gradients

linear-gradient() creates a smooth transition between two or more colors along a straight line.

linear-gradient()

The first argument is the direction (to right, to bottom, or an angle like 135deg). The remaining arguments are color stops.

  • to right, left to right; same as 90deg
  • to bottom, top to bottom; the default direction
  • 135deg, diagonal angle
  • Color stops, two or more colors; optional percentage positions
  • repeating-linear-gradient(), repeats the gradient pattern to fill the element

linear-gradient()

Horizontal, diagonal, and multi-stop linear gradients.

Radial Gradients

radial-gradient() creates a gradient that radiates outward from a center point in a circular or elliptical shape.

radial-gradient()

radial-gradient() starts from a center point and spreads outward. The shape can be circle or ellipse, and you can set the center position.

  • circle, perfectly round gradient
  • ellipse, default; stretches to fit the element
  • at center, sets the gradient origin position
  • closest-side / farthest-corner, keywords controlling how far the gradient extends
  • repeating-radial-gradient(), repeats the pattern outward

radial-gradient()

Ellipse and circle radial gradients with different origins.

Images: object-fit and object-position

When an <img> has a fixed width and height, object-fit controls how the image fills that space, just like background-size does for background images.

object-fit / object-position

object-fit is the inline image equivalent of background-size. object-position moves the image within its box, like background-position.

  • cover, fills the box, may crop; maintains aspect ratio
  • contain, fits fully inside the box; may leave empty space
  • fill, stretches to fill the box; distorts aspect ratio
  • none, displays at natural size; may overflow or leave gaps
  • object-position: top, shifts which part of the image is visible inside the box

object-fit

cover fills the fixed box; contain fits the whole image inside.

Responsive Images (max-width: 100%)

By default, images display at their natural size and can overflow their container on small screens. One CSS rule makes any image fully responsive.

Responsive Images

Setting max-width: 100% ensures images never exceed their container width. Combined with height: auto, the image scales proportionally.

  • max-width: 100%, image shrinks to fit its container but never grows beyond natural size
  • height: auto, maintains the aspect ratio as width changes
  • width: 100%, forces the image to always fill the full container width
  • Add these rules to img in your CSS reset to make all images responsive by default
  • Use width: 100% for images that should always fill their column; max-width: 100% for images that should only shrink

Responsive Image

max-width: 100% prevents the image from overflowing its container.

aspect-ratio for Images

aspect-ratio locks the width-to-height ratio of an element. When one dimension is set, the other is calculated automatically, perfect for image placeholders and media embeds.

aspect-ratio

aspect-ratio prevents layout shift by reserving space for images before they load. It also ensures consistent image sizes in a grid without hardcoding heights.

  • aspect-ratio: 16 / 9, widescreen video ratio
  • aspect-ratio: 1, square (same as 1 / 1)
  • aspect-ratio: 4 / 3, traditional photo ratio
  • Set width and aspect-ratio, height is computed automatically
  • Combine with object-fit: cover for uniform image cards in a grid

aspect-ratio

All images share the same 4/3 ratio regardless of their natural dimensions.

Knowledge Check

1. Which background-size value scales the image to fully cover the container, possibly cropping it?

2. What does background-repeat: no-repeat do?

3. Which CSS property controls how an <img> fills its container similar to background-size: cover?

4. What does background-attachment: fixed do?

5. Which CSS property maintains an element's width-to-height ratio automatically?