HTML Global Attributes
In the Attributes lesson, you learned that many attributes belong to specific tags: href goes on links, src goes on images, type goes on inputs. But a special group of attributes breaks that pattern. They are valid on almost every HTML element you will ever write.
These are the global attributes, and they are some of the most-used pieces of HTML in the real world. Every professional web page relies on them for styling, navigation, accessibility, scripting, and more.
A global attribute is an attribute that can be added to any HTML element, regardless of its tag name. Some may have no visible effect on certain elements, but they are still allowed.
The Big Pictureβ
There are quite a few global attributes, so it helps to sort them into groups by purpose. This map shows the ones you will meet most often:
Let's take them group by group.
Group 1: Identifying Elementsβ
The id attributeβ
The id attribute gives an element a unique name within the page. Think of it like a passport number: no two elements on the same page should share one.
<h2 id="pricing">Pricing</h2>
<p id="pricing-note">All prices include tax.</p>
Why is a unique name useful?
- CSS can target exactly one element:
#pricing { ... } - JavaScript can find it quickly:
document.getElementById("pricing") - Links can jump straight to it:
<a href="#pricing">Jump to pricing</a> - Labels can connect to form fields (you will see this in the forms module).
Rules for a good id:
| Rule | Good | Bad |
|---|---|---|
| Must be unique in the document | id="hero" used once | id="hero" used twice |
| Must not contain spaces | id="main-title" | id="main title" |
| Is case-sensitive | Hero and hero are different | Mixing cases by accident |
| Should start with a letter | id="section-1" | id="1-section" (valid but awkward in CSS) |
| Should describe the purpose | id="contact-form" | id="div7" |
The class attributeβ
While an id is unique, a class is meant to be shared. It groups elements that should look or behave alike, like labeling several students as "Team Blue".
<p class="note">Remember to save your work.</p>
<p class="note">Take a break every hour.</p>
<p class="note important">Submit before Friday!</p>
An element can carry several classes, separated by spaces. In the last paragraph above, note and important both apply.
- π id
- π·οΈ class
- Must be unique on the page
- An element has at most one
id - Targets one specific element
- CSS selector starts with
#
<header id="site-header">...</header>
- Can be reused on many elements
- An element can have many classes
- Targets groups of elements
- CSS selector starts with
.
<button class="btn btn-primary">Save</button>
Use class for styling and grouping (your default choice), and use id for the few cases where you need to point at one specific element, such as a link target or a script hook.
Group 2: Style and Visibilityβ
The style attributeβ
The style attribute lets you write CSS directly on one element:
<p style="color: crimson; font-size: 20px;">This text is red and larger.</p>
This text is red and larger.
It is handy for quick experiments and for values that must be set per element. But for real projects, it is better to write styles in a separate CSS file and connect them with class.
- π‘ Inline style
- β Class + CSS file
<p style="color: crimson;">Warning</p>
<p style="color: crimson;">Error</p>
<p style="color: crimson;">Alert</p>
Repeated everywhere. Changing the color means editing every line.
<p class="danger">Warning</p>
<p class="danger">Error</p>
<p class="danger">Alert</p>
.danger {
color: crimson;
}
Change the color once, and every element updates.
The hidden attributeβ
The hidden attribute is a boolean attribute (remember those?) that tells the browser this element is not currently relevant, so it should not be displayed.
<p>This paragraph is visible.</p>
<p hidden>This paragraph is hidden.</p>
This paragraph is visible.
This paragraph is hidden.
Hidden content is skipped by browsers and screen readers, which makes it useful for content you plan to reveal later with JavaScript, such as a popup message or a tab panel.
Like a comment, a hidden element is still present in the page's source code. Anyone can see it through DevTools, so never hide sensitive information this way.
Group 3: Language and Textβ
The lang attributeβ
You already used lang on the <html> tag, but it can be applied to any element, which is perfect for marking a phrase in a different language.
<html lang="en">
...
<p>The French say <span lang="fr">c'est la vie</span>, meaning "that's life".</p>
</html>
This helps screen readers pronounce the phrase correctly, and helps browsers offer accurate translation and hyphenation.
The dir attributeβ
The dir attribute sets the text direction. Languages such as Arabic, Hebrew, and Urdu are written from right to left.
| Value | Meaning |
|---|---|
ltr | Left to right (default for most languages) |
rtl | Right to left |
auto | Let the browser detect it from the content |
<p dir="rtl" lang="ar">Ω
Ψ±ΨΨ¨Ψ§ Ψ¨ΩΩ
ΩΩ Ω
ΩΩΨΉΩΨ§</p>
This line reads left to right.
Ω Ψ±ΨΨ¨Ψ§ Ψ¨ΩΩ ΩΩ Ω ΩΩΨΉΩΨ§
The translate and spellcheck attributesβ
Two smaller but useful helpers:
- π€ translate
- β spellcheck
Tells translation tools whether to translate an element. Use no for brand names, code, or anything that should stay untouched.
<p>Welcome to <span translate="no">CodeHarborHub</span>!</p>
Turns the browser's spell checker on or off for editable text, such as form fields.
<textarea spellcheck="true"></textarea>
<input type="text" spellcheck="false" />
Turn it off for things like usernames or code, where red squiggly lines would only be distracting.
Group 4: Interactionβ
The tabindex attributeβ
When people navigate a page with the Tab key (very common for keyboard and assistive-technology users), the browser moves through interactive elements like links and buttons. The tabindex attribute controls this behavior.
| Value | What it does |
|---|---|
tabindex="0" | Makes any element reachable by keyboard, in its natural page order |
tabindex="-1" | Makes an element focusable by script, but skips it in Tab navigation |
tabindex="1" or higher | Forces a custom tab order (avoid this) |
<div tabindex="0">This div can now receive keyboard focus.</div>
Numbers greater than zero override the natural order and create a confusing, hard-to-maintain experience for keyboard users. Stick to 0 and -1.
The contenteditable attributeβ
Add contenteditable="true" and the visitor can edit that element's text directly in the page, no form required.
<p contenteditable="true">Click here and start typing to change this text!</p>
Click here and start typing to change this text!
Changes exist only in the visitor's browser and vanish on refresh, unless you add JavaScript to save them. It is the foundation of many rich text editors and note-taking apps.
The draggable attributeβ
Set draggable="true" to let an element be dragged with the mouse. The full drag-and-drop behavior needs JavaScript, but the attribute is the starting point.
<p draggable="true">Drag me!</p>
The accesskey attributeβ
accesskey lets you assign a keyboard shortcut to an element:
<a href="/search" accesskey="s">Search</a>
Browsers and operating systems use different key combinations to trigger it, and they often clash with existing shortcuts, so it is rarely recommended today.
Group 5: Help and Custom Dataβ
The title attributeβ
The title attribute adds extra information that appears as a tooltip when a visitor hovers over the element with a mouse.
<abbr title="HyperText Markup Language">HTML</abbr>
Hover over HTML to see the full name.
title for important informationTooltips do not appear on touch screens, and many keyboard and screen-reader users never see them. Anything essential should be visible as normal text on the page. Think of title as a small bonus, not the main message.
The data-* attributesβ
You met these in the previous lesson. They let you attach your own custom data to any element, ready for JavaScript to use:
<li data-product-id="8421" data-category="books">HTML for Everyone</li>
const item = document.querySelector("li");
console.log(item.dataset.productId); // "8421"
console.log(item.dataset.category); // "books"
A Few Newer Global Attributesβ
Modern HTML keeps adding helpful global attributes. Here are two worth knowing about, even if you do not use them yet:
| Attribute | What it does |
|---|---|
inert | Makes an element and everything inside it non-interactive: it cannot be clicked, focused, or read by assistive tools. Useful behind a modal dialog. |
popover | Turns an element into a popup that can be shown and hidden without writing JavaScript. |
These are newer features, so it is a good habit to check browser support (for example on MDN) before using them in a production project.
Quick Reference Tableβ
| Attribute | Purpose | Example |
|---|---|---|
id | Unique identifier | id="contact" |
class | Shared group name(s) | class="card featured" |
style | Inline CSS | style="color: red;" |
hidden | Hides the element | hidden |
lang | Language of the content | lang="fr" |
dir | Text direction | dir="rtl" |
translate | Allow or block translation | translate="no" |
spellcheck | Toggle spell checking | spellcheck="false" |
tabindex | Keyboard focus behavior | tabindex="0" |
contenteditable | Makes content editable | contenteditable="true" |
draggable | Allows dragging | draggable="true" |
accesskey | Keyboard shortcut | accesskey="s" |
title | Tooltip text | title="More info" |
data-* | Custom data for scripts | data-user-id="42" |
Common Mistakes with Global Attributesβ
1. Using the same id on more than one element
<!-- β Duplicate ids: which one does a link or script find? -->
<p id="note">First</p>
<p id="note">Second</p>
<!-- β
Use a class for repeated items -->
<p class="note">First</p>
<p class="note">Second</p>
2. Putting spaces in an id, or commas in a class list
<!-- β Incorrect -->
<h1 id="main title" class="big, bold">Welcome</h1>
<!-- β
Correct -->
<h1 id="main-title" class="big bold">Welcome</h1>
An id cannot contain spaces, and multiple classes are separated by spaces only, never commas.
3. Writing the same attribute twice on one element
<!-- β The second style is ignored -->
<p style="color: red" style="font-size: 20px">Hello</p>
<!-- β
Merge them into one attribute -->
<p style="color: red; font-size: 20px">Hello</p>
4. Overusing inline styles
Inline styles are hard to reuse, hard to override, and make your HTML cluttered. Prefer classes and a separate stylesheet.
5. Using a positive tabindex
Values like tabindex="5" create a custom, fragile tab order that confuses keyboard users. Use 0 or -1 instead, and let the natural page order do the work.
Try It Yourselfβ
This snippet contains four mistakes related to global attributes. Can you find them all?
<h1 id="page title" class="big, bold">Welcome</h1>
<p id="intro" style="color: red" style="font-size: 20px">Hello!</p>
<div id="intro" tabindex="5">Extra content</div>
π Click to reveal the solution
<h1 id="page-title" class="big bold">Welcome</h1>
<p id="intro" style="color: red; font-size: 20px">Hello!</p>
<div id="extra" tabindex="0">Extra content</div>
id="page title"contained a space, so it becamepage-title.class="big, bold"used a comma, but classes are separated by spaces only.- The paragraph had two
styleattributes, which were merged into one. - Two elements shared
id="intro", so the second was renamed. Thetabindex="5"was also changed to0.
Quick Recapβ
- Global attributes work on almost every HTML element.
idmust be unique, whileclasscan be shared, and elements can have several classes.styleapplies inline CSS, but classes with a stylesheet are the better long-term choice.hiddenhides an element, andlanganddirdescribe language and text direction.tabindexcontrols keyboard focus. Stick to0and-1.contenteditableanddraggableadd interactivity, andtitleadds a helpful tooltip.data-*attributes store custom data for JavaScript.
Frequently Asked Questionsβ
What are global attributes in HTML?
Global attributes are attributes that can be used on any HTML element, such as id, class, style, title, lang, and hidden. Unlike tag-specific attributes such as href or src, they are not tied to one kind of element.
What is the difference between id and class?
An id uniquely identifies a single element and must not be repeated on a page. A class groups elements together and can be reused on as many elements as you like. An element can also have multiple classes.
Can an element have more than one class?
Yes. Separate the class names with spaces, for example class="card featured large".
Should I use the style attribute or a CSS file?
For real projects, use a CSS file with classes. Inline styles are fine for quick tests, but they are harder to maintain and reuse.
Is the title attribute good for accessibility?
Not on its own. Tooltips do not appear on touch devices and are often missed by keyboard and screen-reader users, so never put essential information only in a title attribute.
What is the difference between hidden and CSS display none?
They have a similar visual effect, since hidden elements are not shown. The hidden attribute is part of the HTML itself and is easy to toggle with JavaScript, while display: none is a CSS rule that can also be applied conditionally through stylesheets.
Test Your Understandingβ
- What makes an attribute "global"?
- Why must an
idbe unique, but aclasscan repeat? - How do you give an element three classes at once?
- Which
tabindexvalues are recommended, and which should you avoid? - Why is the
titleattribute a poor place for essential information?