Skip to main content
Syllabus
On this page

CSS Backgrounds

Badar KhalilUpdated September 29, 2026 4 min read

What Are CSS Backgrounds?

CSS backgrounds paint the area behind an element's content and padding (and border, depending on settings). Our focus keyword is CSS backgrounds, and you will learn every background property, plus trending techniques: CSS background-size cover vs contain, multiple backgrounds, background-clip text (gradient text), background-blend-mode, CSS patterns without images, and responsive hero sections.

The Background Properties

PropertyPurposeCommon values
background-colorSolid color behind everything#fff, transparent
background-imageImage or gradient layerurl(hero.jpg), linear-gradient()
background-repeatTiling behaviorrepeat, no-repeat, repeat-x, space, round
background-positionWhere the image sitscenter, top right, 50% 20%
background-sizeImage scalingauto, cover, contain, 200px 100px
background-attachmentScroll behaviorscroll, fixed, local
background-originWhere position startspadding-box, border-box, content-box
background-clipWhere the paint is cut offborder-box, padding-box, content-box, text
background-blend-modeHow layers blendmultiply, overlay, screen

background-color

The color is painted below all image layers. Always set a background color as a fallback in case an image fails to load, and to guarantee text contrast.

background-image

Accepts url(), gradients and image-set() for high-DPI images. Use CSS backgrounds for decorative images. If the image carries meaning, use an HTML <img> with alt text instead, because backgrounds are invisible to screen readers and search engines.

background-size: cover vs contain

  • cover scales the image to fill the whole box and crops the overflow. Best for hero sections.
  • contain scales the image so it fully fits inside the box, possibly leaving empty space.
  • auto uses the natural size. Two values set width and height, for example 40px 40px.

background-position and repeat

Position accepts keywords, lengths and percentages. Use center to focus the subject. The four-value syntax right 20px bottom 10px offsets from edges. For patterns, use repeat. For a single logo, use no-repeat.

background-attachment

fixed pins the background to the viewport for a simple parallax effect. It can be janky on mobile browsers, and iOS Safari historically ignores it, so test carefully.

background-clip and origin

background-clip: text with a transparent text color creates gradient text. Add -webkit-background-clip: text for older Safari and Chrome versions. padding-box and content-box help make borders and inner frames.

Multiple Backgrounds

Separate layers with commas. The first layer is on top. Only the last layer may contain a background-color in the shorthand.

Code
.hero {
  background:
    linear-gradient(to top, rgb(0 0 0 / 70%), transparent) ,
    url(hero.jpg) center / cover no-repeat,
    #0f172a;
}

The background Shorthand

Syntax order: background: color image position / size repeat attachment origin clip. Position and size must be written with a slash between them: center / cover. Remember that the shorthand resets every unspecified sub-property to its initial value, which can accidentally erase earlier settings.

background-blend-mode

Blend an image with the background color or another layer. Example: background-color: #2563eb; background-image: url(photo.jpg); background-blend-mode: multiply; gives a duotone look, a very trendy effect.

Patterns Without Images

Gradients can draw stripes, grids and dots using tiny repeating tiles, saving HTTP requests. Combine background-size with gradients to build any pattern.

Performance and Accessibility

  • Compress images and use modern formats (WebP, AVIF). Use image-set() for retina versions.
  • Large hero backgrounds affect LCP. For above-the-fold hero images, an <img> with fetchpriority='high' is often faster than a CSS background.
  • Always ensure text over images has enough contrast, using overlay gradients.
  • Do not put essential content in background images.

Best Practices

  • Set a fallback background-color.
  • Use cover plus center for hero backgrounds.
  • Add a dark gradient overlay for readable text.
  • Use the shorthand carefully, or use longhand properties for clarity.

Common Mistakes

  • Forgetting the slash in center / cover.
  • Placing a color in a non-final layer of the shorthand.
  • Expecting a background image to show without giving the element a height.
  • Using background-attachment: fixed on mobile without testing.

Summary

Backgrounds combine color, images and gradients in stackable layers. Learn size, position, repeat, clip and blend modes, and you can build heroes, patterns and gradient text with pure CSS. Try the examples below.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Exercise: Create a Hero With an OverlayHTML

Style .hero so it is 240px tall, uses two background layers (a dark gradient overlay from bottom to transparent on top, and a blue-to-purple linear gradient below), and has a fallback background color. The heading must be white and readable.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
A 240px hero with a layered gradient and readable white text.

This is a self-check — compare your result with the expected output above.

Exercise: Make Gradient TextHTML

Use background-clip: text to give the h1 a horizontal gradient from orange to pink. Include the -webkit- prefixed version too.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
The heading text shows an orange to pink gradient.

This is a self-check — compare your result with the expected output above.

Quiz: CSS Backgrounds Quiz Pass 70%
Loading quiz…

Was this page helpful?