CSS Basics

The language that controls how a page looks, kept separate from what the page contains.

What is it?

HTML says what's on a page: a heading, a paragraph, an image. It says nothing about the heading's color, the text size, or the space around the image. That's the job of CSS (Cascading Style Sheets).

Keeping style separate from structure means the same HTML can look completely different with different styles. It also means one set of rules can be reused across many pages, so you don't repeat "make this text blue" everywhere.

CSS is made of rules. A selector says which elements a rule applies to, and one or more properties with values say what to change. A property might be color, font-size, or margin; the value is the setting, like blue or 16px.

Often several rules apply to the same element. A general rule might style all paragraphs while a more specific one targets a single paragraph. The way the browser decides which value wins is called the cascade, and it's where CSS gets its name.

Explain like I'm 10

If HTML is a document's labeled outline, CSS is the style guide applied on top ('make all headings blue, add space between paragraphs') without changing a word of the content.

Examples

A basic rule

p {
  color: darkslategray;
  font-size: 16px;
  line-height: 1.5;
}

This rule targets every paragraph on the page and sets its text color, font size, and line spacing.

The cascade in action

p { color: black; }
.highlight { color: orange; }
#intro { color: red; }

If a paragraph has both class="highlight" and id="intro", all three rules apply. CSS settles the conflict by specificity and order. Here the id selector wins, so the paragraph is red, not orange or black.

How it works

The browser reads all the CSS that applies, whether from a stylesheet file, a style tag, or inline on an element. It matches each rule's selector against the page's elements.

For each element, it gathers every matching rule and picks the final value for each property. Roughly: more specific selectors win, and when specificity ties, the later rule wins.

Why does it exist?

CSS keeps appearance out of content. Before it, changing a site's look meant editing styling scattered across every page. Now one stylesheet can restyle a whole site, and the same content can look different in different contexts, like print versus screen.

When to use it

Use CSS for anything about how a page looks or is arranged: colors, spacing, fonts, sizing, positioning, and layout. When a look should be consistent across elements or pages, use a shared stylesheet instead of one-off inline styles.

When not to use it

CSS doesn't describe what content is (that's HTML) or add new behavior in response to user actions (that's JavaScript). It can handle small interactive touches like hover effects on its own.

Common mistakes

  • Wondering why a style 'isn't applying' when a more specific rule elsewhere is overriding it.

  • Overusing inline styles instead of a shared stylesheet, so one visual change is hard to apply everywhere.

  • Forgetting that order matters when specificity is equal: the later rule wins.

Practice exercises

  1. Easy:

    Write a CSS rule that makes all h1 elements on a page green and centered.

  2. Medium:

    Write three rules that all apply to the same element (by tag, class, and id) and predict which value wins for a shared property.

  3. Hard:

    Given a small HTML page with conflicting styles, trace the cascade by hand to find the final color of a specific element.

Interview questions

What problem does CSS solve that HTML alone doesn't?

HTML describes structure and content but says nothing about appearance. CSS controls how content looks (color, size, spacing, layout). Keeping it separate lets you restyle content, or reuse styles across pages, without touching the markup.

What is a CSS selector?

The part of a rule that decides which elements it applies to. It can match by tag name, class, id, attribute, or relationship to other elements.

What is 'the cascade' in CSS?

The tie-breaking rules the browser uses when several CSS rules set the same property on the same element. Specificity decides first; if that ties, the rule that comes later in the source wins.

How is specificity calculated, at a high level?

From strongest to weakest: inline styles, then id selectors, then class/attribute/pseudo-class selectors, then plain tag selectors. The stronger selector wins no matter where it appears in the stylesheet.

Given `p { color: black; } .highlight { color: orange; } #intro { color: red; }` applied to a paragraph with `class="highlight" id="intro"`, what color renders, and why?

Red. All three selectors match, but id beats class and class beats tag, whatever order the rules are written in. Specificity decides here, not position.

If two CSS rules have exactly equal specificity, which one wins?

The one that appears later in the source, whether in a later stylesheet or later in the same one. This is the part of the cascade that depends only on order.

How do inline styles, a `<style>` block, and an external stylesheet differ in how they affect the cascade?

All three can set the same properties. But an inline style attribute beats any selector-based rule, however specific, because it's tied directly to that one element instead of competing on selector specificity.

What does `!important` do, and why is it generally discouraged?

It makes a declaration win regardless of specificity or source order. It's discouraged because once two !important declarations fight, normal cascade logic no longer helps; you have to search the whole codebase to know what wins.

What is the CSS box model?

Every element is a box with four layers: the content, padding around it, a border around the padding, and margin outside the border. The rendered size is the sum of all four unless box-sizing changes the calculation.

What's the difference between `box-sizing: content-box` and `box-sizing: border-box`?

With content-box (the default), width covers only the content, so padding and border add pixels on top. With border-box, width already includes padding and border, so the rendered width matches the value you wrote.

Why should an `id` selector generally be avoided for general-purpose styling, separate from the fact that it's very specific?

An id is unique per page, so the rule only ever targets one element. You can't reuse it without duplicating the rule, and its high specificity makes it hard to override later.

What's the difference between `nav a` and `nav > a` as selectors?

nav a matches any <a> anywhere inside a <nav>, however deep. nav > a matches only an <a> that is a direct child of <nav>, not one nested inside another element.

Do all CSS properties inherit from a parent element to its children?

No. Text properties like color and font-family inherit by default, so setting them on a container affects the text inside. Box-model properties like margin, padding, and border don't inherit; each element needs its own, or gets none.

Given `div { color: red; } div p { color: blue; }` and `<div><p>Text</p></div>`, what color does the text render, and why?

Blue. div p matches the paragraph directly. div { color: red; } only matches the div; the paragraph would get red only by inheritance, and a rule that matches an element directly always beats an inherited value.

You change a CSS property on an element but nothing visibly happens — what are the likely mechanisms?

A more specific (or later, equally specific) rule is overriding it. Or the property doesn't apply to the element's display type (like vertical-align on a block element). Or the change never loaded, for example because of caching or a wrong file path.

What's a pseudo-class, like `:hover` or `:first-child`, and how is it different from a regular selector?

A regular selector matches fixed traits: tag, class, or id. A pseudo-class matches a state or position that can change, like being hovered, being the first child of its parent, or being a form field that's currently invalid.

A `<link rel="stylesheet">` is in the page, but none of its styles seem to apply — what would you check?

Check that the href path resolves (a typo or wrong relative path is the most common cause), that the file is served as CSS, that the browser cache isn't serving a stale or empty copy, and that its rules aren't being overridden by more specific ones elsewhere.

Why is a shared stylesheet generally better than inline styles for keeping a site's typography consistent?

A shared stylesheet defines a rule once, say p { font-size: 16px; }, and every paragraph on every page that loads it picks it up. With inline styles, you repeat the value on every element and have to find and update each one by hand.