HTML: 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.
1. 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.
2. Most Commonly Used Global Attributes
The following four global attributes are the most frequently used in daily development:
id— A unique identifier for an element, must not be duplicated on the same page. Used for CSS selectors, JavaScriptgetElementById(), and anchor links.class— A space-separated list of class names, used for CSS and JavaScript selectors. An element can have multiple classes.style— Inline CSS styles written directly on the tag. Has higher priority than external stylesheets.title— Tooltip text displayed on hover. Especially useful for<abbr>,<iframe>, and similar elements.
▶ Example
▶ Try it Yourself3. 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:
lang— Declares the language of element content, e.g.,lang="en"(English),lang="zh-CN"(Simplified Chinese)dir— Text direction:ltr(left-to-right, default),rtl(right-to-left, for Arabic/Hebrew)
▶ Example
▶ Try it Yourselflang 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.
4. 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:
<!-- Use data-* to store product information -->
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<!-- Container element -->
<div class="product" data-id="1001" data-name="Wireless Mouse" data-price="99.00" data-stock="true">
<!-- Heading -->
<!-- Heading -->
<!-- Heading -->
<!-- Heading -->
<h3>Wireless Mouse</h3>
<!-- Paragraph -->
<!-- Paragraph -->
<!-- Paragraph -->
<!-- Paragraph -->
<p>Price: $99.00</p>
<!-- Button -->
<!-- Button -->
<!-- Button -->
<!-- Button -->
<button onclick="addToCart(this)">Add to Cart</button>
</div>
<!-- Script tag -->
<!-- Script tag -->
<!-- Script tag -->
<!-- Script tag -->
<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>
5. Interactive Attributes
These attributes control element visibility, focus order, and editing behavior:
hidden— Hides an element (equivalent todisplay: none), the element takes up no spacetabindex— Tab key navigation order:tabindex="0"focuses in document order,-1makes it unfocusablecontenteditable— Makes element content editable; set totrueto enablespellcheck— Enables or disables spell checking:true/false
▶ Example
▶ Try it Yourself6. 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" |
❓ FAQ
class, id, style, title, lang, hidden, data-*, contenteditable, tabindex, draggable. The tables on this page list the complete set; the first six cover most real-world work.id is unique across the page; class can repeat on as many elements as you want. In CSS, id is selected with # and carries higher specificity, class with .. Working rule: styling and bulk selection belong to class; reserve id for unique anchors — precise JavaScript targeting and in-page jumps. In 90% of cases, class is enough.data-: <div data-user-id="123" data-role="admin">, then read them with element.dataset.userId (hyphens convert to camelCase automatically). Good for display-related data: product codes, table row states, component config. Never store passwords or API keys there — anyone can see them in the page source.hidden is an HTML attribute meaning "not supposed to be shown right now," while display:none is CSS styling. Caveat: an explicit display rule on the element defeats hidden — set display: flex and hidden stops working, so either drop the style or control visibility through CSS. Use hidden for temporary hiding and CSS when visibility is part of the styling.contenteditable="true", users click the element and type directly, like a mini rich-text editor — handy for online notes and editable tables. What gets typed lives only in the browser; to persist it, send the content to the server with JavaScript.alt belongs to <img> only — it's the alternative text shown when the image fails to load and read by screen readers. title is global: hovering any element pops up its tooltip. Image descriptions go in alt (counted by both SEO and accessibility); optional hover hints go in title. Search engines don't treat title tooltips as page content.<center> element and align="center" are obsolete. Centering is CSS work: text with text-align: center; block elements with a set width and margin: 0 auto; vertical centering is easiest with display: flex; align-items: center on the parent.style attribute puts CSS right on the tag: <p style="color: red; font-size: 18px">, outranking external stylesheets (except !important). Fine for one-off tweaks and JS-driven style changes, but poor for broad layout — styles scattered through HTML can't be reused and are painful to update. Keep real styling in CSS files and leave style for temporary overrides.id uniquely identifies one element (CSS #, also used for anchor links); class is a repeatable group label (CSS . selects all of them at once); name isn't a global attribute — it lives on form controls, becomes the submitted field name, and groups radio buttons sharing the same name. Mnemonic: style with class, target one element with id, submit form fields with name.📖 Summary
- Global attributes can be used on all HTML elements
idfor unique identification,classfor style grouping,stylefor inline styles,titlefor hover tooltipslangdeclares language,dircontrols text directiondata-*stores custom data, read via thedatasetAPIhiddenhides elements,tabindexsets Tab order,contenteditableenables content editingaccesskeyfor keyboard shortcuts,draggablefor drag-and-drop,autofocusfor auto-focusing
📝 Exercises
- 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. - Todo Editor: Use
contenteditable="true"to create a simple task editing area, and use thehiddenattribute to toggle "show/hide editor panel" functionality. Set a logical Tab focus order usingtabindex.