Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we always specify an ID attribute in HTML? [closed]

Tags:

html

From https://www.w3schools.com/tags/att_global_id.asp:

In HTML5, the id attribute can be used on any HTML element (it will validate on any HTML element. However, it is not necessarily useful).

I'm making a browser-based application for internal use, which in one of the pages there is no CSS or JavaScript referring to any element of the page - just tables.

Should I still specify the id attribute for the elements though there is no (not yet) need to use it? Or it is just best practice to put id'entifiers for future use?

like image 805
Vylix Avatar asked Feb 22 '17 07:02

Vylix


People also ask

When should you use IDs in HTML?

Definition and Usage The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.

Can HTML id be uppercase?

The ID and NAME elements must start with a letter i.e. upper case A to Z or lower case a to z; a number is not allowed. After the first letter any number of letters (a to z, A to Z), digits (0 to 9), hyphens (-), underscores (_), colons (:) and periods (.) are allowed.

Why do we use ID attribute in HTML?

The id global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

Is it compulsory to specify attributes for a tag?

Attributes are always specified in the start tag (or opening tag) and usually consists of name/value pairs like name="value" . Attribute values should always be enclosed in quotation marks.


Video Answer


2 Answers

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.

like image 175
codemirror Avatar answered Sep 28 '22 21:09

codemirror


No, it is not necessary. It is required only when you have to access any form control uniquely on a web page.

like image 29
Gaurav kumar Avatar answered Sep 28 '22 19:09

Gaurav kumar