Lesson 35 of 36
HTML Tutorial

Data and Global Attributes

Attributes that work on every element - id, class, title, lang, hidden, contenteditable - plus custom data-* attributes.

3 min readBeginnerWeb FundamentalsMiscellaneous Tags

Some attributes work on every HTML element. They are called global attributes. This lesson covers the ones you will use constantly, plus custom data-* attributes for storing your own information.

id and class#

html
<section id="specials" class="card highlighted">
  <h2>Today's specials</h2>
</section>
  • id is a unique identifier within the page. Use it for in-page links, labels and scripts. No spaces.
  • class is a space-separated list of labels. The same class can appear on many elements, which makes it the main hook for CSS.

Choose names that describe purpose rather than appearance: price is better than red-text.

title#

The title attribute provides advisory information, usually shown as a tooltip on hover.

html
<a href="menu.html" title="See prices and allergens">Menu</a>

Do not depend on it: touch and keyboard users rarely see it, and it is not a replacement for visible text or labels.

lang and dir#

html
<html lang="en">
  <p>The Nepali word for tea is <span lang="ne">चिया</span>.</p>

lang states the language of an element's content, which helps pronunciation, hyphenation, spellcheck and translation. Set it on html and override it on fragments in other languages. dir="rtl" sets right-to-left direction for languages like Arabic.

hidden#

The boolean hidden attribute hides an element from everyone, including assistive technology:

html
<p id="success" hidden>Thanks, your table is booked!</p>

A script can remove the attribute when needed. If you want something visually hidden but still read by screen readers, use CSS techniques instead, not hidden.

contenteditable and spellcheck#

html
<div contenteditable="true" spellcheck="true">Edit this note directly in the page.</div>

contenteditable makes an element editable by the user. It is the basis of many rich-text editors, but changes are not saved without scripting.

tabindex and accesskey#

tabindex="0" makes a non-interactive element focusable in the normal tab order, and tabindex="-1" makes it focusable only by script. Avoid positive values, which scramble the natural order. Prefer real buttons and links, which are focusable already.

data-* attributes#

Custom attributes starting with data- let you attach your own information to elements, invisibly, for use in CSS and JavaScript.

html
<button type="button" class="add" data-item-id="42" data-price="300">
  Add cardamom latte
</button>

<script>
  document.querySelector(".add").addEventListener("click", (event) => {
    const { itemId, price } = event.currentTarget.dataset;
    console.log(`Added item ${itemId} costing Rs. ${price}`);
  });
</script>

The name after data- becomes a camel-case property on element.dataset, so data-item-id turns into dataset.itemId. CSS can also select them, for example [data-price]. Values are always text.

Common mistakes#

  • Reusing the same id on several elements.
  • Using title as the only description of a control.
  • Forgetting the page-level lang.
  • Using hidden for content you want screen readers to read.
  • Inventing non-standard attributes without the data- prefix.

Try it yourself#

Add lang, an id and two classes to your page's main section. Create a button with three data-* attributes and print them to the console from a script. Add a hidden message and reveal it by removing the attribute from the console. Wrap a Nepali or Hindi word in a span with the correct lang.

Recap#

Global attributes such as id, class, title, lang, hidden and contenteditable work on any element. Use data-* for custom values read by CSS and JavaScript. Keep ids unique and never rely on title or data-* for important information.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-