English العربية Português Japanese

HTML Global Attributes Reference

Global Attributes are properties that can be used on all HTML tags. They define an element's identity, style, behavior, language, and interaction methods.

What Are Global Attributes

Global attributes are universal attributes defined in the HTML specification that can be applied to any HTML elements (including root elements like <html>, <head>, and <body>). They provide consistent semantic and behavioral control across all tags. Understanding and using global attributes proficiently is key to writing semantic, accessible, and maintainable HTML.

Most Commonly Used Global Attributes

The following four global attributes are the most frequently used in daily development:

HTML
<!-- id — unique identifier -->
<div id="header">Website Header</div>

<!-- class — class names -->
<p class="highlight important">This text has both highlight and important styles applied</p>

<!-- style — inline styles -->
<span style="color: red; font-weight: bold;">Bold red text</span>

<!-- title — hover tooltip -->
<abbr title="HyperText Markup Language">HTML</abbr> is a web markup language.
▶ Try It Yourself

Language and Direction

These two attributes are critical for declaring the language and reading direction of page content, and are very important for SEO and accessibility:

HTML
<!-- Declare the page's primary language as English -->
<html lang="en">

<!-- Body text in English -->
<p lang="en">Welcome to our website!</p>

<!-- Right-to-left Arabic text -->
<p dir="rtl" lang="ar">مرحبا بالعالم</p>
▶ Try It Yourself
ℹ️ Note: The lang attribute is crucial for SEO (search engines can accurately identify content language) and screen readers (which select the correct speech synthesis engine). Make it a habit to set lang on the <html> tag.

Data Attributes (data-*)

The data-* attribute allows you to store custom data on HTML elements, which JavaScript can read via the dataset API. Note that * must start with a lowercase letter and cannot contain uppercase letters:

HTML
<!-- Use data-* to store product information -->
<div class="product" data-id="1001" data-name="Wireless Mouse" data-price="99.00" data-stock="true">
  <h3>Wireless Mouse</h3>
  <p>Price: $99.00</p>
  <button onclick="addToCart(this)">Add to Cart</button>
</div>

<script>
  function addToCart(btn) {
    const product = btn.closest('.product');
    const id = product.dataset.id;
    const name = product.dataset.name;
    const price = product.dataset.price;
    console.log('Added to cart:', id, name, price);
    alert(`Added "${name}" to cart, price: $${price}`);
  }
</script>

Interactive Attributes

These attributes control element visibility, focus order, and editing behavior:

HTML
<!-- hidden — hides invisible info -->
<div hidden>This message is not visible to users</div>

<!-- tabindex controls focus order -->
<input type="text" tabindex="2" placeholder="Focused second">
<input type="text" tabindex="1" placeholder="Focused first">

<!-- contenteditable enables in-page editing -->
<div contenteditable="true" style="border:1px solid #ccc; padding:10px; border-radius:4px;">
  This text can be edited directly in the browser!
</div>

<!-- spellcheck — spell checking -->
<textarea spellcheck="true" placeholder="English text will be spell-checked"></textarea>

Other Global Attributes

Here are more useful global attributes:

Attribute Description Example
accesskey Keyboard shortcut to activate or focus an element accesskey="s" (press Alt+S to focus)
draggable Whether an element is draggable, works with the Drag & Drop API draggable="true"
autofocus Auto-focus on page load (for <input>, <textarea>, <button>, and other form elements) autofocus
translate Controls whether element content can be translated (yes / no) translate="no"
slot Used with Web Component <slot> elements slot="header"
is Specifies a customized built-in element is="x-foo"
itemscope / itemprop / itemtype Microdata for structured data markup itemscope itemtype="https://schema.org/Product"

📖 Summary

📝 Exercises

  1. Product Showcase: Create product cards using data-* to store each product's id, name, price, and stock status. Display product information in a popup when a button is clicked. Include at least 3 product cards.
  2. Todo Editor: Use contenteditable="true" to create a simple task editing area, and use the hidden attribute to toggle "show/hide editor panel" functionality. Set a logical Tab focus order using tabindex.
100%