ID and Class in HTML: What’s the difference?

Search for a command to run...

This is amazing, thanks for sharing!
Ten Crucial Tips for Seniors and Leads

As part of my training, I'm working on creating a smaller user microservice for a fake property website. After crafting the basic controller/service/repository and the testing alongside it, we were tasked with dockerising both the application and the...

I have been so excited to learn Docker. For so long, it's been a name floating around every tech space I've inhabited, but I haven't had the time to sit down and look into the details of what it is or how it works in a practical sense. Today, I took...

Today I announced my first official job as a developer after accepting a position as Junior Software Engineer with Novatec GmbH. I see this job as a long time coming, and the culmination of years of interest in technology and coding, but the truth is...

Follow Me on Twitter @AnnaJMcDougall One of the really cool things I'm discovering about NodeJS is that it allows us to interact more directly with computers, and enables the production of tools using the CLI (Command Line Interface: you may know it ...

A lot of beginners struggle with the difference between class and ID in HTML. Both are used to identify HTML elements, and both are often used to apply styling, but there are a few key differences.
Simply put, the rule of thumb is this:
Do you want someone else to have your ID? No! It’s yours! You are a unique individual, and even if you have an identical twin who is genetically the same as you, your ID still belongs to you. Similarly, in HTML we use ID to specify an element which is unique or only occurs once on the page. This is helpful not only for styling individual elements, but it becomes extremely important once you start integrating JavaScript, because it allows you to quickly and easily interact with specific page elements.
Ever heard of a class-action lawsuit? It’s when multiple people bind together to sue someone because they’re all affected by something. Similarly, in HTML we use class to bind together multiple elements so they can all benefit from our styling in the same way.
If you were to apply ‘class’ to individual elements, and then later try to work with those elements in JavaScript, you will have to access them in a completely different (and less direct) way which can be a real pain especially when you first start using DOM manipulation.
Let’s have a look at a quick example. Let’s say I have two lists of items to buy at the supermarket: things I know I can afford, and things which are luxuries which I’ll order if I have the money that week.
The HTML and result of these lists could look something like this, without styling:

Now let’s say that I want to give my two list headings different fonts: ‘Staple Items’ will look basic and use Arial, while ‘Luxuries’ will look a bit nicer with a custom Google font called ‘Satisfy’. I’m also having a party on Saturday, so there are some items which are really important, regardless of their cost, and I want those items to be coloured red so that I don’t forget to buy them: milk, butter, brie, and wine. Based on this information, take a moment and think about which elements should be given an ID, and which should be given a class.
Our fonts are going to apply to individual headers, so we should give them each an ID. Meanwhile, our important items will be marked with a class called ‘important’, because we have four items which all need the same styling. In the end, this is what we end up with:

It might not be pretty, but it proves our point!
You want to apply a style to each of these elements. Which of these would take ID, and which Class?