# How CSS Works: Syntax & Linking

> Write a rule, target an element, see it change. Here is exactly what each piece does.

Subject: CSS | Lesson 2 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/how-css-works
Previous: [Flexbox](https://codewithanimation.in/css/flexbox.md) | Next: [Selectors & Combinators](https://codewithanimation.in/css/selectors-and-combinators.md)

## CSS rules: selector, property, value

CSS is a rule-based language. Every rule has two parts: a **selector** that identifies which HTML elements to style, and a **declaration block** (wrapped in `{ }`) containing one or more declarations.

A declaration is a `property: value;` pair. The property names a style attribute (`color`, `font-size`, `margin`), and the value says what it should be (`red`, `1rem`, `0`). The semicolon after each declaration is required; forgetting it is the most common parse error.

Put them together: `h2 { color: teal; font-size: 1.5rem; }` targets every `<h2>` on the page and gives it a teal color and 1.5rem size. That is the whole model.

## Separation of concerns: style lives apart from structure

HTML describes meaning and content. CSS describes presentation. Keeping them separate means you can restyle an entire site by editing one stylesheet, without touching any HTML.

An external CSS file is downloaded once and cached by the browser for every subsequent page visit. If you inlined styles in every HTML file, the browser would have to re-download the same style data on every page.

The separation also lets you apply the same styles to many elements at once with a single rule, instead of repeating the same `style=` attribute hundreds of times.

## Three ways to attach CSS to HTML

**External** (`<link rel="stylesheet" href="styles.css">`): a separate `.css` file linked from `<head>`. This is the standard approach for any real project. The file is cached, reusable across pages, and easy to maintain.

**Internal** (`<style>` element inside `<head>`): styles scoped to a single HTML file. Useful for quick prototypes or email templates where a separate file is not practical.

**Inline** (`style=""` attribute directly on an element): the highest-specificity option with no reuse. Reserve it for one-off overrides or dynamically computed values from JavaScript.

When styles conflict, inline wins over internal/external. Between internal and external, source order decides: whichever declaration comes later wins (specificity being equal).

## Common mistake: Missing semicolons and mismatched braces

A missing semicolon after a declaration silently drops that rule and sometimes the next one too, because the browser parser gives up on the whole block. Always close every declaration with `;`.

A missing closing brace `}` makes the browser treat everything after it as still inside the current rule. Nothing after the unclosed block will render correctly. Open DevTools and check the Styles panel: it shows parse errors with a warning icon.

## Tip: One external stylesheet per project

Start with a single `styles.css` linked from every page. Only reach for `<style>` tags when you need page-specific overrides, and never use inline `style=` for anything you might want to change later. This discipline pays off immediately: changing a font size for the whole site becomes a one-line edit.

## Try it: Try it

Explore the anatomy of a CSS rule and see how each linking method connects styles to HTML.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/how-css-works

## Check yourself

### Question 1

In `h2 { color: teal; }`, what is `color` called?

- A. A property
- B. A selector
- C. A declaration block
- D. A value

Correct answer: A

A CSS declaration is `property: value;`. Here `color` names what you are setting (the property), and `teal` is what you are setting it to (the value).

- Why not B: The selector is `h2`. It selects which elements the rule targets.
- Why not C: The declaration block is the entire `{ color: teal; }` part, not just `color`.
- Why not D: `teal` is the value. `color` is the property name that `teal` is assigned to.

### Question 2

Which linking method gives the widest reuse and the best caching across multiple pages?

- A. External stylesheet (`<link rel="stylesheet">`)
- B. Internal `<style>` element
- C. Inline `style=` attribute
- D. A `<script>` tag that sets `element.style`

Correct answer: A

An external `.css` file is downloaded once and cached by the browser. Every page that links to it reuses the same cached file, which is why it is the standard approach for real projects.

- Why not B: A `<style>` block is scoped to one HTML file. The browser cannot cache it independently.
- Why not C: Inline styles apply to one element only. They have the highest specificity but zero reuse.
- Why not D: Setting styles via JavaScript is dynamic and correct in specific cases, but it is not a CSS linking method.

### Question 3

You have both a `<link>` stylesheet and a `<style>` block on the same page, and both target `p { color: blue; }` vs `p { color: red; }`. Which color wins (assuming equal specificity)?

- A. The one that appears later in the HTML source
- B. The external stylesheet always wins
- C. The internal `<style>` block always wins
- D. The browser picks randomly

Correct answer: A

CSS resolves conflicts using three criteria: origin (user-agent / author / user), specificity, and source order. When origin and specificity are equal, the declaration that appears later in the source wins. A `<style>` block placed after a `<link>` tag will override it.

- Why not B: Neither external nor internal has a built-in priority advantage. Source order decides when specificity is equal.
- Why not C: Internal styles do not automatically win. Source order is what the browser uses when specificity is equal.
- Why not D: The browser is deterministic. Later source order wins when specificity is equal.

## Remember this

- A CSS rule = selector + `{ property: value; }`. Every declaration ends with a semicolon.
- External `<link>` is cacheable and reusable across pages. Prefer it for real projects.
- Inline `style=` has the highest specificity but zero reuse. Use sparingly.
- When specificity is equal, source order decides: the later declaration wins.
