Key takeaway
- Class vs ID selector in CSS: syntax, naming rules, specificity and when to use each, with a comparison table and tips for maintainable stylesheets.
A class selector (.name) matches every element whose class attribute contains that name, so one rule can style a whole group. An ID selector (#name) matches the one element whose id attribute has that value, and the value must be unique in the document. Use classes for styling and reuse, and keep IDs for anchors, labels and script hooks.
The practical difference is weight. According to MDN, one ID selector outranks any number of class selectors, which is why IDs make stylesheets hard to override. The sections below cover the syntax, the naming rules, the specificity arithmetic and the habits that keep CSS maintainable across teams.
What is the difference between a class and an ID selector?
| Point | Class selector | ID selector |
|---|---|---|
| Syntax | .card { } | #header { } |
| Matches | Every element with that class in its class list | The element whose id equals the value |
| Uniqueness | Reusable on any number of elements | The value must be unique within the document |
| Per element | An element can carry several classes, separated by spaces | An element has one id, and it cannot contain whitespace |
| Specificity | 0-1-0 | 1-0-0 |
| Equivalent attribute selector | [class~="card"] | [id="header"] (same match, lower specificity) |
| Best used for | Styling and reusable components | Fragment links, form labels, script hooks |
Sources: MDN pages on class selectors, ID selectors and specificity, read 13 November 2026.

How does a class selector work?
A class selector starts with a full stop followed by the class name. The browser applies the rule to every element whose class attribute lists that name. The class attribute holds a list of names separated by whitespace, so one element can take several classes, as MDN describes.
/* one rule, many elements */
.note { border-left: 4px solid #2a5bd7; }
/* an element must carry both classes */
li.spacious.elegant { margin: 2em; }
Chaining classes with no space between them requires all of them on the same element. A space changes the meaning: .red.fancy matches one element with both classes, while .red .fancy matches a .fancy element inside a .red element.
Naming rules for classes
- Class values may contain any character except ASCII whitespace, but a name used in a selector must be a valid CSS identifier. A name such as
123itemoritem?onemust be escaped in the selector, for example.\31 23item, or the rule is ignored. - MDN recommends names that describe purpose, such as
warning, rather than appearance, such asred-text, so the name stays correct if the design changes.
How does an ID selector work?
An ID selector starts with a hash character followed by the value of the id attribute. It matches the element with that exact value.
#site-header { position: sticky; top: 0; }Rules for ID values
- The value must be unique within the whole document, and an element has only one.
- It must not contain whitespace. Unlike
class, anidcannot hold a list. - Technically it can contain any Unicode character, but for use in CSS or
querySelector()it must be a valid CSS identifier. Otherwise escape it withCSS.escape(). MDN recommends choosing values that need no escaping. Source: MDN, the id attribute.
A common convention is to start IDs and class names with a letter and use only letters, digits, hyphens and underscores. This is stricter than HTML requires, but such names need no escaping in selectors.
Where are classes and IDs used besides styling?
| Use | Class | ID |
|---|---|---|
| Styling | Preferred for almost all rules | Possible, but carries high specificity |
| Fragment links | Not applicable | <a href="#exciting"> jumps to the element with id="exciting" |
| JavaScript lookup | document.getElementsByClassName("note"), querySelectorAll(".note") | document.getElementById("exciting"), querySelector("#exciting") |
| Changing state | element.classList.add("active") or toggle | Not used for state |
MDN advises against reading IDs as properties of window, because they can collide with existing or future browser globals. Use getElementById() or querySelector() instead. The method is getElementsByClassName, with an s, and it returns a live collection.
Which selector wins when both apply?
Specificity is a three-column value, ID then class then type. MDN lists what counts in each column: ID selectors in the first, class selectors, attribute selectors and pseudo-classes in the second, and type selectors and pseudo-elements in the third. Columns are compared left to right, so one ID beats any number of classes. If all three tie, the rule declared last wins.
#panel { color: green; } /* 1-0-0, wins */
.a .b .c .d { color: yellow; } /* 0-4-0 */
| Situation | What MDN says |
|---|---|
| Inline style attribute | Beats normal author rules; only !important overrides it |
!important | Overrides specificity within the same origin and layer, and MDN calls using it to win specificity fights a bad practice |
:where() | Always adds 0-0-0, useful for low-specificity base styles |
:is(), :not(), :has() | Add nothing themselves, but their most specific argument counts |
Cascade layers (@layer) | Later layers win without specificity tricks; unlayered styles beat layered ones from the same origin |
Source: MDN, specificity, read 13 November 2026.
When should you use a class and when an ID?
| Need | Choose | Why |
|---|---|---|
| Style a component used more than once | Class | Reusable and low specificity |
| Style one element in a shared stylesheet | Class | Avoids a high-specificity rule that later rules must fight |
| Link to a section of the page | ID | A fragment link needs an element with that id |
| Associate a form label with a field | ID | The for attribute refers to an id |
| Reach one element from a script | ID or class | getElementById is direct; a data attribute or class also works |
| Override a third-party style | Class, then layers | Prefer cascade layers or :where() over IDs and !important |
Which habits keep large stylesheets maintainable?
- Style with classes and keep specificity low, so later rules can override earlier ones without escalation.
- Keep IDs for anchors, labels and script hooks, and keep them unique. Duplicate IDs are invalid and make lookups unreliable.
- Name classes by purpose, not appearance.
- Use
classListto change state in scripts, not inline styles, so the stylesheet stays the single source of styling. - Reach for cascade layers or
:where()before!important.
Related reading on CSS: CSS content-visibility, CSS animation keyframes, fluid typography with clamp() and CSS mask and clip-path. If you work in React, see functional vs class components, which concerns a different meaning of "class".
Why does this matter beyond one stylesheet?
For engineering leads, selector rules are a small part of a delivery standard: shared naming conventions, low specificity and review checks keep front-end code easy to change across teams. The same idea of written conventions that are enforced, not remembered, applies when teams build agent workflows. ElixirClaw is an agentic OS platform to build, orchestrate and govern AI agents and automation workflows while keeping humans in control.
To map one engineering decision your agents should make, and the context and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is the main difference between a class and an ID in CSS?
A class can be applied to many elements and an element can have several classes. An ID must be unique in the document, and an element has one. A class selector starts with a full stop and an ID selector with a hash.
Can two elements share the same ID?
No. The id value must be unique within the document. Duplicate IDs are invalid HTML, so use a class when more than one element needs the same styling.
Can an element have more than one class?
Yes. The class attribute is a list of names separated by whitespace, and a selector such as li.spacious.elegant matches only elements that carry both.
Which has higher specificity, a class or an ID?
An ID. Specificity is compared ID first, then class, then type, so one ID selector (1-0-0) outranks any number of class selectors (0-1-0 each), according to MDN.
Should I avoid ID selectors in CSS?
For styling, mostly yes, because their high specificity makes rules hard to override. IDs remain the right choice for fragment links, label associations and script hooks.
What is the correct JavaScript method to find elements by class?
document.getElementsByClassName("name") or querySelectorAll(".name"). For an ID use getElementById("name") or querySelector("#name").
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE