HTML offers multiple ways to select and style elements.
- Two of the most commonly used selectors are IDs and Classes
- An ID is an attribute, a unique identifier assigned to only one HTML element within a page.
- It is often used for unique styling and JavaScript manipulations.
<div id="myUniqueID">This is a div with an ID.</div>
- The class attribute lets you give the same name to multiple HTML elements. That way, you can easily change their look or behavior all at once.
- Classes are not unique and can be assigned to multiple elements.
- They are generally used for applying the same styles or behaviors to a group of elements.
<div class="myClass">This is a div with a class.</div>
<p class="myClass">This is a paragraph with the same class.</p>
Differences Between IDs and Classes
- Uniqueness: IDs are unique, and classes can be reused.
- JavaScript: IDs are often used for JavaScript operations.
- Styling: Both can be used for styling, but IDs have higher specificity.