ProgramGuru

How CSS Works

How CSS Works

Understand how the browser decides which elements a CSS rule affects and where the displayed result comes from.

What you’ll learn

  • Describe selector matching and computed styles.
  • Predict which class-bearing elements a rule affects.
  • Distinguish the HTML structure from the visible result.

Understand the concept

The browser parses HTML into a document tree and CSS into style rules. Each rule has a selector and declarations. Selectors are tested against document elements; matching declarations contribute to the computed style.

Several declarations can target the same element. The browser resolves conflicts using cascade rules and inheritance, which we will study separately.

Visualize the relationship

HTML elementsMatch selectorsApply declarationsPaint resultOnly matching elements receive a rule’s declarations.
Only matching elements receive a rule’s declarations.

One matching element

Only the paragraph with class notice matches.

HTML

<p class="notice">Shipping is free today.</p><p>Offer ends Sunday.</p>

CSS

.notice { color: #1d4ed8; font-weight: 700; }

Rendered output

Shipping is free today.

Offer ends Sunday.

Observe: The first paragraph becomes blue and bold; the second keeps its default color.

Multiple matching elements

A single selector can match more than one element.

HTML

<p class="notice">Free shipping</p><p class="notice">Easy returns</p><p>Terms apply.</p>

CSS

.notice { color: #0f766e; background: #ccfbf1; padding: 8px; }

Rendered output

Free shipping

Easy returns

Terms apply.

Observe: Both notice paragraphs share the styling because both carry the same class.

Explain the result

For <code>.notice { color: ... }</code>, <code>.notice</code> finds matching elements, and <code>color</code> sets the text color of each matched element.

Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.

Common mistakes to avoid

  • Do not assume the browser applies rules merely because the CSS file exists; selectors must match.
  • A later conflicting declaration can change the effective style of an element.

Try it yourself

Add the class notice to the final paragraph. Predict exactly which three elements will use the teal rule.

Before changing the code, predict what will change. Then verify by checking the rendered result.

Check your understanding

What makes a CSS rule apply to an HTML element?
Choose one answer. You can try again.

Answer and explanation

The element matches its selector

The selector determines which elements are candidates for a rule.

What you should remember

Think in three stages: HTML elements, selector matching, and computed visual styles.

Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: CSS Syntax and Rules →