HTML How to Make a Website Adding Pages to a Website Add Iconography

Ryan Schmelter
Ryan Schmelter
9,710 Points

Why not use ID instead of class for phone, email, and twitter?

Can anyone explain why ID isn't used in this case? These classes are being used anywhere else on the site.

2 Answers

Those could be ID. Actually, it should be the case, the classes are too specific for those elements. But I think it's done for didactic purposes.

Evgeniia Mas
Evgeniia Mas
4,448 Points

Hello! I think it is really a developer choice depending on project's features and selectors differencies. If you are going to give the same styles to different elements (same font-size, font-family and so on), you should give them the same class and therefore avoid CSS duplication. If you need to have a specific styles or going to do something with any element, you'd better choose id. As a unique selector (which can be used with that name only once on the one page) it gives you excelent oppotunity to select only that element and for example change it color or use any javascript function - change phone if user selected another city and like that. Using id you wil be sure that only one element is selected. Also you can assign id and class to one element at once and use each selector accordingly. Remember that styles assigned in id are "more important" as they has priority to class styles if there is a conflict between them (using same styles to the same element). So using right selectors (not randomly) is a good practice, helping to avoid possible problems and extra large CSS file. I advise you keep this in mind when you choose the type of selector.