CSS ID vs Class- Understanding the Difference
What Is the Difference Between CSS ID and Class?
If you've been writing CSS for any length of time, you've probably used both ID and class selectors. Most tutorials throw both at you and call it a day. That's lazy. Here's what actually matters:
Classes are reusable. IDs are unique. That's the core difference, but there's more nuance than that simple statement suggests.
Using the wrong selector in the wrong situation will cost you time, create specificity wars, and make your code harder to maintain. This guide cuts through the noise.
CSS ID Selector: The Basics
An ID selector targets a single, specific element on your page. You define it in CSS with a hash symbol:
#header { background: #333; }
And apply it in HTML with the id attribute:
<div id="header">Content</div>
Key characteristics of IDs:
- Must be unique on the page — no two elements should share the same ID
- You can only use each ID once per HTML document
- Higher specificity than classes
- Often used for JavaScript hooks, anchor links, and layout containers
CSS Class Selector: The Basics
A class selector can target multiple elements at once. You define it with a dot:
.btn { padding: 10px 20px; }
Apply it in HTML with the class attribute:
<button class="btn">Submit</button>
<button class="btn">Cancel</button>
Key characteristics of classes:
- Reusable across unlimited elements
- Multiple elements can share the same class
- One element can have multiple classes
- Lower specificity than IDs
- Preferred for styling reusable components
Specificity: Why It Matters
Specificity determines which CSS rule wins when multiple rules target the same element. Here's the hierarchy from lowest to highest:
- Type selectors (p, div, span) and pseudo-elements (::before)
- Class selectors (.btn), attribute selectors ([type="text"]), and pseudo-classes (:hover)
- ID selectors (#header)
- Inline styles (style="...")
- !important (avoid this)
IDs beat classes. Every time. If you have:
#nav { color: white; }
.nav { color: black; }
The nav will be white, regardless of which rule appears later in your stylesheet.
This is why IDs cause problems. They override everything except inline styles or !important. Classes give you more flexibility because you can layer styles without fighting specificity.
Can One Element Have Both?
Yes. An element can have both an ID and a class:
<div id="sidebar" class="widget container">
But this doesn't mean you should use both for styling. Pick one. Classes for styles, IDs for JavaScript hooks or anchor targets is the conventional approach.
ID vs Class: Direct Comparison
| Feature | ID Selector (#) | Class Selector (.) |
|---|---|---|
| Uniqueness | One per page | Unlimited reuse |
| Specificity | Higher | Lower |
| JavaScript | Perfect for getElementById | Works with querySelectorAll |
| Styling multiple elements | No — use separate IDs | Yes — single class |
| Multiple per element | No | Yes (class="a b c") |
| Anchor links | Yes (#section-name) | No |
When to Use ID Selectors
IDs aren't evil. They have legitimate use cases:
- JavaScript targeting — getElementById is the fastest DOM method. Use IDs for elements you'll manipulate with JavaScript.
- Anchor links — The href="#contact" pattern only works with IDs. You can't link to a class.
- Unique page sections — Header, footer, sidebar, main content. These appear once and need distinct targeting.
- Form labels and inputs — Label's for attribute links to an input's ID.
That's it. If you're not doing one of those things, use a class.
When to Use Class Selectors
Classes are your default choice. Use them when:
- You need to style multiple elements the same way
- You're building reusable components (buttons, cards, alerts)
- You want to layer styles without specificity conflicts
- You're using CSS frameworks like Tailwind or Bootstrap
Buttons are the classic example. You don't create #primary-button, #secondary-button, #submit-button. You create .btn and .btn-primary.
Common Mistakes to Avoid
Mistake 1: Using IDs for Everything
Newcomers often give every styled element a unique ID because it "works." Then they spend hours fighting specificity when they want to override styles. Classes are more forgiving.
Mistake 2: Using IDs for Reusable Styles
If you have ten buttons that all need the same padding, you don't give each one a different ID. That's ten times the CSS and ten times the maintenance headache.
Mistake 3: Using Generic IDs Like #content or #main
These names are too vague. #site-header, #hero-section, #product-grid — be specific. It makes debugging easier when you're working with DevTools.
Mistake 4: Using Spaces in Class Names
HTML allows multiple classes: class="btn primary large". That's fine. But class="btn primary" with a space between "btn" and "primary" creates two separate classes. If you meant "btn-primary" as one class, use a hyphen.
How to Choose: Decision Flowchart
When you're unsure what to use, ask yourself these questions in order:
- Will this element appear more than once on the page? → Use a class.
- Will JavaScript need to target this element? → Use an ID.
- Is this a link target (anchor)? → Use an ID.
- Is this a unique structural container (header, footer)? → ID is acceptable.
- Otherwise → Default to a class.
Getting Started: Practical Example
Let's build a simple card component to see ID and class in action:
/* CSS */
.card { border: 1px solid #ddd; padding: 20px; }
.card-title { font-size: 1.5rem; font-weight: bold; }
.card-image { width: 100%; height: auto; }
.card-featured { border-color: gold; }
/* HTML */
<article class="card card-featured" id="featured-post">
<img class="card-image" src="photo.jpg" alt="Post image">
<h2 class="card-title">Featured Article</h2>
<p>Content here...</p>
</article>
Notice the pattern:
- .card — base styles for all cards
- .card-title, .card-image — component-specific sub-styles
- .card-featured — modifier class for the featured variant
- #featured-post — ID for JavaScript targeting (if needed)
Classes handle styling. The ID sits there for JavaScript. Clean separation.
The Bottom Line
Classes are for styling. IDs are for identification. That's the rule. Classes win because they're reusable, composable, and don't create specificity nightmares.
Use IDs when you need JavaScript hooks, anchor links, or genuinely unique elements. Use classes for everything else. Stop overthinking it.