HTML ID

Article Summary

Learn how the HTML id attribute works, its rules, and differences from class with clear examples.

The id attribute is used to uniquely identify one element on a page.


ID Attribute Example

<h1 id="main-title">Welcome</h1>

ID vs Class

FeatureIDClass
UniqueYesNo
ReusableNoYes
Used forSingle elementGroup of elements

ID with Attributes

<div id="header" title="Site header">
  Header content
</div>

Important Notes

  • IDs must be unique
  • Used for anchors and scripting
  • Do not repeat IDs
Was this helpful?