CSS · Unit 1: Styling basics · Lesson 3 of 5
Colors and contrast
color sets the text color and background-color sets the color behind it.
Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.
The idea
color sets the text color and background-color sets the color behind it. You can write a name (tomato), a hex code (#1a73e8) or rgb(26, 115, 232).
A hex code is three pairs of digits from 00 to ff, for red, green and blue. #000000 is black and #ffffff is white.
Text must stand out from its background. Contrast is measured as a ratio; aim for at least 4.5:1 for normal text so everyone can read it.
Worked example
CSS
.banner {
background-color: #1a3d5c;
color: #ffffff;
padding: 12px;
}
The HTML it styles
<div class="banner">Free entry on Sundays</div>
White text on dark blue has a contrast of about 11:1.
Key terms
- Color values
- Names (tomato), hex codes (#1a73e8) or rgb(26, 115, 232).
- Contrast
- How much text stands out from its background. Aim for 4.5:1 or more for normal text.
Practise it in Code Academy
It has 4 exercises (multiple choice, fix the broken code and write code from a prompt) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.
Every lesson is free: no ads, no lives and no paywall.