CSS background images, gradients, background shorthand, border-radius for rounded corners, box-shadow, text-shadow, and multiple backgrounds.
Backgrounds, Borders & Shadows
Backgrounds, rounded corners, and shadows add visual depth and polish to your designs. This chapter covers every background technique, border rounding, and shadow effect CSS offers.
background-color
The simplest background - a solid colour:
Already covered in chapter 4. All colour formats (hex, rgb, hsl, named) work here.
background-image
Load an image as the background:
The image tiles (repeats) by default to fill the element. You almost always need additional properties to control this.
background-size
Controls how the image is scaled:
cover is the most common value. It ensures no gaps and is the equivalent of object-fit: cover for <img> elements.
background-position
Controls where the image is anchored:
Values: center, top, bottom, left, right, or exact values like 50% 30% or 20px 100px.
background-repeat
Controls tiling behaviour:
background-attachment
Controls whether the background scrolls with the page:
Note:
background-attachment: fixedcreates a parallax-like effect but performs poorly on mobile devices. Use it sparingly.
The background shorthand
Combine all background properties in one declaration:
The order: image position / size repeat attachment color. The / separates position from size.
Gradients
Gradients are generated images - you use them with background-image (or the background shorthand), not
background-color.
Linear gradients
Create a smooth transition between colours along a line:
Directions:
Multiple colour stops:
Control where each colour starts:
Hard colour stops
For sharp transitions instead of smooth blending, place two colours at the same position:
Radial gradients
Create a circular or elliptical gradient:
Control shape, size, and position:
Conic gradients
Rotate colours around a centre point:
Multiple backgrounds
An element can have multiple backgrounds, separated by commas. They stack on top of each other - the first one is on top:
This is a common pattern: a dark gradient overlay on top of an image, making white text readable on any photo.
border-radius
Rounds the corners of an element:
A single value rounds all four corners equally.
Individual corners
Shorthand (clockwise from top-left):
Circles and pills
50% on a square element creates a circle. A very large value like 9999px creates a pill shape on rectangular
elements.
box-shadow
Adds a shadow behind an element:
The syntax: offset-x offset-y blur-radius spread-radius colour.
Common shadow presets
Multiple shadows
Stack multiple shadows for more realistic depth:
Inset shadows
Add the inset keyword for an inner shadow:
text-shadow
Adds a shadow behind text:
Syntax: offset-x offset-y blur-radius colour.
outline
Outlines are drawn outside the border and do not affect layout:
Unlike borders, outlines do not affect the element's size or position. They are primarily used for focus indicators (accessibility).
Note: Never remove focus outlines (
outline: none) without providing an alternative focus style. Focus indicators are essential for keyboard navigation.
Practical example
A polished card component combining everything:
What you learned
- background-image with
background-size: coverfills elements with images - Linear, radial, and conic gradients create smooth colour transitions
- Multiple backgrounds stack on top of each other
- border-radius rounds corners;
50%makes circles; large values make pills - box-shadow adds depth; stack multiple shadows for realism; use
insetfor inner shadows - text-shadow adds depth behind text
- Outlines are for focus indicators and do not affect layout
Next step
Static styles are powerful, but movement brings interfaces to life. The next chapter covers transitions and animations - how to smoothly change CSS values over time.