Tutorials CSS Backgrounds
Ch 16 / 60
Chapter 16 Beginner 8 min read CSS

CSS Backgrounds

Backgrounds do far more than fill an element with colour. CSS gives you precise control over images, gradients, tiling, positioning, sizing and even multiple layered backgrounds — all without a single image editor.

What you'll learn
background-color — solid and transparent fills
background-image — URLs and gradients
background-repeat, background-position
background-size — cover, contain and custom
background-attachment, background-origin, background-clip
The background shorthand and multiple backgrounds

background-color

background-color fills an element with a solid colour. It renders behind any background images. It accepts any CSS colour value — named, hex, rgb, hsl, or transparent.

#264de4
rgb(34,197,94)
hsl(270,80%,60%)
rgba(…,.25)
oklch(…)
transparent
currentColor
inherit
CSS — background-color
.card       { background-color: #fff; }
.highlight  { background-color: rgba(38, 77, 228, 0.08); }
.danger     { background-color: hsl(0, 90%, 95%); }
.dark       { background-color: #0f172a; }
.transparent{ background-color: transparent; }  /* default */

/* background-color renders BEHIND background-image */
.with-image {
  background-color: #264de4;        /* fallback if image fails */
  background-image: url(hero.jpg);
}

/* CSS Custom Properties for theming */
:root { --surface: #ffffff; }
.card { background-color: var(--surface); }

background-image

background-image sets an image or gradient as the element's background. It accepts a url() pointing to an image file, or any CSS gradient function.

linear-gradient
linear-gradient
radial-gradient
radial-gradient
conic-gradient
conic-gradient
repeating
repeating-linear
ellipse
elliptical radial
overlay
overlay layer
CSS — background-image
/* URL image */
.hero     { background-image: url('/images/hero.jpg'); }

/* Gradient — no URL needed */
.gradient {
  background-image: linear-gradient(135deg, #264de4, #a855f7);
}

/* Multiple backgrounds — first value is on TOP */
.overlay  {
  background-image:
    linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)),
    url('/images/hero.jpg');
}

/* Remove background image */
.no-image { background-image: none; }
Always set a background-color fallback
When using background-image: url(…), always also set background-color as a fallback. If the image fails to load (slow network, 404, blocked), the colour still renders — keeping text readable.

background-repeat

background-repeat controls whether and how a background image tiles across the element. By default images repeat in both directions.

repeat (default)
no-repeat
repeat-x (horizontal)
repeat-y (vertical)
space (even gaps)
round (rescales)
CSS — background-repeat
.tiled    { background-repeat: repeat; }     /* default */
.once     { background-repeat: no-repeat; }  /* show once */
.horiz    { background-repeat: repeat-x; }   /* horizontal only */
.vert     { background-repeat: repeat-y; }   /* vertical only */
.spaced   { background-repeat: space; }     /* evenly distributed, no clip */
.rounded  { background-repeat: round; }     /* rescales to fill evenly */

/* Two values: horizontal vertical */
.mixed    { background-repeat: repeat no-repeat; } /* x=repeat y=no-repeat */

background-position

background-position controls where a background image is placed within the element. You can use keywords, percentages or pixel offsets.

top left
top center
top right
center left
center
center right
bottom left
bottom center
bottom right
CSS — background-position
/* Keywords */
.hero    { background-position: center; }        /* most common */
.top     { background-position: top center; }
.bottom  { background-position: bottom center; }

/* Percentage — relative to element size minus image size */
.pct     { background-position: 50% 50%; }       /* same as center */
.focus   { background-position: 30% 20%; }

/* Pixel offsets — from top-left origin */
.offset  { background-position: 40px 20px; }

/* Edge-relative offsets (CSS4) */
.edge    { background-position: right 20px bottom 30px; }
/* image 20px from right, 30px from bottom */

background-size

background-size controls how large the background image is rendered. The two most important values are cover and contain.

cover
Fills container — may crop. Used for hero images.
contain
Fits entirely — may show gaps. Used for logos.
auto (default)
Image renders at natural pixel size.
50px 50px
Exact pixel dimensions for tiled patterns.
25% (percentage)
Percentage of element width.
CSS — background-size
.hero   { background-size: cover; }    /* fill container, may crop — most common */
.logo   { background-size: contain; } /* fit inside container, no crop */
.natural{ background-size: auto; }    /* natural pixel size (default) */

/* Exact dimensions */
.exact  { background-size: 400px 200px; }   /* width height */
.fluid  { background-size: 100% auto; }      /* full width, proportional height */
.square { background-size: 50px; }            /* 50px wide, auto height */

/* Tiling pattern with consistent tile size */
.pattern{ background-size: 24px 24px; background-repeat: repeat; }

/*
  cover:   aspect ratio preserved, fills entirely — may clip edges
  contain: aspect ratio preserved, fits fully — may leave transparent gaps
*/

background-attachment

background-attachment controls whether the background scrolls with the page content or stays fixed in place as the user scrolls.

scroll (default)
The background moves with the element as the page scrolls. Most common behaviour.
fixed
The background stays fixed relative to the viewport — the element scrolls over it creating a parallax-like effect. The image position is relative to the viewport, not the element.
local
The background scrolls with the element's own content (for elements with overflow: scroll). The background moves when you scroll inside the element.
CSS — background-attachment
.normal    { background-attachment: scroll; }  /* default */
.parallax  { background-attachment: fixed; }   /* stays fixed as page scrolls */
.inner-scroll{ background-attachment: local; }

/* Classic parallax pattern */
.parallax-section {
  background-image:      url('/images/hero.jpg');
  background-attachment: fixed;
  background-size:       cover;
  background-position:   center;
  min-height:            400px;
}

/* ⚠ Note: background-attachment: fixed is GPU-expensive */
/* It can cause performance issues on mobile devices. */
Performance on mobile
background-attachment: fixed forces the browser to repaint on every scroll event, which is GPU-expensive and noticeably sluggish on mobile devices. Consider using CSS position: sticky or JavaScript-based parallax libraries for better performance on all devices.

background-origin & background-clip

These two properties control the reference area for positioning (background-origin) and the painting area (background-clip) of the background.

border-box
border-box
Paints under the border (default)
padding-box
padding-box
Clips to padding edge, not under border
content-box
content-box
Clips to content edge only
Text!
text
Clips to text shape — gradient text
CSS — background-origin & background-clip
/* background-origin — where position 0% 0% starts */
.a { background-origin: padding-box; } /* default — from padding edge */
.b { background-origin: border-box; }  /* from border edge */
.c { background-origin: content-box; } /* from content edge */

/* background-clip — where the paint is cut off */
.a { background-clip: border-box; }   /* default — under the border */
.b { background-clip: padding-box; }  /* stops at border inner edge */
.c { background-clip: content-box; }  /* stops at padding inner edge */

/* Gradient text effect — background-clip: text */
.gradient-text {
  background-image:   linear-gradient(135deg, #264de4, #a855f7);
  -webkit-background-clip: text;
  background-clip:    text;
  color:              transparent;  /* must be transparent to see gradient */
}

The background Shorthand

The background shorthand combines all background properties in one declaration. The order follows a specific pattern — and background-size must come immediately after background-position, separated by a /.

CSS — background shorthand
/* background: color image repeat attachment position / size */
.hero {
  background:
    #0f172a             /* color (fallback) */
    url('/images/hero.jpg') /* image */
    no-repeat           /* repeat */
    center / cover;     /* position / size — the / is required! */
}

/* Common shorthand examples */
.simple   { background: #264de4; }
.gradient { background: linear-gradient(135deg, #264de4, #a855f7); }
.image    { background: url(hero.jpg) center / cover no-repeat; }

/* Reset all backgrounds */
.reset    { background: none; }

/* Note: using shorthand resets any sub-property not specified.
   If you need to change just one property, use the longhand. */

Multiple Backgrounds

CSS allows layering multiple backgrounds on a single element, separated by commas. The first value is the topmost layer. Gradients, images and colour fills can be combined freely.

Image + Overlay
gradient overlay
glow spots
multiple glows
grid pattern
grid lines
fade out
bottom fade
top+bottom glow
dual edge glow
diagonal
stripe texture
CSS — Multiple backgrounds
/* Comma-separated layers — first is on TOP */
.hero {
  background:
    linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)),  /* dark overlay */
    url('/images/hero.jpg') center / cover no-repeat,    /* image */
    #264de4;                                            /* colour fallback */
}

/* Multiple glow spots */
.card {
  background:
    radial-gradient(circle at 20% 30%, rgba(168,85,247,.3) 0, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(38,77,228,.3) 0, transparent 50%),
    #0f172a;
}

/* CSS grid background (no image needed!) */
.grid-bg {
  background:
    repeating-linear-gradient(90deg, transparent 0, transparent 39px,
      rgba(255,255,255,.05) 39px, rgba(255,255,255,.05) 40px),
    repeating-linear-gradient(0deg, transparent 0, transparent 39px,
      rgba(255,255,255,.05) 39px, rgba(255,255,255,.05) 40px),
    #0f172a;
}

Gradient Patterns — Pure CSS

With nothing but CSS gradients you can create texture patterns, replacing image files entirely. These render crisply at any resolution and scale.

linear 3-stop
linear-gradient(135deg, blue, purple, pink)
radial offset
radial-gradient(circle at 30% 40%,…)
conic sweep
conic-gradient(from 180deg,…)
side glows
two linear overlays on dark
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Printable reference covering all background, gradient, layout and box model properties.
Download PDF

Try It Yourself

This editor demonstrates all background properties working together — a hero section with gradient overlay, a card with glow spots, gradient text, and a pure CSS grid pattern.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 16

Questions & Answers
Both values preserve the image's aspect ratio, but they differ in what they prioritise:

cover — scales the image until it completely fills the container. The image may be cropped on the sides or top/bottom to cover all empty space. Use for hero images and backgrounds where full coverage is more important than showing the whole image.

contain — scales the image until it fits entirely inside the container without cropping. The container may have empty (transparent or background-coloured) space around the image. Use for logos, diagrams and images where showing the whole image is critical.

Combine with background-position: center so the most important part of the image stays centred when cropped with cover.
Separate background values with commas. The first value renders on top, subsequent values render beneath it, and the background-color is always the bottommost layer.

Example — dark overlay on a photo:
background:
  linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
  url('/images/hero.jpg') center / cover no-repeat,
  #264de4;

Layers from top to bottom: dark gradient overlay → photo → blue colour fallback.

Individual sub-properties like background-size and background-position can also take comma-separated values to target each layer independently.
The background shorthand order is:
background: color image repeat attachment position / size

The special rule is that background-size must immediately follow background-position, separated by a forward slash /. Without the slash, the value is interpreted as position, not size.

Example: background: #0f172a url(hero.jpg) no-repeat center / cover;

Not all values need to be specified. If omitted, each property uses its default. However, using the shorthand resets all background sub-properties to defaults — if you want to change only one, use the individual longhand instead.
background-clip: text clips the background painting area to the shape of the text characters — so only the text "windows" show the background through.

To make this visible, you must set color: transparent so the text colour doesn't cover the background showing through.

Full recipe for gradient text:
background-image: linear-gradient(135deg, #264de4, #a855f7);
-webkit-background-clip: text; (needed for Safari/Chrome)
background-clip: text;
color: transparent;

Always include both the prefixed -webkit-background-clip and the standard background-clip for cross-browser support.
Background images can fail to load for multiple reasons: slow network, image file returning a 404, user has disabled images, or the image is blocked by an ad blocker or browser extension.

When the image fails, without a background-color fallback, the element's background becomes transparent — which may make overlaid text completely unreadable or the section visually broken.

Best practice:
.hero {
  background-color: #0f172a; ← fallback always renders
  background-image: url('/images/hero.jpg');
}

The background-color renders first (at the bottom), then the image paints on top. If the image fails, the dark colour keeps the text readable. This is especially important for hero sections with white text.
Multiple Choice Questions
Question 1 of 5
Which CSS background property value scales the image to fill the container, potentially cropping it?
Question 2 of 5
In the background shorthand, which separator is required between background-position and background-size?
Question 3 of 5
When stacking multiple backgrounds with commas, which layer renders on top?
Question 4 of 5
What additional property must be set to transparent for gradient text using background-clip: text to work?
Question 5 of 5
Which background-repeat value distributes tiles evenly with gaps, never clipping?
Chapter Summary
  • background-color — solid fill using any CSS colour. Renders behind background-image. Always set as a fallback when using images.
  • background-image — accepts url() or any gradient function (linear-gradient, radial-gradient, conic-gradient).
  • background-repeat — controls tiling: repeat (default), no-repeat, repeat-x, repeat-y, space, round.
  • background-position — keywords (center, top left), percentages or pixels. Edge-relative syntax: right 20px bottom 30px.
  • background-size — cover (fill, may crop), contain (fit whole image), or exact dimensions. Most hero images use center / cover.
  • background-attachment — scroll (default), fixed (parallax), local. Use fixed sparingly — it's slow on mobile.
  • background-clip: text clips the background to text shape. Combine with color: transparent for gradient text effects.
  • Shorthand order: color image repeat attachment position / size. The / before size is required.
  • Multiple backgrounds: comma-separated layers, first is topmost. Combine gradients, images and colour fills. Pure CSS patterns are possible with repeating-linear-gradient.
~8 min read
Beginner level
Chapter 16 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel