What CSS Actually Does
CSS (Cascading Style Sheets) is the language that controls how HTML elements look and are positioned on a page. HTML says "this is a heading" and "this is a list" — CSS says how big that heading is, what color it is, and how much space sits around it.
- ✓HTML is structure and meaning. CSS is presentation and layout — they're deliberately separate.
- ✓A rule is made of a selector (what to style) and a declaration block (how to style it).
- ✓"Cascading" means multiple rules can apply to one element, and CSS has clear rules for which one wins.
Three Ways to Write CSS
CSS can live in three places. In real projects you'll almost always reach for the third one, but it helps to recognize all three.
style="..." written directly on one element. Quick to test, hard to maintain — avoid it in real projects.
A <style> block inside <head>. Fine for a single-page demo or a quick prototype.
A separate .css file linked with <link>. The standard approach — one stylesheet, reused across every page.
<!-- External stylesheet: the method you'll use going forward --> <head> <link rel="stylesheet" href="style.css"> </head>
/* A rule: selector + declaration block */ p { color: navy; font-size: 16px; }
Targeting the Right Elements
A selector decides which elements a rule applies to. These four are the ones you'll reach for constantly.
Element selector — every <p> on the page.
Class selector — every element with class="card". Reusable across many elements.
ID selector — the one element with id="main". IDs must be unique per page.
Universal selector — every element on the page.
/* Element selector */ h1 { color: #e8590c; } /* Class selector — reusable */ .card { padding: 16px; } /* ID selector — one element only */ #main { max-width: 800px; } /* Combining a tag with a class */ li.active { font-weight: bold; }
Use a class when the style might apply to more than one element — most of the time, this is what you want. Reach for an id only when you need to target one specific, unique element.
Selecting by Relationship
Selectors can also target elements based on where they sit relative to another element.
/* Descendant: any span inside .card, at any depth */ .card span { color: gray; } /* Direct child: only li that is a direct child of ul */ ul > li { list-style: none; } /* Grouping: apply one rule to several selectors at once */ h1, h2, h3 { font-family: sans-serif; }
When Rules Compete: Specificity
When more than one rule targets the same element, CSS uses specificity to decide which wins — not just the order the rules were written in. As a rough scale: an id beats a class, a class beats an element selector, and a later rule of equal specificity beats an earlier one.
If a <p> has both class="intro" and id="lead", it renders orange — the id rule wins regardless of where the rules appear in the file.
Breaking Down What You Just Learned
Rule structureEvery CSS rule pairs a selector with a declaration block of property: value pairs.
External stylesheetsLink a .css file with <link> so styles stay separate from markup and reusable across pages.
Element, class, idThree core selector types, from broadest (every tag) to narrowest (one unique element).
CombinatorsDescendant and child selectors target elements based on their position in the HTML.
SpecificityWhen rules conflict, ids beat classes and classes beat tag selectors.
Try It Yourself
Take the static page you built on Day 15, create a linked style.css, and give the heading, one paragraph class, and one unique element three different colors using an element, a class, and an id selector.
Style a Page With Three Selector Types
Wiring up your first stylesheet
Create day16.html and style.css in the same folder and connect them together.
- Link style.css inside the <head> of day16.html using <link rel="stylesheet">.
- Build a page with a heading, two paragraphs, and a list of three items.
- Write an element selector rule that styles all paragraphs.
- Add a class to one paragraph and give it its own class selector rule.
- Add a unique id to the heading and style it with an id selector.
- Use a descendant selector to style only the list items inside your list.
Recap
A selector paired with a declaration block of property: value pairs.
The standard way to add CSS — one .css file linked to every page.
Element, class, and id — each with a different scope.
Decides which rule wins when more than one rule targets the same element.
Key Takeaways
- CSS controls presentation; HTML stays focused on structure and meaning.
- External stylesheets, linked with <link>, are the standard way to add CSS to a page.
- Element, class, and id selectors target elements at different levels of scope.
- Descendant and child combinators select elements based on their position in the markup.
- Specificity — not just rule order — decides which rule wins when two rules conflict.