What is the difference between a class and an ID in CSS?

1 Answers
Answered by suresh

What is the difference between a class and an ID in CSS?

The Difference Between a Class and an ID in CSS

Classes and IDs are both used in CSS to apply styles to elements on a webpage, but they have some key differences:

  • Class: A class in CSS is indicated by a dot followed by the class name (e.g., .my-class). Classes can be used multiple times on a webpage, allowing you to apply the same styles to multiple elements.
  • ID: An ID in CSS is indicated by a hashtag followed by the ID name (e.g., #my-id). IDs are unique and can only be used once on a webpage, making them ideal for styling a specific element.

It is important to note that classes are more versatile and commonly used for styling elements, while IDs are typically used for unique, one-off styling situations.

Answer for Question: What is the difference between a class and an ID in CSS?