HTML and CSS · Lesson 11 of 12
Colours, fonts, variables and transitions
The details that make a page feel finished: a consistent palette, readable type, and small, smooth changes when you hover or click.
Colours and CSS variables
Colours can be written as hex (#1971c2), rgb(25 113 194) or hsl(208 77% 43%). Instead of repeating them, store them in custom properties (CSS variables) on :root and use var(--brand). Change the variable once, and everything updates, which is how dark mode is usually built.
Fonts and transitions
A font stack lists fonts in order of preference: font-family: Inter, system-ui, sans-serif;. System fonts load instantly; web fonts look custom but cost a download. Keep body text around 16px with a line height of about 1.5.
A transition animates a property change: transition: transform 0.2s;. Animate transform and opacity, which are cheap, rather than width or top, which make the browser redo the layout.
Your turn
Make a button that uses a --brand variable for its background, grows slightly (scale 1.05) on hover with a smooth transition, and turns green when you change --brand to #2b8a3e.
Show a solution
<style>
:root { --brand: #1971c2; }
.go { background: var(--brand); color: white; border: 0; padding: 10px 18px; border-radius: 10px; transition: transform 0.2s; }
.go:hover { transform: scale(1.05); }
</style>
<button class="go">Start practice</button>In the interview
What are CSS custom properties, and why use them?
Variables defined with --name and read with var(--name). They cascade and can change at run time, so one set of rules can serve light and dark themes or different brands by swapping the variables.
Which properties are cheap to animate?
transform and opacity: the browser can animate them on the GPU without recalculating layout. Animating width, height, top or margin forces layout on every frame and can stutter on phones.
What is a font stack?
A list of fonts in order of preference; the browser uses the first one it has. Ending with a generic family (sans-serif, monospace) guarantees something sensible is always used.