HTML Global Attributes

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
Any HTML element

Introduction

HTML elements share a common set of global attributes—properties you can add to almost any tag, from <div> and <p> to <table> and <svg>. They handle identity (id, class), language (lang, dir), custom data (data-*), visibility (hidden), accessibility (aria-*), and more.

Understanding global attributes is essential because they are the glue between your HTML structure, CSS styling, JavaScript behavior, and assistive technologies. This hub page explains what each common global attribute does, how it differs from element-specific attributes like href and src, and how to use them correctly in real pages.

What You’ll Learn

01

id & class

Hooks for CSS/JS.

02

lang & dir

Language & direction.

03

data-*

Custom metadata.

04

title & hidden

Tooltips & visibility.

05

aria-*

Accessibility.

06

on* handlers

Event attributes.

What Are Global Attributes?

Global attributes are defined in the HTML specification as attributes that are allowed on all elements, unless a specific element’s definition says otherwise. They do not belong to one tag—you can put class="note" on a paragraph, a list item, or an image alike.

The core set includes: accesskey, autocapitalize, class, contenteditable, data-*, dir, draggable, hidden, id, lang, spellcheck, style, tabindex, title, and translate. In addition, all aria-* attributes and on* event handler attributes (such as onclick) are global.

💡
Beginner Tip

If you see an attribute on one element and wonder whether another tag can use it too, check whether it is global. Attributes like href, src, and type are not global—they only work on specific elements. See the full attribute index for every property.

Global vs Element-Specific Attributes

HTML attributes fall into two broad categories. Knowing the difference helps you write valid markup and choose the right tool for each job.

CategoryWorks onExamples
GlobalNearly every HTML elementid, class, lang, data-user-id, aria-label, onclick
Element-specificOnly defined elementshref on <a>, src on <img>, colspan on <td>

Global attributes often cooperate with element-specific ones. For example, an <input> with type="email" uses the global id for a <label> for="..." association and the specific type attribute to control validation.

  • Reuseclass and data-* work the same way on every tag.
  • Validation — putting href on a <div> has no effect and is invalid HTML.
  • Layering — combine globals for structure/accessibility with specific attrs for element behavior.

📝 Global Attributes Reference

Quick lookup for the most commonly used global attributes in modern HTML.

Identity & styling

Language & text

Behavior & state

Accessibility (aria-*)

All WAI-ARIA state and property attributes are global. Common examples:

AttributePurpose
roleDefines the element’s semantic role when native HTML is insufficient.
aria-labelAccessible name when visible text is missing.
aria-hiddenHides decorative content from assistive technologies.
aria-expandedWhether a collapsible region is open or closed.
aria-liveHow assistive tech announces dynamic updates (polite, assertive).

See individual attribute guides for deeper coverage: id, class, lang, dir, hidden, data-*, and more in the attribute sidebar.

Event Handler Attributes (on*)

Attributes like onclick, onchange, oninput, and onkeydown are global—you can attach them to any element. The browser runs the attribute value as JavaScript when the event fires.

html
<button type="button" onclick="alert('Clicked!')">Click me</button>

Inline event handlers work for quick demos, but in production code prefer addEventListener in a separate script. That keeps behavior out of markup, avoids Content Security Policy issues, and is easier to test.

  • Global scope — inline handlers run in a special scope with this bound to the element.
  • One handler per event — assigning onclick in HTML replaces any previous inline handler for that event.
  • Prefer JS files — use element.addEventListener('click', fn) for maintainable apps.

Deprecated, Removed & Obsolete Globals

Not every attribute that once appeared on many elements is still valid. HTML evolves—some globals were removed entirely; others remain parseable but should not be used for new pages.

Removed from the specification

  • contextmenu — removed. Use JavaScript and the oncontextmenu event instead of an HTML attribute.
  • dropzone — removed. Handle drag-and-drop with the Drag and Drop API in script.

Obsolete presentation attributes

These were once global (or widely used) for visual styling. In HTML5 they are obsolete—use CSS instead:

AttributeModern replacement
alignCSS text-align or flexbox/grid alignment
bgcolorCSS background-color
borderCSS border (on tables and elements)
colorCSS color
width / heightCSS sizing (still valid on some elements like <img>, but not as global presentation)
Still valid (not deprecated)

autocapitalize and translate are current global attributes supported in modern browsers. Do not confuse them with removed attributes like contextmenu or obsolete presentation attrs like align.

Common Pitfalls

  • Duplicate id values — ids must be unique per document. Duplicates break accessibility, labels, and querySelector expectations.
  • Overusing inline style — hard to maintain and overrides cascade unpredictably. Use classes in a stylesheet.
  • Using title as the only accessible name — tooltips are not reliably exposed to all assistive tech. Use visible text or aria-label when the name matters.
  • Invalid data-* names — after data-, names must be lowercase, no spaces; use hyphens (data-user-id), which become dataset.userId.
  • hidden vs display:nonehidden is semantic; CSS can still override it. Prefer hidden for content that is not yet relevant.
  • Positive tabindex — values greater than 0 disrupt natural tab order. Use 0 or -1 in most cases.
  • Obsolete globals for layoutalign, bgcolor, and border on random elements are outdated; use CSS.

⚡ Quick Reference

Identity
id class

Select & style

i18n
lang dir

Language

Custom
data-*

JS metadata

A11y
aria-*

Accessibility

Examples Gallery

Six examples showing global attributes on everyday elements. Each includes View Output and Try It Yourself links to the live editor.

Example 1 — id and class on <div>, <p>, <span>

Use id for a unique hook and class for reusable styling across different elements.

html
<style>
  .highlight { background: #fef9c3; padding: 0.2rem 0.4rem; }
  #intro { font-size: 1.1rem; }
</style>

<div id="intro" class="card">
  <p>Welcome to <span class="highlight">global attributes</span>.</p>
  <p class="highlight">The class works on any element.</p>
</div>
Try It Yourself

How It Works

id="intro" targets one element with CSS #intro. class="highlight" styles both the <span> and the second <p> because class is reusable on any tag.

Example 2 — lang and dir on Mixed-Language Content

Mark language and text direction so browsers, translators, and screen readers handle each phrase correctly.

html
<p lang="en">Hello, welcome!</p>
<p lang="fr">Bonjour, bienvenue !</p>
<p lang="ar" dir="rtl">مرحباً بك في موقعنا</p>
Try It Yourself

How It Works

lang declares the language of each paragraph. dir="rtl" on the Arabic line sets right-to-left layout so text flows naturally. See also the dir attribute guide.

Example 3 — data-* Attributes Read with dataset

Store custom values on elements and read them from JavaScript without non-standard attributes. Full guide: HTML data-* attributes.

html
<article
  class="product"
  data-product-id="42"
  data-price="19.99"
  data-in-stock="true">
  Wireless Mouse
</article>

<script>
  const el = document.querySelector('.product');
  console.log(el.dataset.productId); // "42"
  console.log(el.dataset.price);     // "19.99"
</script>
Try It Yourself

How It Works

Hyphenated names after data- map to camelCase on element.dataset. data-product-id becomes dataset.productId.

Example 4 — title Tooltip and hidden Attribute

Show advisory text on hover and hide content that is not currently relevant.

html
<p title="This tooltip appears on hover">
  Hover over this paragraph.
</p>

<p hidden>
  This paragraph is hidden until you remove the hidden attribute.
</p>

<p>Only the first and third paragraphs are visible by default.</p>
Try It Yourself

How It Works

title provides supplemental information—typically a native tooltip. hidden is a boolean global attribute that removes the middle paragraph from the rendered output until you delete the attribute or toggle it with JavaScript.

Example 5 — contenteditable Paragraph with spellcheck

Let users edit text directly in the page while the browser checks spelling.

html
<p contenteditable="true" spellcheck="true">
  Click here and edit this sentence. Misspelled wrds get underlined.
</p>
Try It Yourself

How It Works

contenteditable="true" turns the element into an editable region. spellcheck="true" asks the browser to underline unknown words—both contenteditable and spellcheck are global and work on any element that can contain text.

Example 6 — Complete Page Combining Multiple Globals

A full HTML document using lang, id, class, data-*, title, hidden, aria-label, and tabindex on semantic tags like <header>, <main>, and <button>.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Global Attributes Demo</title>
  <style>
    .note { border-left: 4px solid #3b82f6; padding-left: 1rem; }
    .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; }
  </style>
</head>
<body>
  <header id="top">
    <h1>Global Attributes Demo</h1>
  </header>

  <main class="note" data-section="intro" title="Main content area">
    <p lang="en">This page combines several global attributes.</p>
    <p hidden>Draft paragraph not shown yet.</p>
  </main>

  <button type="button"
    aria-label="Back to top"
    tabindex="0"
    onclick="location.hash='#top'">
    ↑ Top
  </button>
</body>
</html>
Try It Yourself

How It Works

The root lang on <html> sets the document language. id, class, and data-section identify and style the <main> block. hidden suppresses draft text. The <button> uses aria-label for an accessible name and tabindex for keyboard focus.

💡 Best Practices

✅ Do

  • Use class for reusable styling; id for unique anchors
  • Set lang on <html> and override on foreign-language phrases
  • Store script configuration in data-* attributes
  • Prefer semantic HTML (HTML tags) before adding aria-*
  • Use hidden for content that is not yet relevant
  • Attach events with addEventListener in production code (see onclick for inline handler reference)

❌ Don’t

  • Duplicate id values on the same page
  • Rely on title as the only accessible name
  • Use obsolete globals like align or bgcolor for layout
  • Assume removed attributes (contextmenu, dropzone) still work
  • Stuff large JSON blobs into data-* attributes
  • Use positive tabindex values without a strong reason

Universal Browser Support

Core global attributes—id, class, lang, title, hidden, data-*, and aria-*—are supported in every modern browser. Newer attributes like autocapitalize and translate are widely supported on mobile and desktop; always test contenteditable and spellcheck in your target browsers.

Baseline · Since HTML

HTML global attributes

Core global attributes—id, class, lang, title, hidden, data-*, and aria-*—are supported in every modern browser. Newer attributes like autocapitalize and translate are widely supported on mobile and desktop.

100% Core attribute support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Global attributes Universal

Bottom line: Global attributes are among the most stable parts of the web platform. Prefer them over obsolete presentation attributes and removed features like contextmenu.

Conclusion

Global attributes are the shared vocabulary of HTML. They let you identify elements (id, class), describe language (lang, dir), attach custom data (data-*), control visibility (hidden), improve accessibility (aria-*), and wire up behavior (on* handlers like onclick).

Use this page as your hub, then dive into individual guides for id, class, lang, dir, hidden, tabindex, contenteditable, and other globals linked in the sidebar.

Key Takeaways

Knowledge Unlocked

Six things to remember about global attributes

Use these points when adding attributes to any HTML element.

6
Core concepts
🏷️ 02

id & class

CSS/JS hooks.

Identity
📝 03

data-*

Custom metadata.

Scripting
04

aria-*

Accessibility.

A11y
🚫 05

Obsolete

Use CSS instead.

History
06

on* events

Prefer addEventListener.

JS

❓ Frequently Asked Questions

Global attributes are attributes that can be used on any HTML element, regardless of tag name. Examples include id, class, lang, dir, title, hidden, data-*, tabindex, and aria-* attributes. They provide identity, styling hooks, language, accessibility, and behavior that apply across the entire vocabulary of HTML elements.
Yes, with rare exceptions. Nearly all HTML elements accept the standard global attributes defined in the HTML specification. Some attributes like contenteditable or draggable have no visible effect on elements that are not meant to be edited or dragged, but the attributes are still valid syntactically.
Global attributes work on any element—id on a div works the same as id on a span. Element-specific attributes only apply to certain tags—for example, href belongs to anchor elements, src to img and script, and colspan to table cells. If an attribute is not global, using it on the wrong element is invalid.
Custom data-* attributes store extra information on elements without affecting layout or semantics. The name after data- becomes a camelCase property on element.dataset in JavaScript. Use them for configuration, state, or hooks that your scripts read—never for content that should be visible to all users.
Yes. Any element can have WAI-ARIA attributes such as aria-label, aria-hidden, aria-expanded, and role. Use them to improve accessibility when native HTML semantics are insufficient—but prefer semantic elements first.
Yes, style is a valid global attribute, but prefer external or embedded CSS for maintainability. Inline style is appropriate for one-off demos, email HTML, or dynamically generated values that cannot be expressed in a class.
Did you know?

The data-* attribute family was introduced in HTML5 so developers could store custom metadata without inventing invalid attribute names. Before data-*, sites often abused class or non-standard attributes, which broke validation and caused naming collisions.

Practice global attributes in the editor

Open Try It Yourself and experiment with id, class, data-*, and more on any element.

Open Try It editor →

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.

6 people found this page helpful