Classes and IDs give your elements names — so CSS and links can find them precisely.
idclass (e.g. page-section)href=“#section-id”) that jump to each sectionid vs class by using both correctlyclass — Group Membership<article class="post-card">
<h3 class="post-title">The Rise of Brutalist Web Design</h3>
<p class="post-excerpt">An aesthetic movement that rejects decoration...</p>
</article>
<article class="post-card">
<h3 class="post-title">Typography Rules for Developers</h3>
<p class="post-excerpt">The five rules designers wish coders knew...</p>
</article>
One CSS rule styles both cards:
.post-card {
border: 1px solid #ccc;
padding: 1.5rem;
border-radius: 6px;
}
Multiple classes on one element — separate them with spaces:
<!-- This card is both a "post-card" and "featured" -->
<article class="post-card featured">
<h3 class="post-title">Editor's Pick</h3>
</article>
.featured {
border-color: #b7142e;
background: #fff5f6;
}
id — Unique Identity<header id="site-header">
<nav id="main-nav">
<a href="/">Home</a>
<a href="/work">Work</a>
<a href="/contact">Contact</a>
</nav>
</header>
<main id="page-content">
...
</main>
<footer id="site-footer">
...
</footer>
IDs serve two purposes:
#site-header { ... } selects that one element<a href="#contact"> jumps to the element with id="contact"Use class when… | Use id when… |
|---|---|
| Multiple elements share a visual pattern | There’s only ever one of this element per page |
| You’re defining a reusable component | You need a page anchor (href="#section-name") |
| You want to combine styles (card + featured + dark) | You need a guaranteed unique selector |
class for almost everythingid sparingly — mainly for navigation anchors and major page landmarks<div> Container<div> to group elements for layout or styling<div> has no visual appearance and no semantic meaning — it’s a blank container<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
</div>
Without classes, these divs are invisible and identical. With classes, they become a named, styleable system.
btn-primary, card, and text-muted create a shared vocabulary for the whole teamred-box or left-column become misleading when the design changes