Classes Vs. IDs And Descendent Selectors [Audio]

Umm…ahh…you know…well I survived my first audio post well enough to put together a second one, though I did have a acase of the…umms…this time around. In between the…ahhs…I talk about a discussion I caught a couple of weeks ago on the role of ids and descendent selectors in css.

If for some reason you don’t see the audio above click here. Below is a summary of what’s included in the audio.

The discussion played out over several articles and podcasts across a few different sites.

Chris refers to the following post in the podcast with Jeffrey. It’s when he stopped using ids and moved entirely to classes.

It began innocently enough on Smashing Magazine. Chris Coyier does a semi-regular Q&A post there and in response to a question about how to recognize bad css code he mentioned using css selectors that are too specific and as example offered the following awful selector.

1
.article #comments ul > li > a.button

Harry Roberts expanded on the answer over at CSS Wizardry with a number of really good suggestions of what not to do. Toward the end of his post he mentioned selectors and said never to use ids in css. Use them for html fragment identifiers and javascript hooks, but never for css styling hooks. Among his reasons were

  • You can also abstract what’s in an ID to reusable and modular classes
  • Ids are too specific (255 times more than classes) and lead to specificity issues

Jeffrey Zeldman then joined the discussion. Harry’s comment to never use ids in css didn’t sit well with him. Jeffrey doesn’t have anything against using classes, but felt the advice to never use ids was too dogmatic. He said that properly used ids with descendent selectors can be a good thing. They can be more semantic and lead to less code than adding classes everywhere.

As luck would have it, both Harry and Jeffrey were back to back guests on the weekly ShopTalk show hosted by Chris and Dave Rupert and each expanded a little more on their thoughts abut classes, ids, and descendent selectors.

My Thoughts

This topic is something I think about a lot or at least have for the last year or so. If you remember last spring I wrote a few posts about css practices and looked at object oriented css and smacss among other things.

I’d be lying if I said those posts led to me completely changing practices. For the most part I still write css much the way I used to, though the ideas behind oocss and smacss resonated with me. Both seek to further separate content, structure, and presentation. Pushing for more modular code and design makes sense to me.

For years my practice has been to look at part of the design and start writing css to make my html look like the design. Typically that meant adding an id to a higher level structure in the design and working down to more specific elements through descendent selectors. For example if there’s a list I want to style in a sidebar, I’d add id=”sidebar” to the main div and work my way down

1
2
3
#sidebar ul { }
#sidebar li { }
etc.

That leads to a couple of problems, though.

  • Stylistic patterns that are repeated inside high level elements need to be coded more than once. The above styles lists in the sidebar, but not lists in footer.
  • Because of the above I’d typically look at all the lists in a design and style the default list the way the majority would look. That leads to having to overwrite styles on all the other lists.

DRY (Don’t Repeat Yourself) CSS is one way to solve the first problem. Better use of classes another way.

Styles are set on the class and the class can be added to both the lists in the sidebar and those in the footer. The two lists will display the same and the css is in one place and easily reused again. There’s also no overwriting since only lists with the class applied get the styles.

None of this is hard to understand, but what has been more difficult for me is the change in thought process.

It’s still instinct for me to reach for an id, though I am getting better at reaching for classes. What’s more difficult to change is how I go about styling things. I still see an element in the design and begin to write css to make it look how I want. Instead I should be looking for abstract patterns across elements and write css for those in classes.

Something Chris mentioned in one of the podcasts (The one with Jeffrey I think) made things clearer for me. It wasn’t anything profound, but rather a dose of obviousness that I supposed I needed to hear.

find a pattern and give that pattern a class

With that in mind I think the way for me to change my thought process is to go on coding like I have been when initially setting up styles, but as soon as possible begin refactoring my css. As soon as I see a pattern across elements, I should think about moving that pattern to a class.

On a larger scale I’ll save many of these classes in a library that can be reused across projects. I think in time a couple of things will happen.

  • I’ll build up a library of reusable classes that I’ll reach for more often.
  • I’ll naturally begin writing css with abstract patterns and classes instead of adding styles to more concrete design objects.

If ids still find their way in, that’s ok. I have nothing against using them.

How do you approach ids and classes? Do you prefer one over the other? Is it something worth thinking about?

Download a free sample from my book, Design Fundamentals.

2 comments

  1. Consider backing away from the mike a bit – sounds a bit “wet.” Do you really think this will be easier for you? If so, fine; but I would MUCH MUCH MUCH more prefer to read what you have to say (where I can control my pace and ‘skippage’) than be forced into wading through a “podcast” which, almost by definition, is much less focused and easier to bloat with excess verbiage. You did say you wanted feedback.

    • Yep. Any and all feedback is welcome. Never feel like you have to agree with me. Letting me know what I’m doing wrong is often more helpful, since I’m probably not aware of it.

      I’ll see what I can do about the sound. I’m still feeling my way through how to best record my voice. I have a Plantronics headset with the mike attached so it is close. I probably can’t change that, but maybe I can experiment with settings in Garage Band.

      I can understand generally preferring reading to audio. I’m more that way too. I think it’s different for different people though, and I wanted to explore other ways to present information.

      Does having the post written out help? You don’t have to listen to the audio if you don’t want to. When I say the written part is a summary it’s pretty much everything I’m saying in the audio. It’s just not a word for word transcript. I am trying to keep the audio focused too.

      Again I don’t want to stop writing either. I’m still writing one full post a week and would like to bring back the second. It’s just that for now I need to be able to spend more time on some other things and I didn’t want to only publish once a week. I thought offering the podcast would be a nice compromise. A way to still publish twice while freeing up a little more of my time.

Leave a Reply

Your email address will not be published.

css.php