IDs vs Classes in HTML

When I first started learning HTML the differences between IDs and classes wasn’t very clear to me and I would mix them up often. I thought they were interchangeable, and while they do very similar things they have their differences and advantages. Here’s a refresher!

Classes and IDs are used to identify HTML elements, and are used in conjunction with CSS to present the elements in different ways. IDs are unique and can only be applied to one element, whereas classes can be applied to multiple elements.

IDs should be used when only that element on the page will be styled that way. Because it is a unique selector you don’t want to use it on an element that will be repeated on the page. If you are planning on styling multiple elements on a page in the same way, that is when a class selector should be used.

I hope this overview of IDs vs Classes was helpful for you and guides you to use the correct selector in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *