20 min read
Colours and Backgrounds
Colour names, hex and rgb values, text and background colours, background images, and a consistent theme using CSS variables.
Writing Colours
| Ways to write a colour | ||
|---|---|---|
Colour Properties
- color — the text colour.
- background-color — the colour behind an element.
- border-color — the colour of a border (next part of the course).
Set the page's main colours on body, so every element inherits them.
What the browser shows
A Consistent Theme with Variables
Typing the same hex codes again and again is slow and error-prone. CSS custom properties (variables) store them once:
Theme colours as variablesCSS
:root { --cream: #FBF6EE; --tea-brown: #5A3825; --gold: #D9962B; --text: #2B1D14;} body { background-color: var(--cream); color: var(--text);} header,footer { background-color: var(--tea-brown);}Change --gold once and every button and link that uses it changes too. This keeps the colour scheme from your plan consistent across the whole site.
Background Images
A photo behind a sectionCSS
.hero { background-image: url("../images/teas/tea-plantation.webp"); background-size: cover; background-position: center; min-height: 300px;}The url() path is relative to the CSS file. style.css is inside the css folder, so it goes up one level (../) to reach images. background-size: cover fills the box without stretching the photo.
Practice
More lessons in CSS · Next: Fonts and Text · Previous: Selectors
