A simple question: why should we add the id
into our HTML tags if they work perfectly well without them? I know that one of their uses is being able to navigate
They're most often used to uniquely identify elements for styling (CSS) and scripting (JavaScript et al) purposes.
But if you're asking about HTML and only HTML, then one example where declarative IDs are useful is associating a with its
,
or
control via its
for
attribute:
Without assigning this attribute, activating the label does nothing, but when you pair both elements together using for
and id
, activating the label causes its control to gain focus.
The other way to associate a form label with its control is to contain it within the label:
But this doesn't always suit the structure of a form or a page, so an ID reference is offered as an alternative.
Other circumstances where an id
attribute serves a function are covered extensively in Alohci's answer.