Should we always specify an ID attribute in HTML?

No need to add IDs if you're not going to use them. It will just add extra bytes to your website to load. I don't see why would you think you need them, the quoted text you added to your question doesn't say you should use them, it just says they CAN be used on any element.


Only put required elements or attributes in HTML.

You do not need to put ID attribute if it is not required, and you can always add ID attribute whenever required.

Keeping only required elements in html will make it easy to read, clean, low on size and hence improves performance and speed.


IDs are useful if you want to perform end-to-end testing with a framework like Protractor since it is very easy to select the particular element you want. This is not necessary, however, because of CSS attribute selectors. You can select on parts of the CSS class in the DOM, even without an ID being present.

Does the ID make it easier? Sure, but is it required for displaying a page? No.

Tags:

Html