Book a free demo

Web Developer Path ยท lesson 8 of 18

Backgrounds and gradients

Fill the space behind your content with colours, smooth gradients and sizes that fit.

What you will learn

  • background-color and background-image
  • linear-gradient() and radial-gradient()
  • background-size, background-repeat and background-position

Understand the idea

Every element has a background, transparent by default. background-color fills it with one colour. background-image puts an image or a gradient there, and the image sits on top of the colour, so a colour makes a good fallback.

A gradient is an image the browser draws itself, so it needs no file and never goes blurry. linear-gradient(135deg, #6c4de6, #ff5d8f) blends from one colour to another along a line at the angle you choose (90deg goes left to right, 180deg top to bottom). Add more colours for more stops. radial-gradient(circle, yellow, orange) spreads out from the centre.

Three properties control a background image. background-size is cover (fill the box, cropping if needed), contain (show all of it) or a size such as 100px 50px. background-repeat decides if it tiles (repeat, no-repeat). background-position places it (center, top right). A single background shorthand can set several at once, but the long-hand names are clearer while you learn.

Example

A gradient hero banner
.hero {
  background-image: linear-gradient(135deg, #6c4de6, #ff5d8f);
  background-size: cover;
  color: white;
  padding: 24px;
}

.card {
  background-color: #e6fcf5;
}

Change the angle 135deg or one of the colours and watch the banner change.

Your turn: code to type and practise

Give .hero a background-image that uses linear-gradient, set its background-size, and give .card a background-color. Use the long-hand properties.

Stretch challenge

Try a radial-gradient and a gradient with three colours.

โ† Adding CSS: three ways and three selectorsFonts and text styling โ†’

CSS

More in this module

Keep going in Web Developer Path

Practise the idea

๐Ÿ” Play Decimal Detectives

Find the decimal point, line up place values and solve decimal operations step by step.

Play now

FAQ

Backgrounds and gradients: common questions

What is a CSS gradient?

An image drawn by the browser that blends two or more colours. It needs no image file and stays sharp at any size.

What does background-size: cover do?

It scales the background to fill the whole box, cropping the edges if the shapes differ.

Do I need an image file for a gradient?

No. Gradients are created by CSS functions such as linear-gradient.

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try