What is the difference between id and class selector in CSS?

An id must be unique on the page and is selected with #. A class can be used on many elements and is selected with a dot ..

Pointidclass
Selector#header.note
ElementsOneMany
SpecificityHigher (100)Lower (10)
#header { background: navy; }
.note { color: gray; }

Use classes for styling in most cases, and keep ids for page anchors and JavaScript. See CSS Specificity.


Learn more about the similar topics:
Tutorials
CSS Selectors
CSS Specificity
CSS Pseudo-classes and Pseudo-elements
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is the CSS class selector?
What is grouping in CSS and what is it used for?
What are child selectors in CSS?
What is the difference between id and class selector in CSS?
What is specificity in CSS?
What does !important do in CSS?
What are pseudo-classes and pseudo-elements in CSS?