--- comments: true date: 2013-05-28 23:05:07 layout: post slug: hashed-classes-in-css title: Hashed classes in CSS categories: - Web development tag: meta: Spoofing the uniqueness of IDs by putting hashes in your classes --- I gave a talk at [Beyond Tellerrand](http://2013.beyondtellerrand.com/) yesterday in which I shared some advice concerning the use of IDs in CSS, something which [I vehemently recommend you avoid](/2011/09/when-using-ids-can-be-a-pain-in-the-class/). [Vitaly](http://twitter.com/smashingmag), who was in the audience, [tweeted a snippet of the advice](https://twitter.com/smashingmag/status/339029930551676930) which was, unfortunately, a little out of context. I got a _lot_ of tweets after that asking that I explain further, so here it is… --- For a long time now I have advised people _not_ to use IDs in CSS. Use them in your JS, sure, and as fragment identifiers in HTML, but do not use them to style things in CSS. The reasons for this are, firstly, lack of reusability and secondly—and more importantly—_they are a specificity heavyweight_. Instead of covering the specificity problems again, you can check out [the article I previously wrote on the subject](/2011/09/when-using-ids-can-be-a-pain-in-the-class/) back in 2011. Whenever I give this advice I typically get mixed reactions; a lot of people see how it makes sense and (already) heed the advice; others seem to loathe it. I have honestly _never_ heard a genuinely compelling argument for using IDs in CSS. Not. One. The closest I _have_ heard to a decent argument is along the lines of: > Using an ID in my CSS allows me to see _from my markup_ what is intended to be > unique. I was addressing this argument [in my talk](https://speakerdeck.com/csswizardry/architecting-scalable-css-1?slide=34), which is where the out-of-context tweet came in. I shall attempt to paraphrase this chunk of my talk below… This argument, to me, feels very weak. I can’t imagine why you’d ever want to mark something as being unique, and even if I could I do not think that introducing such a heavily-specific selector is worth the payoff. _However…_ My advice that I gave on stage was simply that _if_ you do find value in marking something as unique, then do this with a naming convention rather than using an ID. The example I used was this:
<ul class="nav #site-nav">
Which, of course, would replace: