this post was submitted on
3 points (71% like it)
5 up votes 2 down votes
all 26 comments

[–]maloney7 8 points9 points ago*

I use IDs for JavaScript hooks and for naming different sections and containers. I use classes for reusable styles.

Strictly speaking, neither are necessary as CSS selectors such as nth-of-type, ~ and > (amongst others) can pick out any element you like.

[–]x-skeww 0 points1 point ago

CSS selectors such as nth-type, ~ and >

nth-of-type, nth-child, and the like are pseudo classes. ~, >, +, and ' ' (space) are combinators.

[–]Legolas-the-elf 9 points10 points ago

Stop thinking about IDs and classes in terms of CSS. They are first and foremost part of HTML.

When you use an ID, you are talking about a particular element. When you use a class, you are talking about a particular group of elements. Use the id and class attributes in this way.

A class that is only applied to one element is not equivalent to an ID. It means you are talking about a group of elements that happens to only contain one member. You're talking about a group of elements, not an element.

Using class for everything is like using <div> for everything. Sure, you can do that, but that doesn't mean it's a good idea. id and class have different semantics.

[–]Ibuildwebstuff 0 points1 point ago

I'd argue in this case performance is more important than semantics. Benchmarks.

[–]Legolas-the-elf 0 points1 point ago

I think worrying over the difference in the amount of time it takes to render a page is premature optimisation in almost all cases. The time it takes to render most pages is nothing at all compared with the time it takes for the content to be loaded from the network. Have you profiled your sites and found the rendering to be a significant part of the time it takes to load a page?

[–]hupcapstudios 9 points10 points ago

We're all humans. We all have two arms and two legs. So, if an alien were to come down from space and say "HEY HUMAN, LET ME PUT THIS RECTAL PROBE IN YOUR BUTT" People would look around wondering who has to put the rectal probe in their butt. But, if the alien said "Hey Bob Ferdinky, you get the rectal probe". We'd all know exactly who the alien was talking about.

So, in summary, Rectal probes.

[–]m4xm4n 1 point2 points ago

That's a fantastic and surprisingly fitting analogy. Surprising because it involves anal probes.

[–]angerZen 0 points1 point ago

Surprise! Aliens.

[–]mtx 2 points3 points ago

IMHO, it's about documentation. If I have to edit someone else's html/css, I would find it easier to read if I knew which elements are being used repeatedly and which ones are unique.

[–]DexDark 0 points1 point ago

[–]Bankzzz 1 point2 points ago

Here is one point I can bring to the table: When I am working on someone else's style sheet, if I see a certain element that I have to modify has a "class" I then have to go through the code and check to see if there are other elements that have the same class so that I don't mess up other things in the process.

With that being said, the reasons to use classes are just as few as using ID's when you can make a lot of the same selections and cut down on code just by using more advanced selection techniques. I still use them for CERTAIN things when I have to but only IF I have to.

[–]WoollyMittens 1 point2 points ago

If there's one of 'em, use an id. If there's several of 'em, use a class name.

[–]tree_wex -1 points0 points ago

ids get selected faster than classes, but are obviously less flexible and versatile.

[–]x-skeww 2 points3 points ago

You're confusing this with JavaScript.

[–]LousyBog 1 point2 points ago

IDs are selected faster when processing stylesheets as well.

Unless you open a page with over 9000 selectors on a 2006 netbook running IE6 you won't probably ever notice any difference since CSS processing is ridiculously fast. But still, tree_wax is right.

[–]x-skeww 0 points1 point ago

In an over the top micro benchmark you can get a ~1msec difference. In the real world, you won't even get that. Realistically speaking, there is no difference.

[Well, actually there is a difference. If you don't use IDs, it's way easier to keep your CSS cruft free. The result are smaller files with fewer selectors. This is faster. Additionally, it saves time and money, because things are a tad easier to handle now. There are many people who tried, researched, and experienced these things.]

Drawing one box shadow or one gradient or one rounded box or doing one simple query with JavaScript is several times (easily 100 or even 1000 times) slower. Even decoding and drawing one lousy image is super heavy in comparison.

Gzip your favicon. There. Bigger gain. Fits into one packet now.

Seriously, if it would be worth it, Steve Souders (and friends) would tell us to use IDs. Google and Yahoo would tell us to use IDs. Browser vendors would tell us to use IDs. YSlow, Page Speed, and CSS Lint would tell us to use IDs. They don't.

Only people who heard about the performance benefits on the JS side and who haven't done any kind of research, religiously believe that there must be massive performance gains on the CSS side, too. Spoiler: There aren't any.

[–]LousyBog 0 points1 point ago

I completely agree with you, I was just trying to be an smartass.

Although any JavaScript-heavy website seriously benefits from having IDs instead of classes, I was a class-only developer myself until I started to incorporate JS.

[–]x-skeww 0 points1 point ago

any JavaScript-heavy website seriously benefits from having IDs instead of classes

Yes. But that doesn't mean that you should use those IDs which were added for JavaScript on the CSS side. Separation of concerns is a good thing. You really don't want to introduce any kind of coupling between unrelated parts of the stack.