Hacker News new | ask | show | jobs
by bluetwo 3021 days ago
Sure, the author is describing good practice, not best practice.

If you have a choice, use class, then if you need to modify a specific use of the class, use id.

I would still use .footer instead of #footer. Of course you should consider <footer> as well.

3 comments

You can have many classes!!

  <div class="footer special enterprise">


  .footer.special.enterprise {
    background-color: blue;
  }
That, too, yes.
I would use ".xed-footer" for reusable footer bearing the x caracteristic. And #main-footer for the stuff that stick at the bottom of most of my pages with unique caracteristics.

Way more self describing. No team member will ask which one is reusable and which one is not.

> I would still use .footer instead of #footer

Why?

I think the preference against ids is covered over point #2 about specificity. id styles will always override class styles, so when you're trying to apply a class-based style guide to an element that's already been styled with an id, you end up having to use !important which just creates a similar specificity problem for someone else down the line.

Of course, you can still do this if you want to. To quote Chris Rock, "you can drive a car with your feet if you want to." But if you work on a large code base that lots of developers have touched, and lots of future developers will touch, you run into these issues all the time. And the bugs that come about can be tough to spot and to fix.

If it's been styled with an id then it's already specially called out in the CSS.

Either remove that, or add the id to the selector with the style.

I agree with other poster.

For me it's a matter of doing things the same way so when I need to make an update to the code in a year it's intuitive.

"Be kind to your future self" is my motto.