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-colorandbackground-imagelinear-gradient()andradial-gradient()background-size,background-repeatandbackground-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
.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 โ
More in this module
Keep going in Web Developer Path
Adding CSS: three ways and three selectors
Colour the `h1` text, give `.intro` a `background-color`, and give `#special` both a `color` and a `background-color`.
Start โ9Fonts and text styling
Style `h1` with a `font-family`, a `font-size`, `text-align: center` and `text-transform: uppercase`. Style `p` with a `line-height` and `letter-spacing`, and remove the underline from `a` with `text-decoration: none`.
Start โ10Combinators, pseudo-classes and pseudo-elements
Write four rules with exactly these selectors: `.menu a:hover` (set a `color`), `.menu li:first-child` (set `font-weight`), `h2::before` (set `content`) and `input[type="text"]` (set a `border`).
Start โ11The box model
Style `.card`: give it a `width`, some `padding`, a `border`, a `margin` of `12px auto`, and `box-sizing: border-box`.
Start โ12Display and position
Make `.card` `position: relative`, pin `.badge` to its top-right corner with `position: absolute`, `top` and `right`, and hide `.hide-me` with `display: none`.
Start โPractise the idea
๐ Play Decimal Detectives
Find the decimal point, line up place values and solve decimal operations step by step.
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
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.
Pick a game
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay