Skip to main content

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.

Quick definition

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:

RuleGoodBad
Must be unique in the documentid="hero" used onceid="hero" used twice
Must not contain spacesid="main-title"id="main title"
Is case-sensitiveHero and hero are differentMixing cases by accident
Should start with a letterid="section-1"id="1-section" (valid but awkward in CSS)
Should describe the purposeid="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.

  • 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>
Beginner rule of thumb

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>
http://127.0.0.1:5500/index.html

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.

<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.

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>
http://127.0.0.1:5500/index.html

This paragraph is visible.

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.

Hidden is not private

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.

ValueMeaning
ltrLeft to right (default for most languages)
rtlRight to left
autoLet the browser detect it from the content
<p dir="rtl" lang="ar">Ω…Ψ±Ψ­Ψ¨Ψ§ Ψ¨ΩƒΩ… في Ω…ΩˆΩ‚ΨΉΩ†Ψ§</p>
http://127.0.0.1:5500/index.html

This line reads left to right.

Ω…Ψ±Ψ­Ψ¨Ψ§ Ψ¨ΩƒΩ… في Ω…ΩˆΩ‚ΨΉΩ†Ψ§

The translate and spellcheck attributes​

Two smaller but useful helpers:

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>

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.

ValueWhat 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 higherForces a custom tab order (avoid this)
<div tabindex="0">This div can now receive keyboard focus.</div>
Avoid positive tabindex values

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>
http://127.0.0.1:5500/index.html

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>
http://127.0.0.1:5500/index.html

Hover over HTML to see the full name.

Don't rely on title for important information

Tooltips 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:

AttributeWhat it does
inertMakes an element and everything inside it non-interactive: it cannot be clicked, focused, or read by assistive tools. Useful behind a modal dialog.
popoverTurns an element into a popup that can be shown and hidden without writing JavaScript.
note

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​

AttributePurposeExample
idUnique identifierid="contact"
classShared group name(s)class="card featured"
styleInline CSSstyle="color: red;"
hiddenHides the elementhidden
langLanguage of the contentlang="fr"
dirText directiondir="rtl"
translateAllow or block translationtranslate="no"
spellcheckToggle spell checkingspellcheck="false"
tabindexKeyboard focus behaviortabindex="0"
contenteditableMakes content editablecontenteditable="true"
draggableAllows draggingdraggable="true"
accesskeyKeyboard shortcutaccesskey="s"
titleTooltip texttitle="More info"
data-*Custom data for scriptsdata-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?

broken-global.html
<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
fixed-global.html
<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>
  1. id="page title" contained a space, so it became page-title.
  2. class="big, bold" used a comma, but classes are separated by spaces only.
  3. The paragraph had two style attributes, which were merged into one.
  4. Two elements shared id="intro", so the second was renamed. The tabindex="5" was also changed to 0.

Quick Recap​

  • Global attributes work on almost every HTML element.
  • id must be unique, while class can be shared, and elements can have several classes.
  • style applies inline CSS, but classes with a stylesheet are the better long-term choice.
  • hidden hides an element, and lang and dir describe language and text direction.
  • tabindex controls keyboard focus. Stick to 0 and -1.
  • contenteditable and draggable add interactivity, and title adds 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​

  1. What makes an attribute "global"?
  2. Why must an id be unique, but a class can repeat?
  3. How do you give an element three classes at once?
  4. Which tabindex values are recommended, and which should you avoid?
  5. Why is the title attribute a poor place for essential information?