Reading history

CSS Colours, Units and Fonts: hex, rgb, rem, em, vw

4 min read 1 views

Learn CSS colours and units: named, hex, rgb, rgba and hsl colours, gradients, px, rem, em, % and vw, fonts, line height and contrast.

Colour, size and typography decide how a page feels and how easy it is to read. CSS lets you write colours as names, hex codes, RGB or HSL, and sizes in fixed units like px or flexible ones like rem, % and vw. This lesson shows each format in a live example and explains which units to choose for text and layout.

In this lesson you will learn

  • Named, hex, RGB, RGBA and HSL colours, and gradients
  • Absolute and relative units
  • The difference between em and rem
  • Font families, fallbacks and common text properties
  • Readable contrast and line height

Colour Formats

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Colours</title>
  <style>
    div { padding: 6px; margin: 4px 0; color: white; font-family: sans-serif; }
    .name { background: teal; }
    .hex  { background: #6a1b9a; }
    .rgb  { background: rgb(230, 81, 0); }
    .rgba { background: rgba(230, 81, 0, 0.5); color: black; }
    .hsl  { background: hsl(210, 70%, 45%); }
    .grad { background: linear-gradient(90deg, #00695c, #26a69a); }
  </style>
</head>
<body>
  <div class="name">Named colour: teal</div>
  <div class="hex">Hex: #6a1b9a</div>
  <div class="rgb">rgb(230, 81, 0)</div>
  <div class="rgba">rgba(230, 81, 0, 0.5) is 50% transparent</div>
  <div class="hsl">hsl(210, 70%, 45%)</div>
  <div class="grad">linear-gradient</div>
</body>
</html>

Result

  • Named colours (teal, crimson) are easy to read but limited.
  • Hex #RRGGBB gives red, green and blue in base 16: #ff0000 is pure red.
  • rgb() uses numbers from 0 to 255; rgba() adds an alpha value from 0 (invisible) to 1 (solid).
  • hsl() is hue (0–360° around the colour wheel), saturation and lightness, which makes it easy to create lighter or darker shades.
  • linear-gradient() blends colours and is used as a background image.

Units and Fonts

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Units and fonts</title>
  <style>
    html { font-size: 16px; }
    body { font-family: Georgia, "Times New Roman", serif; line-height: 1.5; }
    .px  { font-size: 14px; }
    .rem { font-size: 1.25rem; }           /* 1.25 x 16px = 20px */
    .em  { font-size: 1.5em; font-weight: bold; }
    .pct { width: 50%; background: #e0f2f1; }
    .vw  { font-size: 4vw; }
    .sans { font-family: Arial, Helvetica, sans-serif; text-transform: uppercase; letter-spacing: 2px; }
  </style>
</head>
<body>
  <p class="px">14px text</p>
  <p class="rem">1.25rem text (20px)</p>
  <p class="em">1.5em bold text</p>
  <div class="pct">50% of the page width</div>
  <p class="vw">4vw grows with the window</p>
  <p class="sans">Sans-serif, uppercase, spaced</p>
</body>
</html>

Result

UnitRelative toGood for
pxFixedBorders and small details
remThe root (html) font sizeFont sizes and spacing; respects the user’s browser setting
emThe element’s own font size (or its parent’s, for font-size)Padding that scales with the text
%The parent elementWidths
vw / vh1% of the viewport width / heightFull-screen sections, fluid headings
  • font-family takes a list of fallbacks ending in a generic family (serif, sans-serif, monospace). The browser uses the first font it has.
  • line-height: 1.5 (no unit) spaces lines at 1.5 times the font size, comfortable for body text.
  • text-transform, letter-spacing, font-weight and text-align fine-tune the text.
  • Resize your window: the 4vw line changes size, the others do not.

Readable Text

Text needs enough contrast with its background. The WCAG accessibility guidelines ask for a contrast ratio of at least 4.5:1 for normal text. Browser developer tools show the ratio when you inspect a colour. Very light grey on white is a common failure.

Common Mistakes

  1. Setting all font sizes in px, which ignores users who enlarge their default text size.
  2. Nesting em font sizes, which multiply: 1.2em inside 1.2em is 1.44 times the original.
  3. No fallback font, so a missing web font drops to the browser default.
  4. Low contrast colour choices that look stylish but are hard to read.

Practice Exercises

Exercise 1. Make a semi-transparent black overlay at 60% opacity.

Show answer
.overlay { background: rgba(0, 0, 0, 0.6); }

Exercise 2. If the root font size is 16px, what is 2.5rem in pixels?

Show answer

2.5 × 16 = 40px.

Frequently Asked Questions

Should I use rem or px?

Use rem for font sizes and most spacing so the page scales with the user’s settings, and px for fine details like 1px borders.

How do I use a Google Font?

Add the font’s <link> tag to your page’s head, then use its name in font-family with a fallback, such as font-family: "Hind Siliguri", sans-serif;.

Summary

  • Colours can be names, hex, RGB(A) or HSL; gradients blend them.
  • rem is best for text, % and vw for fluid layout, px for details.
  • Always give font fallbacks and a comfortable line height.
  • Check contrast: at least 4.5:1 for body text.

Next: CSS Flexbox.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *