DataLens

Colors, fonts, and units

Specify colours in hex, rgb and hsl with transparency, set up readable typography with font stacks, sizes and line height, and choose between px, rem, em, % and viewport units.

Level: Intermediate · 7 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

The idea

Most of a page's look comes from three things: colour, type and size. CSS gives you several ways to write each, and picking the right one matters more than it seems: the right unit makes a page scale for users who enlarge their text; the right colour format makes a palette easy to adjust; the right line height makes long text pleasant to read.

Colours

Toko Kita's brand colour is a warm coffee brown. CSS lets you write that same colour in several formats, and each is handy in a different situation. All the first three lines below produce the same brown:

<style>
  .hex   { color: #7a4b2a; }                 /* red, green, blue in hexadecimal */
  .rgb   { color: rgb(122 75 42); }           /* the same colour, 0–255 per channel */
  .hsl   { color: hsl(25 49% 32%); }           /* hue (0–360°), saturation, lightness */
  .faded { color: rgb(122 75 42 / 0.5); }      /* 50% opacity */
  .named { color: rebeccapurple; }
  .badge { background: #2e7d32; color: white; padding: 2px 8px; }
</style>

<p class="hex">#7a4b2a</p>
<p class="rgb">rgb(122 75 42)</p>
<p class="hsl">hsl(25 49% 32%)</p>
<p class="faded">50% transparent</p>
<p class="named">rebeccapurple</p>
<p><span class="badge">In stock</span></p>

Contrast matters. Light grey text on white looks elegant and is unreadable for many people (and for everyone on a phone in sunlight). Aim for a contrast ratio of at least 4.5:1 for normal text; browser developer tools show the ratio when you inspect a colour.

Fonts

Type does more for a page's feel than almost anything else. These are the properties that control it: which typeface, how heavy, how big, and how much space between lines:

<style>
  body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 1rem;
    line-height: 1.6;
    color: #222;
  }
  h1 { font-family: Georgia, "Times New Roman", serif; font-weight: 700; font-size: 2rem; line-height: 1.2; }
  .lead { font-size: 1.25rem; }
  .fine-print { font-size: 0.875rem; font-style: italic; }
</style>

<h1>Toko Kita</h1>
<p class="lead">Freshly roasted Indonesian coffee.</p>
<p>Every bag is roasted on Monday and shipped by Wednesday, so it reaches you at its best.</p>
<p class="fine-print">Prices include VAT.</p>

Web fonts (Google Fonts, or your own files with @font-face) are loaded with a <link> in the <head> and then used by name in font-family.

Units

UnitRelative toUse for
pxa fixed pixelborders, small fixed details
remthe root (html) font size, usually 16 pxfont sizes, spacing
emthe current element's font sizespacing that should scale with its own text (button padding)
%the parent's sizewidths in fluid layouts
vw / vh1% of the viewport width / heightfull-screen sections, fluid type
chwidth of the "0" charactertext column widths
<style>
  .card { width: 50%; padding: 1.5rem; border: 1px solid #ddd; }
  .button { font-size: 1.125rem; padding: 0.5em 1em; background: #7a4b2a; color: white; border: 0; }
  .small .button { font-size: 0.875rem; }
</style>

<div class="card">
  <p>50% of the page width, 1.5rem padding.</p>
  <button class="button">Normal button</button>
  <div class="small"><button class="button">Small button, padding scales too</button></div>
</div>

Why rem for text? Users can set a larger default font size in their browser. Sizes in rem scale with that setting; sizes in px ignore it. A 1.5rem heading is 24 px for most people and 30 px for someone who set 20 px as their default.

The em trap: em multiplies through nesting. A 1.5em element inside another 1.5em element is 1.5 × 1.5 = 2.25 times the base size. That's useful for button padding and confusing for font sizes; use rem for font sizes.

Key idea

Pick colours by role with enough contrast (4.5:1 for text); use font stacks ending in a generic family; unitless line-height around 1.6 for body text; rem for font sizes and spacing, em for things that should scale with their own text, %/vw for fluid widths, px for hairlines.

Why this matters for clean code

A small, deliberate set of values (a few colours, a type scale like 0.875 / 1 / 1.25 / 2 rem, a spacing scale) makes a design consistent and easy to change. Module 13 turns these values into CSS variables, so a colour defined once is used everywhere.

Recap

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: The cascade, specificity, and inheritance →