What is the difference between class and id selectors in CSS?
In CSS, both class and ID selectors are fundamental tools used to target specific HTML elements for styling. While they might seem similar at a glance, they have crucial differences in their purpose, application, and specificity that dictate when and how they should be used.
Class Selector
A class selector allows you to select HTML elements that have a specific class attribute. Classes are designed to be reusable; multiple HTML elements can share the same class, and a single HTML element can have multiple classes. This makes them ideal for applying a common set of styles to many elements across a webpage or even an entire website.
Syntax: In CSS, a class selector is denoted by a dot ('.') followed by the class name. In HTML, elements are assigned a class using the 'class' attribute.
.my-class {
color: blue;
font-size: 16px;
}
<p class="my-class">This is a blue paragraph.</p>
<div class="my-class">This is also blue.</div>
ID Selector
An ID selector targets a unique HTML element identified by its ID attribute. The fundamental rule for IDs is that each ID must be unique within a single HTML document. You should not have more than one element with the same ID on a page. IDs are typically used for styling very specific, single instances of an element, or as targets for JavaScript manipulation.
Syntax: In CSS, an ID selector is denoted by a hash ('#') followed by the ID name. In HTML, an element is assigned an ID using the 'id' attribute.
#main-header {
background-color: #eee;
padding: 20px;
}
<header id="main-header">Welcome to my site</header>
Key Differences Summary
| Feature | Class Selector | ID Selector |
|---|---|---|
| Uniqueness | Can be used multiple times on a single page; an element can have multiple classes. | Must be unique within a single HTML document; an element can only have one ID. |
| Reusability | Highly reusable; designed for applying consistent styles across many elements. | Not intended for reuse; targets a single, specific element. |
| Specificity | Lower specificity (e.g., `element.class`). | Higher specificity than classes (e.g., `#id`). |
| Syntax | CSS: `.classname`, HTML: `class="classname"` | CSS: `#idname`, HTML: `id="idname"` |
| Use Cases | Common styles, UI components, themes, utility classes. | Unique page sections (e.g., `#header`, `#footer`), JavaScript hooks, fragment identifiers in URLs. |
When to Use Which
- Use classes when you need to apply the same style to multiple elements, or when an element might need to change its styling based on its state or context (e.g.,
.active,.disabled). They are the go-to for most styling. - Use IDs sparingly for styling. Reserve them for elements that are truly unique on the page and are integral to its structure (e.g., a main content area). IDs are more powerful for JavaScript to target a specific element or for creating internal page links (e.g.,
example.com/page#section). - Prioritize classes over IDs for styling to maintain a more flexible and maintainable CSS codebase. Over-reliance on IDs can lead to specificity issues and make your CSS harder to override and reuse.