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.
background-color — solid and
transparent fillsbackground-image — URLs and
gradientsbackground-repeat, background-positionbackground-size — cover,
contain and custombackground-attachment, background-origin, background-clip
background shorthand and
multiple backgroundsbackground-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.
.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.
/* 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; }
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.
.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.
/* 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.
.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.
overflow: scroll). The background moves when you scroll inside the
element.
.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. */
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.
/* 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 /.
/* 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.
/* 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.
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.
Quick Check — Chapter 16
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.
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.
background shorthand order is:background: color image repeat attachment position / sizeThe 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.
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 rendersbackground-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.
transparent for gradient text using background-clip: text to work?background-repeat value distributes
tiles evenly with gaps, never clipping?background-color— solid fill using any CSS colour. Renders behindbackground-image. Always set as a fallback when using images.background-image— acceptsurl()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 usecenter / cover.background-attachment—scroll(default),fixed(parallax),local. Usefixedsparingly — it's slow on mobile.background-clip: textclips the background to text shape. Combine withcolor: transparentfor 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.