Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Full-Stack Web Development — Alpha School

Lesson 5: CSS Foundations: Selectors, the Cascade and the Box Model

If HTML is the structure of a page, CSS is its appearance, and learning CSS is learning how to describe a look in rules a browser can follow. A CSS rule has two parts: a selector, which says which elements the rule applies to, and a declaration block, which says what should change, written as property-value pairs inside curly braces. A rule such as p { color: #211d19; font-size: 1rem; } says that every paragraph should use a dark brown colour and a standard text size. The language is deliberately simple, and most of what makes CSS feel hard at first is not the syntax but the way rules interact, which is what this lesson explains.

Selectors: choosing what to style

The simplest selector is an element name, such as h1 or p, which styles every element of that kind. A class selector, written with a dot, such as .btn, styles every element carrying that class, and classes are the workhorse of CSS because you can reuse them and combine them freely: class="btn btn-next" gives an element two classes at once. An ID selector, written with a hash, such as #main-nav, styles one unique element, and it is best used sparingly, because IDs are hard to override later. You can also combine selectors: .card h2 styles headings inside cards, and a:hover styles a link only while the pointer is over it. Start with simple selectors, give things clear class names that describe their purpose rather than their appearance, such as .lesson-card instead of .green-box, and your stylesheets will stay readable as they grow.

The cascade and specificity

CSS stands for Cascading Style Sheets, and the cascade is the set of rules the browser uses when several rules disagree about the same element. The first rule is origin: the browser's own defaults lose to your stylesheet. The second is specificity: a more specific selector beats a less specific one, so an ID beats a class, and a class beats an element name. The third is order: when two rules are equally specific, the one that comes later in the file wins. Understanding this order explains almost every “why won't my style apply?” moment: usually a more specific rule somewhere else is winning, and the fix is to write a better selector, not to add !important, which is a blunt tool that makes later changes harder. The browser's inspector shows you exactly which rules applied and which were crossed out, and reading that list is the fastest way to learn the cascade.

The CSS box model

MarginBorderPaddingContentTotal width = content + padding + border + margin

By default the width you set applies to the content box only; box-sizing: border-box makes it include padding and border, which most developers prefer.

The box model

Every element on a page is a rectangular box, and the box model describes its four layers: the content in the middle, then padding, the space between the content and the border, then the border itself, and finally margin, the space between this box and its neighbours. Padding has the element's background colour, margin is always transparent, and the total width of a box is the content width plus padding, border and margin, which surprises beginners when a box set to 300 pixels wide with 20 pixels of padding actually takes up 340 pixels. The fix is one line at the top of your stylesheet, * { box-sizing: border-box; }, which makes the width you set include padding and border, so a 300-pixel box stays 300 pixels wide. Margins also have a quirk worth knowing: vertical margins between neighbouring elements collapse, so the gap is the larger of the two margins, not their sum, which is why a page with several headings does not end up with double spacing.

Where CSS lives and how it is organised

CSS can live in three places: inline, in a style attribute on one element, which is quick but hard to maintain; in a <style> element in the page head, which is fine for a single page; or in a separate .css file linked from the head, which is the professional choice, because one file can style a whole site and the browser can cache it. As your stylesheet grows, organise it in sections with comments: first the base styles for the whole page, then the layout, then the components such as cards and buttons, and finally the small utilities. Use variables for colours and spacing, so changing the site's main colour means editing one line instead of fifty, and write your rules mobile-first, starting with the small-screen layout and adding rules for larger screens later, which is the approach you will practise in Lesson 8.

Key Terms

CSS
Cascading Style Sheets, the language that describes how a page should look.
Selector
The part of a CSS rule that says which elements it applies to.
Declaration
A property-value pair, such as color: red, inside a rule's curly braces.
Cascade
The browser's rules for deciding which style wins when several rules disagree.
Specificity
How precisely a selector targets an element; more specific selectors beat less specific ones.
Box model
The four layers of every element: content, padding, border and margin.
box-sizing
A property that decides whether a set width includes padding and border; border-box is the usual choice.

Discussion Questions

  1. Why is it better to name a class .btn-next (what it is for) than .green-button (what it looks like)?
  2. Two rules set the colour of the same heading. How does the browser decide which one wins?
  3. Why do most developers set box-sizing: border-box on every element?

Practice Questions

  1. What are the two parts of a CSS rule?
  2. Which selector is more specific: an ID or a class?
  3. Name the four layers of the box model, from the inside out.
  4. A box is set to 300px wide with 20px of padding on each side. How wide is it by default, and how does border-box change that?
  5. Why should you avoid fixing a styling problem with !important?
Show answers
  1. A selector and a declaration block. The selector says which elements the rule applies to, and the declarations say what should change.
  2. An ID is more specific than a class. In the cascade, an ID selector beats a class selector, which beats an element selector.
  3. Content, padding, border, margin. Padding sits inside the border, and margin is the transparent space outside it.
  4. By default it is 340px wide; with border-box it stays 300px. The default width covers the content only, while border-box includes padding and border in the set width.
  5. It overrides the cascade and makes later changes harder. The next developer, or you in a month, will struggle to override it, so a better selector is almost always the right fix.
Keep reading to complete this lesson