treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] id's or classes

  • I am quite new to CSS (writing efficient css) and I just came across this article:
    http://css-tricks.com/efficiently-rendering-css/

    So I read, that ID's are more efficient and faster than classes, so I changed all my selectors with classes that only appear once on the HTML page to ID's
    Than I ran my CSS in CSSLint and I get bunch of warnings saying "Don't use IDs in selectors"
    Can someone shed some light on this please.
    Thank you.
  • IDs are faster than classes, but it's so minor that it barely makes a difference. Rather find ways of optimising that actually work, for example:

    #header #nav .first-child span * { background-color: red; }

    could be more efficient. Rather focus on that than choosing between IDs and classes. I use IDs on things that will never change. For example, I'll have a header, footer, sidebar ID. But I'll use classes in most other cases since they can be reused. CSSLint doesn't like IDs which is why it says you shouldn't use IDs. It's just a preference which I disagree with.

    Summary: Yes IDs are faster, but it's too small to worry about. Rather focus on the other thing Chris talks about in that article.
  • @jamy_za
    I do pay attention to writing efficient CSS as best as I can by learning all the time but my(AHA moments are still few and far between.) Such as not having overqualified selectors, redundancy and efficient use of cascade and inheritance.
    I am sort of obsessed with it, just like I was with entity normalization and relational algebra during my 10+ years of DB development.
    I am just quite new to web development and my question was why CSSLint that was developed by CSS guru Nicolle Sullivan would say not using ID's when I read something different. That's all.



  • @jurotek
    super basics here from CSS point of view

    * ID is good for something unique on the page
    = for instance contact form on contact page #contact-form

    * while using classes is good on stuff that repeats (buttons, ...)
    = say image gallery for example
    = you can have page photos but in it you can have more galleries (so instead of doing 10 ids #gal1, #gal2, ...)
    = you can use 1x class ... .gallery

    hope I make sense here
  • I got it krysak.
    I don't want to beat it to death.
    What prompted my post was that I ran my latest CSS on CSSLint I am working on for the final redesign I am going to post sometimes in a next couple of weeks.
    I had 0 errors and 0 warnings.
    After I read the above mentioned article about efficiency of ID's over Classes I changed some of my selectors I was sure they'll never be required or needed more than once on page with ID's instead Classes and after running it thru CSSLint again I got bunch of warning about them.
    Not a big deal.
  • do not rely on tools but knowledge
    I used that tool once too after I realised that what matters is if the site works, loads ok, code is clean and valid ... than it makes sense
  • You got that right.
    Thanx.