Class Vs Id

The Never Ending Battle

11/29/2014

This week we have spent a lot of time learning about the basic technologies that make websites possible. Specifically Html and CSS. They are two different internet languages that go hand and hand with each other. The best way to think of them is as HTML as the skeleton and CSS the skin. HTML provides the structure for your site while CSS makes it smooth and pretty. A concept the CSS uses extensively to make this happen is the class and id selectors.

Both class and id selectors are used by css to, surprise, select elements in HTML. You would write a tag in HTML and then you would add a class or id to that tag. Now with these added, you could write CSS code that targets this class or id and only what is inside those divs will be affected. So far though, they seem pretty interchangeable. Why would I want to use one over the other?

Here is where they differ. Id's are unique. Meaning each element may only have one Id and that Id may only show up once per page. You would use the Id when you want to target something very specific from everything else. Classes on the other hand can be used multiple times in multiple places. You are going to want to use classes to several different things that you want to have the same css affecting them. If you can just live by the rule "id once, but class over and over", you will be in good shape.

Until next time,
Zac Barnes