Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Attributes and Events

HTML global attributes

Every HTML global attribute with its allowed values and what it does: id, class, hidden, inert, popover, tabindex, lang, contenteditable, microdata and more.

Global attributes can be used on any HTML element, from <html> to <span>, including elements the browser doesn't recognize. Some have no visible effect on certain elements (spellcheck on an image does nothing), but they are always valid.

HTML
<p id="intro" class="lead" lang="en" title="Opening paragraph">โ€ฆ</p>

Besides the attributes below, every element also accepts the on* event handler attributes and the WAI-ARIA role and aria-* attributes (see the ARIA reference). For element-specific attributes, see All attributes.

Global attributes table

Attribute Values Description
accesskey One or more single characters, space-separated Suggests a keyboard shortcut for focusing or activating the element. The modifier keys differ by browser and OS (often Alt or Ctrl+Alt) and can clash with assistive technology, so use it sparingly.
autocapitalize off / none, on / sentences, words, characters How virtual keyboards and voice input capitalize text typed into the element. Has no effect on physical keyboards. On a <form> it sets the default for its fields.
autocorrect on (default), off Newer. Whether the browser may automatically correct spelling while the user types in an editable element. Support is still growing.
autofocus Boolean Focuses the element when the page loads, or when its <dialog> or popover opens. Use at most one per page or dialog, and only where users clearly expect it.
class Space-separated class names Groups elements for CSS selectors (.card) and scripts (classList, querySelectorAll). Names are case-sensitive.
contenteditable true (or empty), false, plaintext-only Makes the element's content editable by the user. plaintext-only allows editing but ignores rich formatting. Children inherit the setting unless they override it.
data-* Any string Custom data for your own scripts and styles. The name after data- must be lowercase and must not contain a colon. Read it in JavaScript with element.dataset.
dir ltr, rtl, auto The base text direction of the content. auto guesses from the first strong directional character, which suits user-generated text. Put dir="rtl" on <html> for right-to-left pages.
draggable true, false Whether the element can be dragged with the HTML Drag and Drop API. Links and images are draggable by default; most other elements are not.
enterkeyhint enter, done, go, next, previous, search, send The label or icon for the Enter key on virtual keyboards. It doesn't change what Enter does; your code still has to do that.
exportparts Comma-separated part names, optionally inner: outer Used on a shadow host inside another component: forwards its inner parts so CSS outside the outer component can style them with ::part(). Defined in CSS Shadow Parts.
hidden Empty or hidden, until-found Hides the element from all users. until-found keeps the content hidden but lets find-in-page and fragment links reveal it (it fires beforematch). CSS display on the element overrides the hiding.
id A unique string with no spaces Identifies the element: a target for #fragment links, <label for>, ARIA references and getElementById(). Must be unique in the document.
inert Boolean Makes the element and everything inside it non-interactive: it can't be focused or clicked, find-in-page skips it, and it is hidden from assistive technology.
inputmode none, text, decimal, numeric, tel, search, email, url Which virtual keyboard to show for an editable element. It doesn't validate anything; for real number or email fields use the right <input type>.
is The name of a customized built-in element Upgrades a built-in element into a custom element that extends it, such as <button is="fancy-button">. Safari doesn't support customized built-ins.
itemid A URL Microdata: a global identifier for the item, such as an ISBN URN. Only on elements with itemscope and itemtype.
itemprop Space-separated property names Microdata: adds a property to the nearest item. The value comes from the element (text, href, src, content, datetime, โ€ฆ).
itemref Space-separated ids Microdata: pulls properties from elements outside the item's subtree. Only on elements with itemscope.
itemscope Boolean Microdata: creates a new item; the itemprops inside describe it.
itemtype Space-separated vocabulary URLs Microdata: the item's type, such as https://schema.org/Recipe. Only on elements with itemscope.
lang A BCP 47 language tag, or empty for "unknown" The language of the content, such as en, fr-CA or sr-Latn. It affects pronunciation by screen readers, hyphenation, quotes and fonts. Always set it on <html>.
nonce A random, single-use string A cryptographic nonce that lets a specific <script> or <style> run under a Content Security Policy that names the same nonce. Browsers hide its value from CSS selectors.
part Space-separated part names Exposes an element inside a shadow tree so styles outside it can target it with ::part(name). Defined in CSS Shadow Parts.
popover auto (or empty), manual, hint Turns the element into a popover, hidden until shown with popovertarget, commandfor or showPopover(). auto closes on outside click or Esc and closes other auto popovers; manual stays until closed explicitly; hint (newer) is for tooltips and doesn't close auto popovers.
slot A slot name Places this element (a light-DOM child of a shadow host) into the matching <slot name="โ€ฆ"> of the shadow tree.
spellcheck true (or empty), false Whether the browser should check spelling in editable content. Turn it off for codes, usernames and email addresses.
style CSS declarations Inline styles for this one element. Handy for quick tests and dynamic values, but stylesheets are easier to maintain, and a strict CSP can block inline styles.
tabindex An integer 0 adds the element to the normal tab order; -1 makes it focusable by script or link but skips it when tabbing. Positive values force an order and almost always cause confusion; avoid them.
title Text Advisory information, usually shown as a tooltip on hover. Touch and keyboard users rarely see it and screen readers handle it inconsistently, so don't put essential information only here. Line breaks in the value are preserved.
translate yes (or empty), no Whether page translation tools should translate the content. Use no for brand names, code and addresses. Inherited by children.
writingsuggestions true (or empty), false Newer. Whether the browser may offer inline writing suggestions (predictive text) in editable content. Inherited by children.

Two XML attributes, xml:lang and xml:space, are allowed on HTML elements but do nothing in HTML documents. Use lang instead.

Examples

hidden, until-found and inert
data-* attributes, contenteditable and translate
popover, tabindex and dir

Usage notes

  • hidden vs CSS. hidden is a semantic "not relevant right now"; the browser applies display: none with low priority, so a rule like .card { display: flex } will show a hidden card. Add [hidden] { display: none !important; } to your CSS if that bites you.
  • inert vs disabled. disabled applies only to form controls and is announced as "dimmed". inert works on any subtree and removes it from interaction entirely; add a visual cue yourself, because the browser doesn't style inert content.
  • id rules. An id must be unique and contain no whitespace. Starting it with a digit is valid HTML, but you then need to escape it in CSS selectors (#\31 23), so a letter first is more convenient.
  • tabindex on non-interactive content. If you make a <div> focusable, also give it a role, an accessible name and keyboard handling, or use a real <button> or <a> instead.