HTML Global Attributes
Learn HTML global attributes — id, class, data-*, tabindex, contenteditable, lang, dir and more — with runnable examples.
Global attributes are attributes you can add to any HTML element. Unlike element-specific attributes — such as href (only on <a>), src (only on <img>), or colspan (only on table cells) — global attributes are part of the shared vocabulary that every tag understands.
This is what makes them so important: instead of learning a different attribute for every element, you learn one set of global attributes and reuse it everywhere. You'll reach for class and id to hook elements up to CSS and JavaScript, data-* to attach your own custom data, tabindex to control keyboard focus, lang and dir to make content accessible in any language, and hidden, title, or contenteditable for everyday UI behavior.
A global attribute being valid on an element doesn't mean it always has a visible effect. For instance, spellcheck won't change the behavior or semantics of a paragraph, and lang has no effect on an element with no text content. That's fine — they're allowed everywhere; they simply do nothing where there's nothing to act on.
You can find all global attributes and their explanations in the reference table below, followed by closer looks at the ones you'll use most.
| Attributes | Description | Value | Syntax |
|---|---|---|---|
| accesskey | Generates a keyboard shortcut for the element. The way of accessing the shortcut key varies depending on the browser (e.g., ALT, CTRL, ALT+SHIFT, or CTRL+ALT). | character | <element accesskey="character"> |
| class | Adds one or more class names to an element, used to target it from CSS and JavaScript. Multiple classes are separated by spaces. | classname | <element class="classname"> |
| contenteditable | Specifies whether the content of an element is editable. With the "true" value, the content will be editable; with the "false" value (default), it will not be. | true false | <element contenteditable="true|false"> |
| contextmenu | Removed. Was used to create a custom context menu shown on right-click. Removed from the HTML standard and unsupported in browsers — do not use. | menu_id | <element contextmenu="menu_id"> |
| data-* | Allows embedding custom data attributes on all HTML elements. These attributes are accessible via the JavaScript dataset API. | value | <element data-attribute="value"> |
| dir | Defines the text direction for contents within the element. Useful for inserting content with a different text direction, such as Arabic or Hebrew. | rtl ltr auto | <element dir="ltr|rtl|auto"> |
| draggable | Defines whether an element is draggable. When "true", the browser allows dragging; when "false", dragging is disabled. | true false auto | <element draggable="true|false|auto"> |
| dropzone | Removed. Was meant to specify whether dragged data is copied, moved, or linked on drop. Never widely implemented and removed from the HTML standard — do not use. | copy move link | <element dropzone="copy|move|link"> |
| hidden | When present, it indicates that an element is not yet or no longer relevant. Browsers will hide the element. | - | <element hidden> |
| id | Defines a unique id for the element, used for anchor links, CSS, and JavaScript. Must be unique on the page, at least one character long, and contain no spaces. | id | <element id="id"> |
| lang | Defines the language of the element's content. See all language codes here. | language_code | <element lang="language_code"> |
| spellcheck | Defines whether an element may be checked for spelling errors. When "true" or an empty string (""), browsers commonly underline misspelled words and provide alternatives. When "false", spelling checks are disabled. | true false | <element spellcheck="true|false"> |
| style | Defines inline CSS styles for an element. Unlike the class attribute, this applies styles directly to the element. | style_definitions | <element style="style_definitions"> |
| tabindex | Defines the tabbing order for an element when navigating with the "Tab" key. If the value is negative, the element is excluded from sequential keyboard navigation. | number | <element tabindex="number"> |
| title | Provides extra information about the element. Browsers typically display this as a tooltip. | text | <element title="text"> |
| translate | Defines whether the content of an element must be translated. When "yes" or an empty string (""), browsers will translate the text. When "no", the element is excluded from translation. | yes no | <element translate="yes|no"> |
The id attribute
id gives an element a unique name on the page. It's the anchor that ties HTML to the rest of the platform: CSS can target it with #name, JavaScript can grab it with document.getElementById(), a label can point a form control at it with for, and a link can jump straight to it.
<h2 id="install">Installation</h2>
<!-- Clicking this link scrolls to the heading above -->
<a href="#install">Jump to Installation</a>The value must be unique within the document and contain no spaces. Duplicate ids are invalid and cause getElementById() to return only the first match. Learn more in the id attribute chapter and see anchor links in HTML Links.
The class attribute
Where id is for a single element, class is for groups of elements that share styling or behavior. One element can hold several classes separated by spaces, and the same class can be reused on as many elements as you like.
<button class="btn btn-primary">Save</button>
<button class="btn btn-secondary">Cancel</button>Both buttons share the btn class (common styles) and add one more class for their variant. This is the foundation of how CSS is applied — see CSS Selectors and CSS id and class.
Custom data-* attributes
data-* lets you attach your own private data to an element without inventing non-standard attributes. Any attribute whose name starts with data- is valid and is ignored by the browser's rendering — it exists purely for your scripts.
<button id="cart" data-product-id="42" data-price="19.99">
Add to cart
</button>
<script>
const btn = document.getElementById("cart");
// A "data-foo-bar" attribute is read as element.dataset.fooBar
console.log(btn.dataset.productId); // "42"
console.log(btn.dataset.price); // "19.99"
</script>Note the naming rule: the attribute data-product-id becomes dataset.productId in JavaScript — the data- prefix is dropped and each hyphenated segment becomes camelCase. Values are always strings, so convert them (Number(btn.dataset.price)) when you need a number.
The tabindex attribute
tabindex controls whether an element can receive keyboard focus and in what order the Tab key reaches it. There are three meaningful cases:
tabindex="0"— adds the element to the natural tab order, in the position determined by its place in the document. Use this to make a normally non-focusable element (like a<div>you've turned into a custom widget) keyboard-reachable.tabindex="-1"— removes the element from the tab order, but still lets you focus it with JavaScript viaelement.focus(). Useful for moving focus to a region (e.g. a dialog or an error summary) without putting it in the Tab sequence.- A positive value (
tabindex="1"and up) — forces the element to the front of the tab order, ahead of everything withtabindex="0"or notabindex. This is an anti-pattern: it overrides the natural reading order and almost always confuses keyboard users. Avoid it.
<!-- Reachable by keyboard, in normal order -->
<div tabindex="0" role="button">Custom button</div>
<!-- Focusable by script only, skipped by Tab -->
<div id="dialog" tabindex="-1">Dialog content</div>The accessible rule of thumb: let the DOM order drive the tab order. Use 0 and -1 deliberately, and reserve positive values for never.
The contenteditable attribute
contenteditable="true" makes an element's content directly editable by the user, turning a plain <div> into a basic rich-text surface. contenteditable="false" (the default) keeps it read-only.
<div contenteditable="true">
Click here and start typing — this text is editable.
</div>It's the building block behind in-page editors and inline note fields. Because the user can change the DOM, read the current text in JavaScript with the element's textContent or innerHTML when you need to save it.
The lang and dir attributes (internationalization)
lang declares the language of an element's content using a language code such as en, fr, or ar. Setting lang on the <html> element is essential for accessibility: screen readers use it to pick the correct pronunciation and voice, and search engines and translation tools use it to identify the language.
dir sets the base text direction — ltr (left-to-right, the default), rtl (right-to-left, for languages like Arabic and Hebrew), or auto (let the browser infer it from the content).
<html lang="en">
<body>
<p>This paragraph reads left to right.</p>
<!-- An Arabic phrase that reads right to left -->
<p lang="ar" dir="rtl">مرحبا بكم في عالم البرمجة</p>
</body>
</html>For right-to-left layout details, see the CSS direction property. Always set lang on <html>, and add it again on any inline content that switches language — getting these right is one of the simplest, highest-impact accessibility wins on a page.
Accessibility notes
A few global attributes have a direct effect on assistive technology, so use them with care:
lang— required for screen readers to read content in the right language and accent. Set it on<html>and on any element whose language differs from the page.dir— ensures right-to-left languages display and are navigated correctly. Pair it withlangfor RTL content.tabindex— positive values break the expected focus order for keyboard and screen-reader users; stick to0and-1.accesskey— its keyboard shortcuts frequently clash with the shortcuts of browsers and assistive technology, so the chosen key may not work for everyone. Treat it as a convenience, never as the only way to reach a feature.
Summary
A handful of global attributes are worth knowing deeply because you'll use them constantly: id, class, data-*, tabindex, and the i18n pair lang / dir. The rest — title, hidden, contenteditable, draggable, spellcheck, translate, style, accesskey — are useful to recognize and look up in the table above when a situation calls for them. And avoid the removed attributes (contextmenu, dropzone) entirely; they are no longer part of HTML.