HTML Global Attributes

HTML Global Attributes are attributes that can be used on almost any HTML element. They provide common functionality such as adding identifiers, classes, styles, tooltips, accessibility support, and custom data storage. Examples include id, class, style, title, and data-*, which help improve structure, styling, and interactivity of web pages.

3 views
5 min read
Try It Yourself

Experiment with the code in an interactive editor

What are Global Attributes? (Attributes that work on ANY HTML tag)

These attributes can be used on ANY HTML element — <p>, <div>, <h1>, even <body>.

Complete list of global attributes:

Attribute

What it does

Example

When to use

id

Unique name (only ONE element can have this)

<div id="header">

To identify a specific element (for CSS or JavaScript)

class

Group name (many elements can share)

<p class="error">

To style multiple elements the same way

style

Inline CSS

<div style="color:red">

For one-time specific styling (avoid when possible)

title

Tooltip when you hover

<abbr title="World Health Organization">WHO</abbr>

To provide additional information

data-*

Store custom data for JavaScript

<button data-user-id="123">

To store extra data without showing it

hidden

Hide the element from page

<p hidden>Secret</p>

To temporarily hide content

contenteditable

User can edit the content

<div contenteditable="true">Edit me</div>

For editable content (like a text editor)

tabindex

Tab order (keyboard navigation)

<input tabindex="1">

To control keyboard navigation order

lang

Language of the element

<p lang="hi">नमस्ते</p>

For multilingual websites

spellcheck

Enable/disable spell check

<textarea spellcheck="true">

For text inputs

draggable

Allow dragging

<img draggable="true">

For drag-and-drop features

Examples with explanations:

1<!-- id: unique identifier - only one element can have this -->
2<div id="main-content">This is the main content</div>
3<!-- CSS: #main-content { color: red; } -->
4<!-- JavaScript: document.getElementById('main-content') -->
5
6<!-- class: group identifier - many elements can share this -->
7<p class="error">First error message</p>
8<p class="error">Second error message</p>
9<!-- CSS: .error { color: red; } -->
10
11<!-- data-*: store custom data for JavaScript -->
12<button data-product-id="456" data-price="29.99" onclick="addToCart(this.dataset.productId)">
13    Add to Cart
14</button>
15
16<!-- contenteditable: user can edit! -->
17<div contenteditable="true">Click and edit this text!</div>
18
19<!-- hidden: not visible -->
20<p hidden>This text is hidden</p>
21
22<!-- title: shows tooltip on hover -->
23<p title="This is a tooltip - hover to see">Hover your mouse over me</p>