Borders & Backgrounds
Borders and backgrounds turn plain boxes into buttons, cards, banners and profile pictures. With a handful of properties you can draw outlines, round corners, add shadows, fill boxes with images and paint smooth gradients without a single image file.
Borders
A border needs three things: a width, a style and a colour. The border shorthand sets all three at once, in any order.
.card {
border: 2px solid #1e3a8a;
}
.warning {
border-left: 6px solid #d97706; /* one side only */
}
.coupon {
border: 2px dashed crimson;
}
Common styles are solid, dashed, dotted, double and none. Without a style, no border appears at all, which is a frequent surprise: border: 2px red; draws nothing because the style is missing.
Rounded corners
border-radius rounds the corners of the box, and it works even when there is no border.
border-radius: 8pxgives gently rounded cards.border-radius: 999pxon a wide button gives a "pill" shape.border-radius: 50%on a square element (such as a profile photo) makes a circle.
Outlines and shadows
outline looks like a border but takes up no space and doesn't change the layout. Browsers use it to show keyboard focus, so never remove it without providing a replacement. box-shadow adds a shadow: horizontal offset, vertical offset, blur, optional spread, then colour.
.card {
box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
}
This casts a soft shadow 4px below the card, blurred over 12px, in black at 15% opacity, which makes the card seem to float slightly above the page.
Background colours and images
background-color fills the content and padding areas. background-image places an image behind the content; if the image fails to load, the colour shows instead, so set both.
.hero {
background-color: #1e3a8a;
background-image: url("images/nairobi-skyline.jpg");
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
| Property | Common values | What it does |
|---|---|---|
background-size | cover, contain, 100% auto | cover fills the box (cropping if needed); contain shows the whole image (leaving gaps if needed) |
background-position | center, top right, 50% 20% | Where the image sits in the box |
background-repeat | no-repeat, repeat-x, repeat | Whether the image tiles (it repeats by default) |
background-attachment | scroll, fixed | fixed keeps the image still while the page scrolls |
The background shorthand combines these, for example background: #1e3a8a url("sky.jpg") center / cover no-repeat;. The size must come straight after the position, separated by a slash.
Use a background image for decoration only. If an image carries meaning (a product photo, a chart), put it in the HTML with an <img> and alt text so screen readers can describe it. See HTML Images.
Gradients
A gradient is a smooth blend between colours, and in CSS it counts as an image, so it goes in background-image (or background).
.banner {
background: linear-gradient(to right, #1e3a8a, #0f766e);
}
.sunset {
background: linear-gradient(135deg, #f59e0b 0%, #dc2626 100%);
}
.spotlight {
background: radial-gradient(circle, #fef3c7, #f59e0b);
}
The first fades from dark blue on the left to teal on the right. The second runs diagonally from amber in the top-left corner to red in the bottom-right. The third glows pale yellow in the centre and darkens to amber towards the edges. A popular trick is layering a semi-transparent gradient over a photo so white text on top stays readable; when you list several backgrounds, the first one is drawn on top.
.hero {
background:
linear-gradient(rgb(0 0 0 / 55%), rgb(0 0 0 / 55%)),
url("images/market.jpg") center / cover;
}
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Borders and backgrounds</title>
<style>
body {
font-family: system-ui, sans-serif;
background: linear-gradient(160deg, #e0f2fe, #fef9c3);
min-height: 100vh;
margin: 0;
padding: 2rem;
}
.card {
max-width: 320px;
background-color: white;
border: 1px solid #cbd5e1;
border-radius: 12px;
padding: 1.5rem;
box-shadow: 0 6px 16px rgb(0 0 0 / 12%);
}
.avatar {
width: 64px;
height: 64px;
border-radius: 50%;
background: linear-gradient(135deg, #1e3a8a, #0f766e);
border: 3px solid #f59e0b;
}
.tag {
display: inline-block;
border: 2px dashed #0f766e;
border-radius: 999px;
padding: 0.2rem 0.8rem;
}
</style>
</head>
<body>
<div class="card">
<div class="avatar"></div>
<h2>Achieng Odhiambo</h2>
<p>Front-end developer, Kisumu</p>
<span class="tag">Open to work</span>
</div>
</body>
</html>
The page has a soft diagonal gradient from pale blue to pale yellow. On it sits a white card with rounded corners and a gentle shadow. Inside are a circular avatar filled with a blue-to-teal gradient and ringed in amber, a name, a job title and a pill-shaped "Open to work" tag with a dashed teal border.
Key points
borderneeds a width, a style and a colour; the style is required.border-radiusrounds corners;50%on a square makes a circle.background-size: coverplusbackground-position: centeris the standard hero-image recipe.- Gradients are images: use
linear-gradient()andradial-gradient()inbackground.
Tip: use the ← and → keys to move between pages.
