Class and ID selectors in CSS

Key takeaways:

  • Understanding class and ID selectors in CSS allows developers to target specific HTML elements with precision, leading to more organized and maintainable code.

  • Unlike ID selectors, which are unique, class selectors can be applied to multiple elements.

  • ID selectors are ideal for targeting single, unique elements within a webpage. They are often used for elements that require distinct styling or for JavaScript interactions.

  • Utilizing both class and ID selectors together allows for more control over element styling. This combination enables developers to apply general styles with classes while reserving IDs for unique adjustments.

CSS selectors are used to find the HTML elements you want to style on a web page. They form the core of CSS, enabling developers to apply styles selectively and efficiently. Among the various types of selectors, class and ID selectors are the most commonly used due to their straightforward syntax and because you can present the same HTML element differently, depending on its class or ID.

Understanding CSS class selectors

Class selectors are one of the most widely used tools in CSS. They allow developers to apply the same style rules to multiple elements across a web page, promoting consistency and reducing code duplication. It matches all the HTML elements based on the contents of their class attribute.

Syntax

A period (.) followed by the class name is how a class selector is defined. For example:

.class-name {
/* Define properties here */
}
Syntax for a class selector

Let's look at a simple working example of using class selectors. In the code below, we apply the same styles to two buttons using the same class.

Advantages of class selectors

Here are some of the main advantages of using class selectors:

  • They ensure reusability whereby we can apply the same styles to multiple elements without repeating code.

  • They are easy to maintain as it is easy to update styles in one place (in a particular class), affecting all elements that use that class.

  • They allow us to combine multiple classes on a single element for complex styling.

Disadvantages of class selectors

  • Excessive use of classes can lead to bloated HTML with too many class names.

  • Without a consistent naming convention, classes can conflict, making maintenance difficult.

  • Class selectors are less specific than ID selectors, which can lead to challenges when trying to override styles.

Want to get hands-on practice with class selectors in a real-world application? Try out this project, Build a Microblogging App Using PHP, HTML, JavaScript, and CSS, where we create a microblogging platform with search and admin features.

Let's look at another example where we combine multiple classes on a single element:

In the code above, we ensure that all buttons share common styles defined in the .button class while allowing for specific variations through additional classes for each button—primary, secondary and danger. The .button class is applied to each button, while additional classes like .primary, .secondary, and .danger provide specific variations.

Understanding CSS ID selectors

ID selectors are used to target unique elements on a web page. Unlike classes, an ID must be unique within the HTML document, making it ideal for styling or scripting individual elements. It matches the HTML element based on the contents of its id attribute.

Syntax

A hash (#) followed by the ID name is how an ID selector is defined. For example:

#idname {
/* Define properties here */
}
Syntax for an ID selector

Let’s look at a simple working example of using ID selectors. In the code below, we apply the styles to a <div> element using an ID selector.

Advantages of ID selectors

  • ID selectors ensure uniqueness as the styles or scripts apply to a single, specific element in the HTML document.

  • They offer higher specificity than class selectors, allowing for precise styling overrides.

  • They are most commonly used to select elements for dynamic interactions using JavaScript.

Disadvantages of ID selectors

  • They offer limited reusability because IDs can only be used once per page, limiting their use in styling multiple elements.

  • Unique IDs can make CSS less flexible and harder to maintain in large projects.

  • They can lead to unexpected behavior and styling issues if we accidentally duplicate IDs.

Practice CSS selectors by trying out this project, Build an Image Sharing App with MERN Stack, where we create a full stack image sharing application focusing on styling the application.

Let's update our example of creating a header but this time with multiple heading elements with one being targeted using an ID selector.

The <h2> element with id="subheading" is targeted using the #subheading selector in CSS. The other <h2> element remains unaffected because it doesn’t have the id="subheading", demonstrating how IDs uniquely style specific elements.

Key differences between class and ID selectors

While both class and ID selectors are essential in CSS, they serve different purposes and have distinct characteristics. Let's summarize them in the table below:

Feature

Class selector

ID selector

Syntax

Defined by a period (.) followed by the class name, .button.

Defined by a hash (#) followed by the ID name, #primary.

Reusability

It can be applied to multiple elements on the same web page.

It must be unique to one element on the web page.

Use cases

Used for grouping multiple elements that share the same style.

Used to target a specific, unique element.

HTML attribute

Assigned with class attribute, e.g., <div class="example">

Assigned with id attribute, e.g., <div id="example">

Conclusively, understanding class and ID selectors in CSS is crucial for creating well-structured and maintainable web designs. While class selectors offer flexibility and reusability across multiple elements, ID selectors provide unique targeting for specific elements. Consider using class selectors when you need to apply the same style to multiple elements. They are ideal for components like buttons, cards, and form elements that share common styling. Use ID selectors for elements that require unique styling or are targeted by JavaScript for dynamic behaviors, such as navigation bars, footers, or specific webpage sections.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is the difference between class and ID selectors in CSS?

Class selectors can be applied to multiple elements and are used for reusable styles, whereas ID selectors are unique to a single element and are typically used for specific styling or JavaScript interactions.


How do I choose between using a class or an ID in CSS?

Use a class when you need to apply the same style to multiple elements. Use an ID when you want to target a unique element that requires distinct styling or functionality.


Can I use both class and ID selectors on the same element?

Yes, you can assign both class and ID selectors to a single element to combine their styles and behaviors. For example: <button class="btn primary" id="submit-btn">Submit</button>


How does CSS specificity work with class and ID selectors?

ID selectors have higher specificity than class selectors. If both are applied to the same element, the ID selector’s styles will override the class selector’s styles unless explicitly overridden by inline styles or !important.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved