Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 122 ยท Interactive Elements

Global Attributes

Learn the HTML global attributes that work on every element, from id, class and lang to hidden, inert, contenteditable, tabindex, dir and translate.

Most attributes belong to one element: href to links, src to images, rows to text areas. A special group of attributes works on every HTML element. These are the global attributes. You already know a few of them from Attributes: id, class, title and lang.

The rest of the group is just as useful and much less known. With global attributes you can make any element editable, hide content until a reader searches for it, freeze part of a page, control the on-screen keyboard on phones, and tell translation tools what to leave alone. This lesson walks through them in groups, so you can see which problem each one solves.

Identity and styling

These are the attributes you'll write most often.

Attribute Purpose
id A unique name for one element. Used by fragment links (#id), <label for>, ARIA references and JavaScript. See IDs.
class A space-separated list of group names, mostly used by CSS. See Classes.
style CSS declarations applied to just this element. Handy for quick tests; prefer stylesheets for real pages. See The style Attribute.
title Advisory text, usually shown as a tooltip on hover.

Don't put important information in title alone. The tooltip only appears on mouse hover, so touch-screen and keyboard users usually never see it, and screen readers handle it inconsistently. If something matters, write it on the page.

Language and direction

lang declares the language of an element's content: lang="en", lang="pt-BR", lang="ja". Set it once on <html> and override it wherever the language changes. Screen readers use it to pick the right pronunciation, browsers use it for hyphenation and font choice, and search engines use it to understand your page.

dir sets the text direction:

  • ltr: left to right, as in English.
  • rtl: right to left, as in Arabic, Hebrew or Persian.
  • auto: let the browser decide from the first strong character in the content. This is the right choice for text you don't control, like user names or comments.

translate="no" tells automatic translation tools to leave an element alone. Use it for brand names, code, or a word that's being discussed as a word. translate="yes" (the default) switches translation back on inside such an element.

Language, direction and translation

The <bdi> element isolates text whose direction you don't know in advance. You'll learn more about it in Quotations, Citations and Abbreviations.

Showing, hiding and freezing content

hidden

The hidden attribute hides an element completely. It isn't rendered, it can't be focused, and screen readers skip it. It's the semantic way to say "this content is not relevant right now", for example the second step of a form that the user hasn't reached yet.

HTML
<p id="success" hidden>Thanks! Your message was sent.</p>

Remove the attribute (or set element.hidden = false in JavaScript) to show it again. One catch: hidden works through the browser's default display: none style, so a CSS rule like .box { display: flex; } will override it and make the element visible. Many developers add [hidden] { display: none !important; } to their stylesheets to prevent that.

hidden="until-found"

A newer value, hidden="until-found", hides content but still lets the browser find it. When someone searches the page with Ctrl+F, or follows a link to a fragment inside it, the browser fires a beforematch event on the element, removes the hidden attribute and scrolls to the match. This is ideal for collapsed sections in long documentation.

Hidden until found

Browser support. hidden="until-found" works in Chromium-based browsers (Chrome, Edge, Opera and others), and other engines have been adding it. Where it's not supported, the browser treats it like plain hidden, so the content is simply hidden. That makes it safe to use as an enhancement. The similar search-and-reveal behavior for closed details elements has the same support story.

inert

inert is a boolean attribute that freezes an element and everything inside it. The content stays visible, but it can't be clicked, focused, selected or reached by screen readers. Use it for parts of the page that are temporarily unavailable: content behind a custom overlay, a form section that's locked until a checkbox is ticked, or off-screen slides in a carousel.

Freezing a section with inert

Try pressing Tab before and after ticking the box: the inert fields are skipped. Browsers don't change how inert content looks, which is why the example dims it with CSS. Always give users a visual hint. Modal dialogs use the same mechanism to block the page behind them; see Dialogs and Modals.

popover

popover is also global: any element can become a popover. It has its own lesson: Popovers.

Editing and dragging

contenteditable lets the user edit an element's content directly on the page. Its values are:

  • true (or an empty value): editable, including rich text like bold or pasted formatting in many browsers.
  • false: not editable, even inside an editable parent.
  • plaintext-only: editable, but only as plain text. Pasted formatting is stripped. This value is newer, so check support if you depend on it.

spellcheck="true" or "false" controls whether the browser checks spelling in editable content and form fields. Turn it off for things like codes, usernames or programming text where red underlines are just noise.

draggable="true" makes an element draggable with the mouse. Note that it's not a boolean attribute: you must write the value true or false. Links and images are draggable by default. You'll use it in Drag and Drop.

Editable content

Editable content is not submitted with a form. If you need to save it, read it with JavaScript (textContent for plain text) and send it yourself. Also remember that whatever users type is untrusted; never insert it into another page as HTML without sanitizing it.

Keyboard and focus

tabindex controls whether and how an element takes part in keyboard focus:

  • tabindex="0" adds an element that isn't normally focusable to the tab order, in source order.
  • tabindex="-1" makes an element focusable from JavaScript (element.focus()) but leaves it out of the tab order. This is useful for headings or containers you move focus to.
  • A positive value forces an element ahead of everything else. Avoid it: it creates a confusing order that's hard to maintain.

Prefer real interactive elements over tabindex. A <button> is focusable and keyboard-operable for free; a <div tabindex="0"> is only focusable. More in Keyboard Access and Focus.

autofocus focuses an element as soon as the page (or the dialog containing it) appears. Use it only when it clearly helps, such as a search page whose sole purpose is the search box. Jumping focus can disorient screen-reader users.

accesskey assigns a keyboard shortcut, such as accesskey="s". The key combination you press varies by browser and operating system (often Alt or Alt+Shift plus the key), and shortcuts can clash with the browser's or assistive technology's own. It's rarely worth using on public websites.

On-screen keyboards

Three global attributes shape the virtual keyboard on phones and tablets. They have no effect on a physical keyboard.

Attribute What it controls Example values
inputmode Which keyboard layout to show numeric, decimal, tel, email, url, search, none
enterkeyhint The label or icon on the Enter key enter, done, go, next, previous, search, send
autocapitalize Automatic capitals while typing off, sentences, words, characters
HTML
<label>
  One-time code
  <input type="text" inputmode="numeric" autocomplete="one-time-code">
</label>

<label>
  Message
  <textarea enterkeyhint="send" autocapitalize="sentences"></textarea>
</label>

inputmode is often better than type="number" for things like PIN codes or card numbers, which are digits but not quantities. These attributes appear again in Input Attributes.

Other global attributes

A few more global attributes belong to specific topics, and you'll meet them in their own lessons:

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main difference between hidden and inert?

    Hidden content isn't rendered at all. Inert content is still visible but can't be focused, clicked or reached by screen readers.
  2. 2. What happens to an element with hidden="until-found" when the user searches the page and the browser finds a match inside it?

    That's the point of until-found: the content is hidden but searchable, and the browser reveals it when found.
  3. 3. Which dir value should you use for user names whose language you don't know?

    dir="auto" lets the browser pick the direction from the text itself, so both English and Arabic names display correctly.
  4. 4. Why is tabindex="3" a bad idea?

    Positive values reorder focus away from the visual and source order. Use 0 to add an element to the natural order and -1 for focus from script only.
  5. 5. Which attribute changes the label of the Enter key on a phone's on-screen keyboard?

    enterkeyhint suggests a label such as Send, Go or Next. inputmode picks the keyboard layout instead.

Summary

  • Global attributes work on every HTML element.
  • id, class, style, title and lang cover identity, styling, hints and language; dir and translate handle direction and machine translation.
  • hidden hides content; hidden="until-found" hides it but lets find-in-page reveal it; inert keeps content visible but unusable.
  • contenteditable, spellcheck and draggable enable editing and dragging.
  • tabindex, autofocus and accesskey affect keyboard focus; use positive tabindex and accesskey sparingly, if at all.
  • inputmode, enterkeyhint and autocapitalize tune on-screen keyboards.

Next lesson: Custom Data Attributes.