DevNotes
@devnotes ·
CSS Part 4: Colour, units and fonts
Whether people read your page or give up on it often comes down to how the text looks.
Colours
CSS accepts colours in several forms:
Hex is what you'll see most, because design tools hand you hex codes. HSL is handy when you want a lighter or darker shade of a colour: keep the first number and change the last one.
color sets the text colour and background-color the background. Keep strong contrast between the two. Light grey text on white looks elegant on your laptop and is unreadable on a phone in sunlight. In Chrome's DevTools, click the small colour square next to a color value: the picker that opens shows the contrast ratio. Aim for at least 4.5.
Units
pxis a fixed size. Good for borders and small details.remis relative to the root font size, which is usually 16px, so1.5remis 24px. Use it for font sizes and most spacing. When someone sets a bigger text size in their browser, everything in rem grows with it, and your layout stays in proportion.%is relative to the parent element.width: 50%is half the parent's width.vwandvhare percentages of the browser window:50vwis half its width. On phones,100vhcan be taller than what's visible while the address bar shows, so for a full-screen section use100dvh.
A safe starting point: rem for text and spacing, px for borders, % for widths.
Fonts
font-family takes a list. The browser uses the first font it has and falls back along the list, so always end with a general family like sans-serif or serif. system-ui uses the device's own font, which looks native and costs nothing to load.
line-height is the space between lines. Between 1.5 and 1.7 makes paragraphs comfortable to read, while big headings look better tighter, around 1.2. Write it without a unit, so it scales with the font size.
To use a font like Inter, pick it on fonts.google.com and paste the <link> it gives you into your <head>.
Keep it consistent with variables
Repeating #1a73e8 in twenty places means twenty edits when the brand colour changes. CSS variables solve that:
Variable names start with two dashes. Defined on :root, they're available to the whole page, and you read them with var(). Change one line and every use updates. They'll make dark mode much easier later, too.
Try it
Pick a brand colour for your portfolio page. Put it and your text colour in variables, set a font and a line-height on the body, and size your headings in rem.
Next: CSS Part 5: Line things up with flexbox
Join WIK to like and reply to DevNotes and see more like this.