BeginnerLesson 7 of 305 min read

CSS backgrounds, gradients, border radius and shadows

Add finish to your boxes with backgrounds, gradients, rounded corners, shadows and outlines.

In this lesson you will

  • Set background images and sizes
  • Create gradients
  • Round corners and make circles
  • Add soft shadows

About 14 minutes

From flat boxes to polished cards

Compare two buttons. One is a flat grey rectangle with square corners. The other has a soft violet gradient, rounded corners and a gentle shadow that makes it look as if it floats above the page. Same HTML, a few lines of CSS. This lesson covers the properties that add finish: backgrounds, gradients, rounded corners and shadows.

Background colour and image

.hero {
  background-color: #EFEAFD;
  background-image: url("images/pattern.png");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
  • background-repeat: images tile by default. Use no-repeat to show one copy.
  • background-position: where the image sits, such as center or top right.
  • background-size: cover fills the box (cropping if needed) and contain fits the whole image inside.

You can also use the background shorthand to set several at once: background: #EFEAFD url(pattern.png) center / cover no-repeat;. Notice the slash between position and size.

Good to know

When you place text over a background image, check the contrast. A busy photo can make the words unreadable. A simple fix is a dark, semi-transparent overlay between the image and the text.

Gradients

A gradient is a smooth fade between colours. It is created by CSS itself, so it costs no download and is perfectly sharp.

.a { background: linear-gradient(135deg, #6C2BE0, #4A6EDD); }
.b { background: linear-gradient(to right, #5A28C8, #46D3DC 60%, #F06CC2); }
.c { background: radial-gradient(circle at top left, #F1EAFF, #FFFFFF); }

A linear gradient takes a direction, as an angle or a word, then a list of colours. Add a percentage after a colour to place it. A radial gradient spreads out from a point. Gradients are treated as images, so you use them in background or background-image, never in background-color.

Give the banner a diagonal gradient from violet to blue, with white text. Then make a second banner with a gradient from pink to cyan.

<style>
 .banner { padding: 28px; border-radius: 16px; color: white; font-family: Arial; font-size: 20px; margin-bottom: 12px; }
 .one { /* gradient here */ }
 .two { /* another gradient */ }
</style>
<div class="banner one">Violet to blue</div>
<div class="banner two">Pink to cyan</div>

Use background: linear-gradient(135deg, #6C2BE0, #4A6EDD); and linear-gradient(90deg, #F06CC2, #46D3DC);

<style>
 .banner { padding: 28px; border-radius: 16px; color: white; font-family: Arial; font-size: 20px; margin-bottom: 12px; }
 .one { background: linear-gradient(135deg, #6C2BE0, #4A6EDD); }
 .two { background: linear-gradient(90deg, #F06CC2, #46D3DC); }
</style>
<div class="banner one">Violet to blue</div>
<div class="banner two">Pink to cyan</div>

Rounded corners

.card   { border-radius: 16px; }
.pill   { border-radius: 999px; }
.avatar { border-radius: 50%; width: 80px; height: 80px; }
.tab    { border-radius: 12px 12px 0 0; }

One value rounds all corners equally. Four values go clockwise from the top left. A very large number, such as 999px, turns a short box into a pill. A 50% radius on a square makes a circle, which is how round profile pictures are made.

Box shadows

.card {
  box-shadow: 0 8px 24px rgba(20, 17, 46, .12);
}
.btn:hover {
  box-shadow: 0 12px 30px rgba(90, 40, 200, .35);
}

A shadow has five parts: horizontal offset, vertical offset, blur, optional spread, and colour. The recipe for natural-looking depth is: a small positive vertical offset, a generous blur and a very transparent colour. Harsh, dark, tight shadows look dated. You can layer several shadows, separated by commas, for a richer look.

Turn the plain box into a card: white background, 16px radius, 24px padding and a soft shadow with a vertical offset of 10px, blur 30px and colour rgba(20,17,46,.15).

<style>
 body { background: #F4F1FC; padding: 30px; font-family: Arial; }
 .card { /* your styles */ }
</style>
<div class="card"><h3>Course card</h3><p>Learn CSS in minutes.</p></div>

background: white; border-radius: 16px; padding: 24px; box-shadow: 0 10px 30px rgba(20,17,46,.15);

<style>
 body { background: #F4F1FC; padding: 30px; font-family: Arial; }
 .card { background: white; border-radius: 16px; padding: 24px; box-shadow: 0 10px 30px rgba(20,17,46,.15); max-width: 300px; }
</style>
<div class="card"><h3>Course card</h3><p>Learn CSS in minutes.</p></div>

Text shadow and opacity

  • text-shadow: 0 2px 6px rgba(0,0,0,.4); adds a shadow behind the letters, handy for text over images.
  • opacity: .5; makes the whole element, including its content, half transparent. If you only want a see-through background, use an rgba colour instead.

Outlines versus borders

An outline looks like a border but takes no space and does not change the layout. Browsers use it to show the keyboard focus. Never remove it without giving something equally clear in its place. You can style it with outline: 3px solid #5A28C8; outline-offset: 3px;.

Common mistake

Putting opacity on a container to fade its background, and ending up with faded text too. Use a transparent colour for the background only.

Subtlety wins

Choose one or two effects per element. A card with a gradient, a heavy shadow, a thick border and a glow looks cluttered. A soft shadow and a clean radius almost always looks better.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which background-size value fills the box, cropping if needed?

  1. contain
  2. cover
  3. auto
  4. repeat

cover scales the image to fill and may crop.

Where do you use a gradient?

  1. In background or background-image
  2. In color
  3. In border-radius
  4. In margin

Gradients are images, so they go in the background properties.

Which radius turns a square into a circle?

  1. 10px
  2. 50%
  3. 0
  4. auto

50 percent rounds the corners fully.

What gives a natural looking shadow?

  1. Black, tight, no blur
  2. Small offset, large blur, low-opacity colour
  3. A thick border
  4. Bright red

Soft and transparent looks realistic.

What is the problem with opacity on a container?

  1. It is invalid
  2. It fades the contents as well as the background
  3. It slows the page
  4. It disables hover

Use rgba or hsla on the background to fade only that.

Summary

  • Background properties control images; gradients are generated by CSS.
  • border-radius rounds corners, 50% makes circles.
  • Soft, transparent shadows look natural.
  • Use effects sparingly.

Next lesson

Next: how CSS decides which rule wins when several apply.

Questions

Can I use two backgrounds on one element?

Yes. Separate them with commas. The first one is on top.

Does a gradient slow the page?

No. It is drawn by the browser and needs no download.

What is the difference between box-shadow and filter: drop-shadow?

box-shadow follows the rectangular box. drop-shadow follows the visible shape, so it works well on transparent PNGs.

Progress is saved in this browser.