-2

Is there a good practice or advice/requirement that every HTML element has an id or/and class specified (Including elements that wouldn't be accessed from JavaScript, for example)?

I would say that it is not necessary for these attributes to be present, but I see a number of situations when there might be a requirement to do that.

For example, another developer wants to extend the functionality of the site without changing the layout or the content - just extending existing/replacing JavaScript (or supplying the plugin, without even knowing the HTML of the final page).

I can see such a scenario in developing reusable components, for example, where an author of the component can't foresee all the use cases upfront but wants to protect himself from constant bug fixing after the release.

Another benefit would be the readability of the final HTML. I learn from reading the rendered HTML more than from more formal reading. A meaningful id often helps.

It would be nice to have an opinion of an experienced web developer or an reference to some guide lines/best practices.

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
OpalApps
  • 149
  • 13
  • 1
    Questions that are opinionated aren't really a good fit for Stack Overflow as there really are no correct answers. In this case, do whatever floats your goat, if you don't mind all the ID's and classes, add them, if you don't really need them, why add them ? – adeneo Mar 05 '17 at 14:44
  • Thanks for the comment, but allow me to disagree with you. The answer here would be - "There is no widely accepted best-practices" or the link the the coding guidelines of some kind. Please suggest the improvement to the question in order to reduce the confusion. Thanks – OpalApps Mar 05 '17 at 14:47
  • One benefit I see is that one can refer to a section of a html page using a `fragment identifier` eg: `http:\\www.myurl.com\index#myid` [reference](https://en.wikipedia.org/wiki/Fragment_identifier) – Himanshu Mar 05 '17 at 14:52

1 Answers1

0

In my opinion i would put the Id or class for every element in my website just so that the code is a little more organized and you can see what the (for example) div is for right away in the code by reading the class. I also agree with your ideas.

Ahm23
  • 338
  • 3
  • 16