Skip to main content

HTML References

HTML Global Attributes

Written by Updated HTML Global Attributes exercises

Global attributes are the ones you can put on any HTML element: id, class, style, title, lang, hidden, data-* and a few more. Learn these once, and you know a good part of the attributes on every tag.

Global attributes apply to all HTML elements, even if not explicitly listed in the tag definition.
For example, you can use id, class, or style with almost any tag.

Example:

html

<p id="intro" class="highlight" title="Introduction section">
  This is a paragraph with global attributes.
</p>

The paragraph gets an id to identify it, a class for styling, and a title that appears as a tooltip on hover.

The common ones

AttributeWhat it doesExample
idA unique name for the element, for links, labels and JavaScript<div id="main">
classOne or more names for styling and scripting<p class="note">
styleInline CSS for this element only<h1 style="color: blue;">
titleExtra information shown as a tooltip<abbr title="HyperText Markup Language">
langThe language of the content<p lang="hi">
dirText direction: ltr, rtl or auto<p dir="rtl">
hiddenHides the element completely<p hidden>
tabindexWhether the element can be reached with Tab<div tabindex="0">
accesskeyA keyboard shortcut<button accesskey="s">
draggableWhether it can be dragged<img draggable="false">
spellcheckTurns spell checking on or off<textarea spellcheck="true">
contenteditableLets the user edit the text<p contenteditable="true">
translateWhether translation tools should translate it<span translate="no">
data-*Your own data for JavaScript<div data-user-id="42">

Examples of Global Attribute Usage

a) Using class and id for Styling

html

<style>
  .highlight { color: crimson; font-weight: bold; }
</style>

<p id="intro" class="highlight">This is highlighted text.</p>

The paragraph gets the crimson bold style from its class, and its id makes it unique on the page.

b) Using contenteditable

html

<h3 contenteditable="true">Click here to edit this heading</h3>

Click the heading and you can type into it, right in the browser. Changes are not saved anywhere unless JavaScript does it.

c) Using data-* Attributes

html

<div data-course="HTML5" data-level="beginner">
  Learn HTML with us!
</div><br/><br/>
<code id="consoleMessage"></code>

<script>
const course = document.querySelector('div').dataset.course;
document.getElementById('consoleMessage').innerHTML = course;
</script>

The script reads data-course through element.dataset.course and shows "HTML5" below. Note how data-course becomes dataset.course; a name like data-user-id becomes dataset.userId.

tabindex: use 0 or -1, not other numbers

  • tabindex="0" puts an element that normally cannot be focused, like a custom widget, into the normal Tab order.
  • tabindex="-1" lets JavaScript focus it, but keeps it out of the Tab order.
  • Positive numbers like tabindex="1" force their own order before everything else, which almost always confuses keyboard users. Avoid them, and keep the HTML in a sensible order instead.

accesskey also has problems: shortcuts differ between browsers and often clash with screen reader keys. Use it rarely.

Example: Global Attributes in Action

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Global Attributes Example</title>
  <style>
    .editable { background-color: #f0f8ff; padding: 10px; }
  </style>
</head>
<body>

<section id="about" class="editable" contenteditable="true" title="Edit this section">
  <h2 data-section="intro">About HTML</h2>
  <p lang="en" dir="ltr">HTML stands for HyperText Markup Language.</p>
</section>

</body>
</html>

Explanation:

  • id and class used for styling and selection
  • contenteditable makes the section editable
  • title shows tooltip
  • data-* stores metadata
  • lang and dir ensure proper accessibility

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • title is not seen by touch users and is unreliable for screen readers. Do not put important information only in a tooltip.
  • hidden hides content from everyone, including screen readers. To hide something only visually, use a CSS "visually hidden" class.
  • Setting lang on the <html> element is one of the simplest accessibility wins: screen readers then use the right pronunciation.

HTML Global Attributes exercises

An id

Write the paragraph Start here. with the id start.

Result

Check your answer to see it here.