Global attributes are attributes defined by the HTML standard that you can put on any HTML element, whatever its type. Common examples are id, class, style, title, lang, hidden, tabindex, data-* and contenteditable. Knowing the html global attributes means you do not have to memorise which element accepts which of them.
Some global attributes have little effect on certain elements, but they are always valid in the syntax. The list below covers the ones you will use most.
Most attributes belong to specific elements: href works on <a>, src on <img>, type on <input>. Global attributes are the exception. They are part of the base for every element, including elements you add yourself such as custom elements.
| Attribute | Purpose |
|---|---|
id | Unique name of an element in the document |
class | Space-separated labels shared by elements |
style | Inline CSS declarations |
slot | Assigns an element to a named slot of a shadow tree |
part | Exposes an element of a shadow tree to outside CSS |
Use class for styling and avoid style in production pages. Inline styles have high specificity, are repeated for every element and may be blocked by a strict Content Security Policy.
lang sets the language of the element's content, for example lang="fr". Set it on <html> and override it on parts in another language. It affects hyphenation, quote marks, font selection and screen reader pronunciation.dir sets text direction: ltr, rtl or auto. Use dir="rtl" for Arabic or Hebrew.translate with no tells translation tools not to translate a brand name or code.spellcheck turns spell checking on or off for editable text.| Attribute | Effect |
|---|---|
hidden | Removes the element from rendering until revealed |
tabindex | Controls keyboard focus: 0 adds it to the tab order, -1 allows focus by script only |
contenteditable | Lets the user edit the element's text |
draggable | Marks an element as draggable |
inert | Makes an element and its descendants non-interactive and hidden from assistive technology |
autofocus | Focuses the element when the page loads |
accesskey | Suggests a keyboard shortcut, rarely reliable |
popover | Turns the element into a popover controlled by a button |
Avoid positive tabindex values such as tabindex="3": they override the natural tab order and make navigation confusing.
hidden attributehidden hides content from everyone, including screen readers. The hidden="until-found" value keeps the content searchable with browser find and lets it open when a match is found, a feature supported in current Chromium-based browsers. Check browser support before relying on it.
inert and popover attributesinert is useful for the page behind a modal dialog, because no focus, clicks or find-in-page matches reach it. popover is newer: a button with popovertarget toggles the popover, and the browser handles light dismiss and layering. Both are supported in current versions of the major browsers, but verify support for older ones in your audience.
data-* attributesAny attribute that starts with data- stores custom information on an element, without breaking validity.
Attribute names after data- become camelCase in dataset: data-item-id becomes itemId. Values are always strings and are visible to anyone who views the source, so never put secrets in them.
role, aria-* and titlerole and the aria-* family are also global. They adjust how assistive technology describes an element, but they never replace correct native elements: prefer <button> over <div role="button">.
title shows a tooltip on hover with a mouse. It is unavailable on touch and keyboard, so never rely on it for important information.
enterkeyhint and inputmode improve the on-screen keyboard for editable content.nonce is used by Content Security Policy.is creates a customised built-in element.itemscope, itemtype and itemprop are microdata attributes.onclick are also global, but prefer addEventListener in scripts.The global attributes, including id, class, style, lang, title, hidden, tabindex, data-* and the aria-* attributes.
Yes. Any data- attribute with a lowercase name is valid, and you can read it with element.dataset.
style attribute a global attribute?Yes, but external stylesheets are better for maintenance and security.
accesskey a good way to add shortcuts?Usually not. Browsers and operating systems use different key combinations, and shortcuts may collide with assistive technology.
Global attributes work on every HTML element. Learn the core group: id, class, lang, dir, hidden, tabindex, data-*, inert, popover and the ARIA family. Prefer classes over style, avoid positive tabindex, and treat title as an extra hint only.