JomMula

Frontend Development: HTML to React · CSS · Lesson 06

CSS basics: selectors, cascade, and the box model

CSS is a list of rules: pick an element, say what it should look like. Learn selectors, the cascade, and the box model — the three ideas behind every layout you'll ever build.

7 min readCSSbasicsUpdated October 2026

HTML gives you structure. CSS decides what that structure looks like: colors, spacing, size, layout, and all the small details that make a page feel finished.

The mental model is simple. CSS is a list of rules:

selector {
  property: value;
}

Pick something, say how it should look. That’s it.

Three ways to add CSS

  1. An external file — the normal way:
<link rel="stylesheet" href="style.css">
  1. A <style> block in the head — fine for a small demo or a single page.

  2. Inline styles — style="color: red" on an element. Avoid these. They’re hard to reuse and they win fights with your stylesheet.

In the playground below, the CSS panel is your external stylesheet.

Selectors

p { color: #5d5680; }          /* every paragraph */
.intro { font-size: 18px; }    /* every element with class="intro" */
#page-title { color: #4f46e5; } /* the one element with id="page-title" */
.card p { margin: 0; }          /* paragraphs inside .card */
h1, h2, h3 { line-height: 1.2; } /* a group */
  • Element selectors are broad. Good defaults.
  • Class selectors are reusable. This is what you’ll use most.
  • ID selectors are unique. Use them for anchors and JavaScript hooks; use classes for styling.
  • Descendant selectors (.card p) target elements inside something else.

The cascade

When two rules target the same element, the more specific one wins:

p { color: red; }
.intro { color: blue; }

An element with class="intro" is blue, because a class is more specific than an element. If specificity is equal, the rule that comes later wins.

There’s also !important, which overrides everything. Avoid it. If you need !important, the structure is usually fighting you.

The box model

Every element is a box with four layers:

Layer What it is
Content The text or image itself
Padding Space inside the box, around the content
Border The line around the padding
Margin Space outside the box, pushing other elements away

By default, width applies to the content only. Add padding and border, and the box gets wider than you asked for. That’s why almost every project starts with:

*,
*::before,
*::after {
  box-sizing: border-box;
}

With border-box, width includes the padding and border. What you set is what you get. It’s the single most useful CSS reset line.

Try it

Here’s a small card using element, class, and id selectors. Change the colors, padding, and border. Watch the box model in action.

Preview

Edit the code, then hit Run (or Ctrl + Enter) and watch the preview. This is a safe sandbox — you can't break the page.

Style the skeleton

the plan's verify step

Open your calculator page and create a style.css file. Link it in the head. Add the border-box reset, set a font and background on body, and give your sections some padding and a visible border so you can see the boxes.

Don't try to make it beautiful yet. Make it readable. We'll handle layout next.

The mistakes everyone makes

  • Missing semicolon. One missing ; can break the next line too.
  • Missing closing brace. The rest of the file disappears.
  • Specificity wars. You add an id, then !important, then another id. Use classes and keep it flat.
  • height: 100% doing nothing. The parent needs a height for a percentage to work. We’ll use flex/grid instead.
  • Styling by tag when a class is clearer. p { color: ... } is fine as a default; use .intro for exceptions.
  • Forgetting box-sizing. Your widths will lie to you.

CSS is selector { property: value; }.

Use classes for reusable styles; ids for anchors and JavaScript.

The cascade picks the most specific rule; later rules win ties.

Every element is a box: content, padding, border, margin.

box-sizing: border-box makes width include padding and border.

Next: Flexbox — the easiest way to put things in a row, center them, and stop fighting with margins.

Previous: HTML checkpoint: build the calculator page
07

Up next

Flexbox: lay things out in a row

Flexbox is the layout tool you'll reach for most: rows, columns, spacing, centering, and wrapping. Learn the five properties that do 90% of the work.

Read now