Page 1
Page 1
Started By
Message

Actual difference between css selectors id and class

Posted on 8/7/15 at 8:00 pm
Posted by Casty McBoozer
your mom's fat arse
Member since Sep 2005
35495 posts
Posted on 8/7/15 at 8:00 pm
I haven't messed with html/css in a long time and when I did it was pretty basic stuff.

My goal is to get through some html/css lessons, move into php and then really get into some sql.

Anyway, I'm getting way ahead of myself. I'm going through some lessons on CSS, and it's telling my that the class selector are for many html elements but the id selector is for exactly one html element. But as I play with the code a bit, it seems evident that the class and id tags are interchangeable. I can use the same id tag for any number of html elements, including applying it to the body tag and applying attributes to the whole page.

So, what, technically, is the difference between the tags? Can you show me an example of where one works and the other doesn't? Thanks.
Posted by TigerRagAndrew
Check my style out
Member since Aug 2004
7217 posts
Posted on 8/7/15 at 9:42 pm to
You *can* use duplicate ids in client html code... as in... the page will load. It's not valid html though. Any JS you write won't be able to uniquely identify any elements.
Posted by TigerRagAndrew
Check my style out
Member since Aug 2004
7217 posts
Posted on 8/7/15 at 10:07 pm to
Here's an example fiddle.

LINK /
This post was edited on 8/7/15 at 10:30 pm
Posted by JollyGreenGiant
The Help Board
Member since Jul 2004
24915 posts
Posted on 8/8/15 at 8:22 am to
What he said. Also, if you want to style the entire body, you can just use the body tag. There's no need to id nor class it since it's guaranteed to be the only one on every page.
Posted by Gr8t8s
Member since Oct 2009
2579 posts
Posted on 8/8/15 at 6:17 pm to
Unless you plan on using JS for functions selecting these elements, there's not much point in using id's for low-level work. You only use an id if you very specifically need to target a specific element, ONE time in a page. Using it more can cause problems with validation and make it hard for JS to figure out which element you are trying to use.
first pageprev pagePage 1 of 1Next pagelast page
refresh

Back to top
logoFollow TigerDroppings for LSU Football News
Follow us on Twitter, Facebook and Instagram to get the latest updates on LSU Football and Recruiting.

FacebookTwitterInstagram