Chapter 6 of 17 · CSS for Beginners — Complete Free Tutorial

Colors & Opacity

Colour sets the mood of a product. CSS accepts many colour formats — this chapter explains each one, shows how opacity works, and adds the one rule that keeps your site readable: contrast.

The colour formats

FormatExampleBest for
Namedrebeccapurple, tealQuick prototypes (147 names)
Hex#4f46e5, #fffDesign hand-offs, brand codes
RGBrgb(79 70 229)Programmatic mixing
RGB + alphargb(0 0 0 / 0.5)Translucent overlays
HSLhsl(245 83% 59%)Building consistent palettes
currentColorborder-color: currentColorComponents that inherit text colour

Hex

h1 { color: #4f46e5; }        /* 6 digits: RR GG BB */
p  { color: #0f172a; }
.c { color: #fff; }           /* shorthand: #ffffff */

Hex is compact and exact. The 8-digit form #4f46e599 adds alpha in the last two digits.

RGB / RGBA

.overlay {
  background: rgb(15 23 42 / 0.55);   /* modern space-separated syntax */
  /* legacy: rgba(15, 23, 42, 0.55) still works */
}

The final number (0–1) is alpha — transparency. 0 is invisible, 1 is solid.

HSL — the designer's format

Hue (0–360 colour wheel), Saturation (0–100% grey → vivid), Lightness (0–100% black → white). Same hue + different lightness gives a harmonious palette for free:

:root {
  --brand-50:  hsl(245 83% 97%);  /* backgrounds */
  --brand-500: hsl(245 83% 59%);  /* base  */
  --brand-700: hsl(245 74% 44%);  /* hover */
  --brand-900: hsl(245 65% 28%);  /* pressed / text on light */
}

Expected output: four shades that always look related, because only lightness changed.

Choosing colours that work together

  1. Pick one brand hue (your accent).
  2. Build light/dark variants by adjusting only the L value in HSL.
  3. Add a neutral scale for text and borders — tiny saturation (2–5%) keeps greys from looking dead.
  4. Reserve one warm/red tone purely for errors.

CSS variables make this a one-time setup — covered in CSS Variables & Specificity.

Opacity vs alpha vs transparency

.muted   { opacity: 0.6; }              /* fades element + ALL children */
.overlay  { background: rgb(15 23 42 / 0.5); } /* only the background */
  • opacity applies to the whole element (text included) — the child text fades too.
  • Alpha in a colour affects only that colour — use it for overlays, tinted panels and shadows.

Contrast: the accessibility rule

Text must be readable by everyone:

  • Body text vs background: at least 4.5:1 contrast (WCAG AA).
  • Large text (24 px, or 19 px bold): 3:1.
  • Do not rely on colour alone to convey meaning (pair it with an icon or text).

A quick test: squint at your screen — if two greys merge, bump the lightness gap. Browser dev tools show an exact contrast ratio when you click a colour.

Practical example: a theme-ready palette

:root {
  --bg:      #ffffff;
  --surface: #f8fafc;
  --text:    #0f172a;
  --muted:   #64748b;
  --accent:  #4f46e5;
  --danger:  #dc2626;
  --success: #16a34a;
}

body      { background: var(--bg); color: var(--text); }
.card     { background: var(--surface); border: 1px solid #e2e8f0; }
.muted    { color: var(--muted); }
.alert    { border-left: 4px solid var(--danger); padding: 1rem; background: rgb(220 38 38 / 0.08); }

Expected output: a clean white page with slate text, an indigo accent, and a soft red-tinted alert box.

Common mistakes

  • Low-contrast grey text — #ccc on white fails badly; use at least #6b7280-equivalent darkness for body text.
  • White text on mid-tone accents — test it; sometimes a dark text colour reads better.
  • Hard-coding colours everywhere — one palette in variables, values referenced by name.
  • Red/green-only signals — add text or icons for colour-blind users.

Practice checklist

  1. Build a 4-shade brand scale using only HSL lightness steps.
  2. Create an overlay with rgb(0 0 0 / 0.6) over an image.
  3. Check your text/background contrast ratio in dev tools — fix anything under 4.5:1.

Next: make text beautiful — CSS Typography.

Quiz: Colors & Opacity

5 questions · 70% to pass

Frequently Asked Questions

CSS (Cascading Style Sheets) is the language that controls how HTML content looks — colours, fonts, spacing, layout, responsiveness and animation. HTML provides structure and meaning; CSS provides presentation. One stylesheet can restyle an entire website at once.

The fundamentals (selectors, box model, Flexbox, media queries) take about 2–3 weeks of daily practice. Grid, animations and architecture come with a little more practice. This 17-chapter series is designed for 3–4 weeks at a relaxed pace — faster if you type out every example.

Yes. HTML first, then CSS, then JavaScript. CSS and HTML together let you build complete, good-looking static pages, and almost every JavaScript framework assumes you already know CSS layout and selectors.

No — CSS is a styling language with selectors and property-value declarations, not variables, loops or conditionals. You can get far without any programming logic, and JavaScript adds the behaviour later.

Flexbox distributes space along one axis (a row or a column) and is ideal for components — toolbars, navbars, card rows. Grid controls rows and columns together and is ideal for page structure and card grids. They are complementary: many layouts use a grid page shell with flex components inside.

Usually one of three reasons: the selector does not match (check class names), a more specific rule wins (use the browser inspector to see computed styles and specificity), or the file is not linked/loads in the wrong order. The specificity chapter explains how the cascade decides.

Every element is a box of four layers: content, padding (inner space), border, and margin (outer space). With box-sizing: border-box, the width you set includes padding and border — the universal reset every project uses.

Use relative units, flexible grids like repeat(auto-fit, minmax(260px, 1fr)), images with max-width: 100%, and mobile-first media queries. The goal is zero horizontal scrolling from 320px up — the responsive chapters show the full workflow.

Not to learn. Frameworks are productivity tools built on the same CSS you are learning here; understanding the fundamentals makes any framework easier to use (and to debug when it fights you). Learn vanilla CSS first, then evaluate frameworks with an informed eye.

JavaScript for interactivity, then a build tool and version control (Git). With HTML, CSS and JavaScript you can build complete websites; after that, frameworks like React or server-side tools like Laravel Blade build on these fundamentals.