← Back to HTML & CSS, page 9

html-css-0161 · Easy fresher

Which global attributes can go on any element, and which ones do you use most?

Global attributes such as id, class, lang, dir, title, hidden, inert, tabindex, contenteditable, translate and data-* work on any element. Use tabindex 0 or -1 only, prefer native controls, and do not put important information in title.

Full answer

Global attributes work on every HTML element. Knowing them avoids needless scripts and wrong markup.

  • id gives a unique name for links, labels, scripts and ARIA references. class gives a group name for styles. style sets inline styles and is best kept for dynamic values.
  • lang and dir set the language and text direction.
  • title gives advisory text, often shown as a tooltip. It is poor for important information, because keyboard and touch users usually cannot see it.
  • hidden hides an element, and inert makes an element and its children not interactive and hidden from assistive technology.
  • tabindex controls keyboard focus: 0 puts a non-interactive element in the natural order, and -1 makes it focusable by script only. Positive values change the order and should be avoided.
  • contenteditable, spellcheck and autocapitalize affect editing. draggable marks an element as draggable.
  • translate="no" tells translation tools not to translate the text. It suits brand names and code.
  • data-* stores custom data, and aria-* and role add accessibility information.
  • accesskey assigns a shortcut, but it clashes with browser and screen reader shortcuts, so it is rarely a good idea.
  • autofocus focuses an element on load, which can be disorienting, so use it carefully.
  • popover and slot serve newer features.

Many properties of the same name exist in JavaScript, such as el.hidden, el.tabIndex, el.translate and el.draggable.

The brand name is marked so that translation tools leave it alone. The div can be dragged and can receive focus from a script, but not from the Tab key. The inert button cannot be focused or clicked.

Prefer a real button or link to adding tabindex to a div.

html
<p id="p" lang="fr" translate="yes">Bonjour</p>
<span id="brand" translate="no">Schoolabe</span>
<div id="d" draggable="true" tabindex="-1" title="Advisory text" data-id="7"></div>
<div id="h" hidden>Hidden</div>
<div id="i" inert><button id="ib" type="button">Inert button</button></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • id, class and style identify and style an element.
  • lang and dir set language and direction; translate="no" protects brand names.
  • hidden and inert remove an element from view and interaction.
  • tabindex 0 and -1 are the safe values.
  • data-* stores custom data.

Common mistakes

  • Using title for information that users need.
  • Using positive tabindex values.
  • Adding tabindex to a div instead of using a button.

Follow-ups

  • What does tabindex="-1" do?
  • Why is accesskey rarely used?

Tips

  • Group the attributes: identity, language, visibility and focus, editing, custom data.
  • Give translate="no" as an example that people forget.