Home > Blockchain >  Should HTML IDs be unique throughout the website?
Should HTML IDs be unique throughout the website?

Time:06-09

Suppose I am creating a website having 5 pages. If a specific ID is used in one page, can I use the same ID in another page?

CodePudding user response:

The main thing is that it should be a single ID per page, and the number of pages is not important.

CodePudding user response:

Yes, you can, and you should, if you’re using the ID for the “same” element.

Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).

https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id

You can link to an element with the fragment identifier like https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id#browser_compatibility

If this element is repeated on each site, it should have the same fragment identifier.

If you want to style the element by means of its ID, it absolutely needs the same ID across all pages as well.

On a side note, styling by means of IDs is discouraged because it’s hard to maintain in bigger systems.

CodePudding user response:

Yes, you can use the same id on different pages, since they aren't related to each other.

CodePudding user response:

Applying the same ID to multiple elements is generally considered bad practice since it defies the W3C spec. If you want to style elements across multiple pages, consider using a class.

  •  Tags:  
  • html
  • Related