Jump to content

Recommended Posts

  • 3 weeks later...
Is that bad practice not to have tag Ids except when needed

 

It's not bad practice at all. On the contrary, adding IDs to everything would clutter up your HTML and make it more difficult to read. I personally only add IDs if I know for a fact I need them, or if there is a strong liklihood that I (or more often my front-end developer) will use them in the future.

 

What about name and class?

Classes are not needed for every element either, and generally should follow the same rules I gave for IDs above. Names are only for form elements, and are required for for elements if you want to be able to access the values upon submission (which, you generally would, or else you wouldn't need a form element).

Edited by haku

I would say that adding ids to each element is an excellent practice. It will save you a lot of work if you decide to implement some Javascript later. I honestly can't think of a situation where you couldn't create unique ids for each element.

I can think of plenty.

 

Normally, you would assign ID's to wrappers and sub wrappers. For example you could give an ID to your "header", "body" and "footer" to easily identify them. You can then give an ID to your "navigation" wrapper (sub wrapper). An example of something you may not bother giving an ID to is a ul tag and possibly li tags inside your navigation - this is obviously dependent on how your navigation works, though.

 

That's the rule of thumb I work on.

Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type. For example, there are multiple divs on a page, but the header and footer will be unique so they get IDs. I wouldn't add a unique ID to every div on the page, because for example some are there simply as innertubes, to ensure cross-browser compatibility. Giving each of these IDs would only add bloat to the page, as these elmements would never need to be uniquely identified. I do however give these inner tubes a common class name.

Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type.

 

You can only apply id's to a single element on any given page. For what you said there, if you have multiple div's which have the same styles (i presume so because you said you were using the id multiple times), you should be giving them a class. It works in pretty much the same way.

 

For example, I query products from a database, for every row that is found, it prints a div with the information inside for each individual product. I have styled the div's identical so I decided to give the <div> a class when writing the php.

 

Not sure if you're bothered about this but it won't pass validation neither.

 

Regards,

 

L2c

No, he was saying that if he had a bunch of div's, he would give whichever he needed to uniquely identify an ID (their own ID). That way he could work with that one individually from the rest easily. He wasn't saying to give them all the same ID.

You can only apply id's to a single element on any given page. For what you said there, if you have multiple div's which have the same styles (i presume so because you said you were using the id multiple times), you should be giving them a class. It works in pretty much the same way.

Xaotique already replied for me, but to elaborate:

 

Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.