HTML: HTML Global Attributes Reference

🗓 Last updated: 2026-11-30👤 Written by: Casey✓ Reviewed by: MOTO🏷 Category: HTML Tutorial

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

Tags in this section: <html> <head> <body> <abbr> <iframe> <input> <textarea> <button> <slot> <img>

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:

▶ Example

▶ Try it Yourself

3. 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:

▶ Example

▶ 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.


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:

HTML
<!-- 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:

▶ Example

▶ Try it Yourself

6. 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"

Tags in this section: <html> <head> <body> <abbr> <iframe> <input> <textarea> <button> <slot> <img>

❓ FAQ

Q What are global attributes in HTML, and which ones exist?
A Global attributes work on any HTML element, no tag restrictions. The everyday dozen: 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.
Q What's the difference between id and class? When do I use which?
A 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.
Q How do I use data attributes in HTML and read them with JavaScript?
A Write attributes prefixed with 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.
Q What's the difference between the hidden attribute and display:none?
A Both hide the element, but 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.
Q What is contenteditable in HTML?
A It makes element content editable: with 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.
Q What's the difference between the title and alt attributes?
A 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.
Q Is there an attribute for centering in HTML?
A No — HTML5 has no centering attribute, and the old <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.
Q How does the style attribute work, and should I write inline CSS?
A The 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.
Q What's the difference between id, class, and name?
A 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

📝 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏