1

We are discussing the pros and cons of using id attributes versus class attributes in our website's HTML tags. I use IDs rather liberally. This includes the use of nested IDs such as:

<body id="thisPage"> <div id="mediaSection"> <div id="mediaImages"> .... </div></div></body> 

In this case, MediaSection is a part of the page unique to this HTML file (referenced by the body tag with thisPage id). When I write CSS to style the mediaImages section it would look like this:

#thisPage #mediaSection #mediaImages { vertical-align: bottom; } 

I write the CSS rules like this for legibility purposes even though I know we just need #mediaImages. My co-worker says that this is bad practice: IDs are more expensive and could compromise browser performance because they give the web browser more information to keep track of. His recommendation is to use classes instead. For example, rewriting the above example:

<body class="thisPage"> <div class="mediaSection"> <div class="mediaImages"> .... </div></div></body> 

Is there merit to using classes instead of ids for this situation?

4
  • Take a look into this Commented Sep 4, 2014 at 15:49
  • 1
    "IDs are more expensive" ? Damn... I don't see why. An ID points to one element while a class points to a set. The problem isn't here. ID are more efficient but classes ease reuse and maintanability. Commented Sep 4, 2014 at 15:50
  • Rather than asking, have you tried it? Two pages one all classes, one all ID's and look at the chrome inspector. I doubt you are going to see a massive difference either way. Commented Sep 4, 2014 at 15:54
  • To the people voting to close: is there a problem with the way the question is phrased? Commented Sep 4, 2014 at 16:02

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.