HTML Global Attributes

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

Global attributes are common to all HTML elements. You can put them on any tag — even if a particular attribute has no useful effect on that element. Classic examples: id, class, lang, title, style, and hidden.

Beyond the core list, documents often also use ARIA (role, aria-*) and, less ideally, HTML event handler attributes (onclick, …). Prefer semantic elements and addEventListener when you can.

Baseline · Core of HTML.

Global attributes are a standard part of HTML. Individual attributes may be newer or experimental — check each attribute’s own page.

Deprecated XHTML leftovers.

xml:lang and xml:base are kept for compatibility. Prefer HTML lang for language.

HTML Syntax

syntax.html
<element  global_name="value">

Boolean globals (like hidden) can appear without a value: <p hidden>.

Common Global Attributes

Start with these everyday globals (not a full MDN dump — each links to its own tutorial when available):

AttributeDescriptionLearn
idUnique document identifier for links, scripts, and CSSOpen
classSpace-separated CSS/JS hooksOpen
lang / dirLanguage and text directionOpen
titleAdvisory text (often a tooltip)Open
data-*Custom data for scripts (dataset)Open
hiddenHides irrelevant content from renderingOpen
tabindexFocusability and keyboard orderOpen
styleInline CSS — prefer stylesheets for real sitesOpen

Live Preview

Apply a common global attribute to a sample paragraph and see the effect.

Hover the paragraph for title. hidden removes it from the layout.

Sample element

Global attributes work on every element — try the presets above.

title="…" — hover for a tooltip

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — id and class

Identity vs reusable styling hooks — both are global.

HTML
<div id="intro" class="card highlight">
  <p>Welcome to global attributes.</p>
</div>
Try It Yourself

How It Works

  1. id must be unique in the document (#intro in CSS/JS).
  2. class can list several names for shared styles.
  3. Same attributes work on div, p, span, and other tags.
  4. See also id and class.

Example 2 — Custom data-* Attributes

Store script-only data on any element.

HTML
<button type="button" id="buy" data-product-id="42" data-price="9.99">
  Add to cart
</button>

<script>
  const btn = document.getElementById("buy");
  console.log(btn.dataset.productId); // "42"
  console.log(btn.dataset.price);     // "9.99"
</script>
Try It Yourself

How It Works

  1. data-product-id becomes dataset.productId (camelCase).
  2. Any element can carry custom data attributes.
  3. Not for visible copy — put user-facing text in the content.
  4. Details: data-* tutorial.

Example 3 — lang and title

Language and advisory text — both global.

HTML
<p lang="fr" title="French greeting">Bonjour !</p>
<p lang="en" title="English greeting">Hello!</p>
Try It Yourself

How It Works

  1. lang helps browsers, translators, and screen readers.
  2. title adds advisory info (often shown on hover).
  3. Use BCP 47 tags like en, fr, en-US.
  4. See also lang, dir, title.

Edge Cases & Pitfalls

Globals are powerful — misuse creates invalid or inaccessible pages.

id

Ids must be unique

Duplicate id values break labels, fragment links, and getElementById.

events

Avoid inline event attributes

MDN discourages onclick/onload in HTML. Use JavaScript listeners instead.

hidden

Do not hide relevant content

hidden is for irrelevant UI, not for stuffing SEO text or permanent content.

style

Inline style is for quick demos

Prefer classes and stylesheets for maintainable sites.

legacy

xml:lang is legacy

Deprecated XHTML attribute — use HTML lang.

Browser Support

Global attributes are part of HTML in the browser. Support for each attribute varies — core ones like id and class are universal.

✓ Core HTML · Widely available

Global attributes

id, class, lang, title, style, and data-* work everywhere modern. Newer globals (popover, inert, …) need feature checks — see each attribute page.

100% Core globals (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer Core globals yes
Supported
Core globals Universally supported

Bottom line: Safe to rely on classic globals everywhere. Check caniuse or MDN before depending on the newest ones.

Key Takeaways

  • Definition: attributes allowed on every HTML element.
  • Everyday set: id, class, lang, title, data-*, hidden.
  • vs specific: href/src are not global — only certain tags.
  • Prefer: stylesheets over inline style; JS listeners over onclick.

One line: global attributes give every element shared hooks for identity, language, styling, data, and accessibility.

Frequently Asked Questions

Attributes that can be specified on any HTML element — for example id, class, lang, title, style, hidden, data-*, tabindex, and many others. Some have no visible effect on every element, but they are still allowed.
Global attributes work on any tag. Element-specific attributes only apply to certain elements — for example href on a, src on img, or type on input.
Custom data attributes store private page data on elements. Names like data-user-id map to element.dataset.userId in JavaScript.
Yes. ARIA attributes and role can be used on elements to improve accessibility when native semantics are not enough. Prefer semantic HTML first.
style is valid but prefer stylesheets for maintainability. HTML event handler attributes (onclick, onload, …) are discouraged — use addEventListener in JavaScript instead.
xml:lang and xml:base come from XHTML and are deprecated but kept for compatibility. Prefer the HTML lang attribute.

Next: headers

Associate table cells with header cells for accessibility.

headers tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful