Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to HTML Notes
Topic #111

HTML Global Attributes

Global attributes can be used on almost any HTML element — they're not tied to a specific tag the way href is tied to <a>.

The Most-Used Global Attributes

AttributePurpose
idA unique identifier for one specific element on the page
classOne or more class names, for CSS/JavaScript targeting — reusable across many elements
styleInline CSS for that one element — see Inline Styles
titleExtra info shown as a browser tooltip on hover
langOverrides the document's language for that element specifically
hiddenHides the element (boolean attribute)
tabindexControls keyboard tab order — see Keyboard Accessibility
data-*Custom data attributes for your own JavaScript — see Data Attributes

Example

<p id="intro-text" class="lead highlight" title="Introduction paragraph">
    Welcome to CodingNow.
</p>

id vs class — When to Use Which

  • id — unique per page, used for one specific element (a target for an in-page link, or a single JavaScript hook)
  • class — reusable, used for styling or grouping many elements the same way

Full comparison in id attribute and class attribute.

Practical Use Case

Global attributes are what connect static HTML to CSS (via class/id) and JavaScript (via id, data-*) — without them, HTML would just be static text with no way for other technologies to hook into specific elements.

Common Mistakes

  • Reusing the same id value on multiple elements on one page — id must be unique; duplicate IDs break JavaScript methods like getElementById and are invalid HTML
  • Using id for styling that should really be a reusable class

Interview Relevance

Q: "Can two elements share the same id?" No — ids must be unique per page; that's the core distinction from class, which is explicitly designed to be reused.

Practice Question

Add appropriate id and class attributes to a navigation <nav> element and its three <a> links.

Related HTML Notes

Want to go beyond the notes?

Join CodingNow's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →