Junior
What are the differences between classes and IDs in CSS?
sobes.tech AI
Answer from AI
- Uniqueness: ID is unique on the page, while a class can be applied to multiple elements.
- Specificity Priority: ID has a higher specificity priority (100) compared to class (10).
- Selectors: ID selector is denoted by
#, class selector by.. - Usage: ID is usually used for styling individual, unique elements (e.g., header, footer), classes for styling groups of elements with common properties (e.g., buttons of different colors).
/* Styling by ID - applied only to one element */
#header {
background-color: blue;
}
/* Styling by class - applied to multiple elements */
.button-primary {
color: white;
background-color: green;
}
| Attribute | Class | ID |
|---|---|---|
| Uniqueness | Not unique | Unique |
| Priority | 10 | 100 |
| Syntax | .class-name |
#id-name |
| Application | Groups of elements | Unique elements |