CSS Basics

CSS: where a button becomes beautiful… or invisible.

▶ the lesson · narrated sketch video · tap for sound

the intro

CSS is how pages get their look: colors, spacing, fonts, layout. You write selectors that find elements and declare what they should look like. The trick is learning the box model and the cascade — once those click, CSS stops being random.

the sample

one selector, three declarations

button {
  background: #4a7cf6;
  color: white;
  border-radius: 999px;
}

fun fact ✦

CSS was first proposed by Håkon Wium Lie in 1994 — and the cascade debates never ended.

the docs

the deep dive

the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid

CSS has a reputation for randomness it does not deserve. It is a court system: when two rules disagree, fixed tiebreakers decide — origin, then specificity, then source order. Learn the tiebreakers and the box model, and the apparent chaos resolves into procedure.

the cascade is a tiebreaker, not chaos

marginborderpaddingcontent
every element is four boxes stacked inside each other

key terms

selector

The find-part of a rule: which elements does this apply to? Tags, classes, ids, and pseudo-classes all match differently.

.card > h2:hover

declaration

One property-value pair inside a rule. A rule is just a list of these.

color: #4a7cf6;

specificity

The cascade's tiebreaker score: inline beats id, id beats class, class beats tag.

id: 100, class: 10, tag: 1

box model

Every element is content wrapped in padding, border, and margin. Sizing bugs are almost always this model misunderstood.

box-sizing: border-box

how it works

  1. 1
    Collect matching rules

    The browser finds every rule whose selector matches the element, from your stylesheets and the browser defaults.

  2. 2
    Sort by specificity

    Higher specificity wins. An id beats ten classes, which beats a hundred tags.

  3. 3
    Break ties with order

    Equal specificity? The rule written later in the file takes it. This is why import order matters.

  4. 4
    Resolve inheritance

    Properties like color and font flow down from parents unless something overrides them. The result is the computed style that gets painted.

the code, explained

the reset that ends width math forever

1*, *::before, *::after {
2 box-sizing: border-box;
border-box: padding and border count inside the width
3}
4.card { width: 200px; padding: 16px }
that div is 200px wide. total. not 200 plus 32

side by side

border-box

  • width includes padding + border
  • the number you set is the size
  • default in every framework
  • mental math: none
VS

content-box

  • width is content only
  • padding adds on top of it
  • the raw CSS default
  • a debugging classic
🪤 gotcha

!important is a debt with compound interest. It beats specificity, so the only way to override it later is another !important, and six months in nobody remembers why any of them exist. Reserve it for utility classes and third-party overrides — nowhere else.

quick answers

Why is my rule being ignored?

Three usual suspects: a more specific rule won, a tie went to a later rule, or the selector has a typo. DevTools shows the winner and crosses out the losers.

rem or px?

rem for anything that should scale with the user font setting — it is an accessibility feature. px is fine for hairlines and shadows.

Do I still need raw CSS if I use Tailwind?

Yes. Every utility class is a CSS declaration in costume; the cascade and the box model decide whether your layout works either way.

quick hits about CSS Basics

short answers · interviews, tips, trivia & hidden gems

🎤 interview

What is the CSS box model?watch ↗

🎤 interview

Which CSS rule wins?watch ↗

📚 tutorial

Why did your CSS get ignored?watch ↗

🔧 useful

Variables in CSS?watch ↗

💎 deep

A keyword that inherits color?watch ↗

🔧 useful

Fluid font sizes without media queries?watch ↗

💎 deep

Inherit, initial, unset — know the difference?watch ↗