Difference between ID and Class


#1

https://www.codecademy.com/courses/make-a-website/lessons/closer-look-css/exercises/id-selectors?action=lesson_resume

What is the difference between a class and ID,I believe they will give the same effect .


#2

Hey @marinaselim classes should generally be used for most cases, the ID selector is generally reserved for elements that require a “higher level of specificity”…

When you have a css rule with many selectors it is said to have a higher level of specificity

for example,
.hero li a span
which reads as, selecting the span within the link tags, of an li element, contained by the parent element with a class of "hero"
we could make rules 3x longer than this if we wanted to but that doesnt make it easier for us!

so the ID selector can be assigned to reduce the amount of selectors and simply give the targeted element (in this case “span”) a priority over the class namings


#3

This topic was automatically closed after 7 days. New replies are no longer allowed.