{module 4}
Colour and type
Making it look right with CSS20 min read
What you will be able to do
- Write colours as codes, and set the font, size and line height of text.
- Measure the contrast ratio of a text colour on its background, and say whether it is enough.
- Choose a colour that passes when the one you were given fails.
Why it matters
Sahel Soap makes soap by hand in Bamako, Mali. Aminata Traoré, the owner, sells at a market on Saturdays and sends customers to her page for the list of soaps. She chose a pale sand colour for the text, to match her packaging. On her laptop indoors it looked soft and warm.
Her customers read the page on phones, outdoors, in strong sun, and could hardly see the words. Some asked her to read the prices aloud. Others left.
Whether text can be read is not a matter of taste. It is measured with one number, and there is an agreed minimum.
The idea
Colour codes
In lesson 1 you used colour names such as maroon. Names are few, and brand colours are exact. A colour code can name any of about 16 million colours:
color: #7a4b24;The code is a hash sign, #, and six characters: two for the amount of red, two for green and two for blue. Each pair goes from 00, none, to ff, the most, using the digits 0 to 9 and the letters a to f. So #000000 is black, #ffffff is white and #7a4b24 is a dark brown. You do not work codes out yourself. Any colour picker gives you the code.
The property background-color sets the colour behind an element. Set it on body and it fills the page.
The contrast ratio
Text can be read when its colour differs enough in brightness from the colour behind it. That difference is the contrast ratio. It goes from 1 to 1, for two colours that are the same, up to 21 to 1, for black on white. It is written like 4.5:1 and read "4.5 to 1".
The Web Content Accessibility Guidelines, known as WCAG, are the rules for pages that everyone can use, published by the W3C, the group that writes the standards of the web. Version 2.2 sets a minimum contrast for text:
- 4.5 to 1 for ordinary text.
- 3 to 1 for large text. Large means at least 24 pixels, or at least about 18.7 pixels and bold.
These are minimums, so you do not round up. A ratio of 4.49 to 1 fails. The rule helps people with weak sight most, and it helps everyone who reads a phone in the sun.
Here is Aminata's old page:
body {
background-color: #ffffff;
color: #d9b779;
}Its contrast ratio is 1.91 to 1, far below 4.5. Eyes are a poor judge of this, and a colour that looks fine on a bright laptop can fail by a long way. So you measure.
Measuring contrast
You need nothing installed. There are two ways.
In the developer tools of Chrome or Edge. Right-click some text and choose Inspect. In the Styles tab, find the color declaration. Next to the value is a small square filled with the colour. Click it, and the colour picker opens. Its Contrast ratio line shows the ratio of this text against its background, with a mark that says whether it meets the minimum. Click the line to see more detail.
With an online checker. The WebAIM Contrast Checker is a free page from WebAIM, a group at Utah State University that works on accessibility. Type the text colour into Foreground and the background colour into Background, as codes. It shows the ratio, and whether the pair passes for normal and for large text. It works in any browser, including Firefox. "Go deeper" links to it.
Type: font, size and line height
The property font-family chooses the font. You give a list, because not every device has every font:
font-family: Verdana, Arial, sans-serif;The browser uses the first font in the list that the device has. The last word, sans-serif, means any plain font without small strokes at the ends of the letters. Every device has one, so the list always ends well.
The property font-size sets the size of the text. Use the unit rem:
font-size: 2.5rem;One rem is the reader's chosen text size. In most browsers it starts at 16 pixels, so 2.5rem is 40 pixels. Why not write 40px? Because a person with weak sight may set a larger text size in their browser settings. Sizes in rem grow with that choice. Sizes in px stay fixed, and the person cannot read your page.
The property line-height sets the height of each line of text. A value with no unit, such as 1.5, means 1.5 times the size of the text. Lines with that space are easier to follow.
Worked example
Make a folder called sahel-soap, with this index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Sahel Soap, Bamako</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Sahel Soap</h1>
<p>Soap made by hand in Bamako with shea butter from local farms.</p>
<h2>Soaps this month</h2>
<ul>
<li>Shea and honey, bar of 120 g, $4</li>
<li>Black soap with lemongrass, bar of 120 g, $5</li>
<li>Baobab oil, bar of 100 g, $6</li>
</ul>
<p class="note">Find us at the Saturday market, stall 12.</p>
</body>
</html>And this styles.css:
/* Contrast measured against the cream background:
text #333333 is 12.14 to 1, headings #7a4b24 are 7.06 to 1. */
body {
background-color: #fffaf0;
color: #333333;
font-family: Verdana, Arial, sans-serif;
font-size: 1rem;
line-height: 1.5;
}
h1 {
color: #7a4b24;
font-size: 2.5rem;
line-height: 1.2;
}
h2 {
color: #7a4b24;
font-size: 1.5rem;
}
.note {
font-weight: bold;
}Save both and open the page. The background is cream, the text is dark grey and the headings are brown, in Verdana if your device has it. Inspect the page, and the Computed tab gives these colours:
body color: rgb(51, 51, 51)
body background-color: rgb(255, 250, 240)
h1 color: rgb(122, 75, 36)The browser shows codes as three numbers from 0 to 255. Each number is one pair of the code: 7a is 122, 4b is 75 and 24 is 36.
The comment at the top of the file gives the measured ratios against the cream: 12.14 to 1 and 7.06 to 1. Both pass with room to spare. The comment shows the next person that someone measured.
Here are the sizes:
body font-family: Verdana, Arial, sans-serif
body font-size: 16px
body line-height: 24px
h1 font-size: 40px
h2 font-size: 24pxThe browser has turned every rem into pixels: 1 is 16, 2.5 is 40 and 1.5 is 24. The line height of the body is 1.5 times 16. The heading has a smaller line height, 1.2, because a long heading over two lines looks loose with 1.5.
Common mistakes
A colour code without its hash sign
color: 7a4b24;rgb(51, 51, 51)The heading is dark grey, the colour it inherits from body. Without the #, the value is not a colour, so the browser ignores the line. In the Styles tab the value is crossed out, with a warning sign.
A size without its unit
font-size: 2.5;32pxThe heading is 32 pixels, the size browsers give an h1 by themselves, and not 40. A number alone is not a size, so the declaration is ignored. This is confusing, because line-height: 1.5 with no unit is correct. line-height accepts a plain number. font-size does not.
A colour that looks fine and fails
font-weight: bold;
color: #999999;rgb(153, 153, 153)There is no error. The note is light grey, and on a laptop it looks calm. Measured against the cream, it is 2.74 to 1, well below 4.5. Bold does not save it, because bold text counts as large only from about 18.7 pixels, and the note is 16. Measure every pair you use, not only the ones that look doubtful.
Think it through
Aminata wants her brand colour, an ochre #b07a2a, for both headings and for the note about the market. Kwame, a learner, prepares the change.
Understand. Three elements might change colour: the h1, the h2 and the note. Each must still meet the minimum.
Break down. Measure the ochre on the cream. Find the size of each element. Compare each one with the right minimum.
Plan. He types b07a2a and fffaf0 into the WebAIM checker. It gives 3.56 to 1: a fail for normal text and a pass for large text. The h1 is 40 pixels and the h2 is 24 pixels, so both are large. The note is 16 pixels and bold, which is not large. So the ochre goes on the two headings only.
Build. He changes color in the h1 and h2 rules to #b07a2a, leaves the note alone, and updates the comment at the top of the file.
Test. The Computed tab gives 24 pixels for the h2, exactly the limit for large text, so it passes. The note is still rgb(51, 51, 51). For the odd case, he imagines someone later making the h2 smaller, at 1.25rem. At 20 pixels the ochre would fail, so he writes in the comment that ochre is only for text of 1.5rem or more.
Reflect. The same colour was right for one element and wrong for another. Contrast belongs to a colour, a background and a size together.
At work
When a client asks how long a job will take, give a range, not a single number. A single number sounds certain, and you are not. A range is honest about what you do not know yet. Say what would push the time to the top of the range.
Aminata, I expect the new colours and fonts to take between 2 and 4 hours.
2 hours if your brand colours pass the contrast check, which I will measure first. 4 hours if one of them is too pale for text. Then I would find a darker shade that still looks like your brand, and send it to you to approve before I use it.
I will tell you which it is as soon as I have measured, today.
The client can plan with this. She knows the most it will take, why it might take that long, and when she will know more.
Your tasks
Task 1: Measure the worked example
Build the page and open it. Use the colour picker in the developer tools to read the contrast ratio of the body text and of the h1. Then type the same pairs into the WebAIM checker.
You are done when both ways give you 12.14 to 1 and 7.06 to 1, or within a few hundredths of them.
Task 2: Test the rem reason
In Chrome, click the three dots at the top right, then Settings, then Appearance, and choose a larger Font size. In Edge or Firefox, type "font size" in the search box of the settings page. Reload your page. Then change font-size: 2.5rem to font-size: 40px in the h1 rule, and reload again. Put the setting back when you finish.
You are done when you can say which headings grew with the setting and which did not.
Task 3: Find a colour that passes
Aminata's old sand colour, #d9b779, fails on white at 1.91 to 1. Find a darker colour of the same family that reaches 4.5 to 1 on white, using the WebAIM checker or the colour picker. Use it for the note on your page, and write the ratio in a comment.
You are done when your note uses a sand or brown colour with a measured ratio of at least 4.5 to 1, and the comment says so.
Recap
- A colour code is
#and six characters: two each for red, green and blue. - Ordinary text needs a contrast ratio of at least 4.5 to 1. Large text, 24 pixels or more, needs 3 to 1. Never round up.
- Measure contrast with the colour picker in the developer tools or with the WebAIM checker. Do not judge it by eye.
- Give sizes in
rem, so that text grows with the reader's chosen size. Aline-heightaround 1.5 makes text easier to read.
Go deeper
- W3C: Understanding Success Criterion 1.4.3 Contrast (Minimum) gives the rule, the ratios and the reasons behind them.
- WebAIM: Contrast Checker measures the contrast ratio of any two colours.
- Chrome for Developers: Make your website more readable shows the contrast ratio in the colour picker of the developer tools.
- MDN: Fundamental text and font styling covers
font-family,font-sizeandline-heightin more depth. - MDN: font-size explains why sizes in
remare kinder to readers than sizes inpx.