- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Global Attributes
HTML References
HTML Global Attributes
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
| Attribute | What it does | Example |
|---|---|---|
| id | A unique name for the element, for links, labels and JavaScript | <div id="main"> |
| class | One or more names for styling and scripting | <p class="note"> |
| style | Inline CSS for this element only | <h1 style="color: blue;"> |
| title | Extra information shown as a tooltip | <abbr title="HyperText Markup Language"> |
| lang | The language of the content | <p lang="hi"> |
| dir | Text direction: ltr, rtl or auto | <p dir="rtl"> |
| hidden | Hides the element completely | <p hidden> |
| tabindex | Whether the element can be reached with Tab | <div tabindex="0"> |
| accesskey | A keyboard shortcut | <button accesskey="s"> |
| draggable | Whether it can be dragged | <img draggable="false"> |
| spellcheck | Turns spell checking on or off | <textarea spellcheck="true"> |
| contenteditable | Lets the user edit the text | <p contenteditable="true"> |
| translate | Whether 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:
idandclassused for styling and selectioncontenteditablemakes the section editabletitleshows tooltipdata-*stores metadatalanganddirensure proper accessibility
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Good to know
titleis not seen by touch users and is unreliable for screen readers. Do not put important information only in a tooltip.hiddenhides content from everyone, including screen readers. To hide something only visually, use a CSS "visually hidden" class.- Setting
langon 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
