# Global Attributes & i18n

> Global attributes work on any HTML element regardless of tag. A small set of them -- `lang`, `dir`, `id`, `class`, `data-*`, and `hidden` -- are the foundation of accessible, internationalised, and well-structured pages.

Subject: HTML | Lesson 36 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/global-attributes-and-i18n
Previous: [Keyboard, Focus & Skip Links](https://codewithanimation.in/html/keyboard-and-focus.md) | Next: [Title, Meta & Indexing](https://codewithanimation.in/html/title-meta-and-indexing.md)

## What global attributes are

Global attributes are HTML attributes you can place on any element, not just specific ones. `class` and `id` are the most familiar: `id` must be unique per page and serves as a fragment target, an AT label anchor, and a JavaScript handle; `class` carries zero semantic meaning but is the primary CSS and JS hook. `hidden` is a boolean attribute that removes an element from both the visual layout and the accessibility tree, which makes it stronger than `display:none` in CSS alone because assistive technology also ignores it.

The `data-*` family lets you store arbitrary key-value pairs directly in the markup. Any attribute starting with `data-` is valid. JavaScript reads and writes them through the `dataset` API: `el.dataset.userId` reads `data-user-id`. CSS can target them with `[data-state='open']` selectors. They carry no semantic meaning for assistive technology, so they are the right place to store UI state that drives JavaScript behaviour without polluting the class list.

## Why lang and dir matter for i18n

The `lang` attribute on `<html>` tells the browser, search engines, and screen readers what language the page is in. Screen readers use it to choose the correct voice and pronunciation engine. Without it, an English reader might pronounce French or Arabic text using English phonetics, making it unintelligible. WCAG 3.1.1 (Language of Page, Level AA) requires it. When a passage within a page switches language, wrap it in any element with `lang` set to the new language; this covers WCAG 3.1.2 (Language of Parts, Level AA).

The `dir` attribute controls the text direction and -- crucially -- the inline layout direction. Setting `dir="rtl"` on a container does more than align text to the right: it mirrors the reading flow, reverses the order of flex children that use `flex-start`/`flex-end`, swaps the meaning of logical CSS properties (`margin-inline-start` becomes the right margin instead of the left), and moves form labels and icons to the expected side for Arabic, Hebrew, and Persian users. Always pair `dir` on `<html>` or a container with the matching `lang` attribute.

## How to use them correctly

Set `lang` on the `<html>` element using a valid BCP 47 language tag: `lang="en"` for English, `lang="ar"` for Arabic, `lang="zh-Hant"` for Traditional Chinese. For inline overrides, wrap the foreign passage: `<span lang="fr">Bonjour</span>`. Do not guess the tag; look it up in the IANA Language Subtag Registry.

For bidirectional text, prefer `dir="auto"` on mixed-content containers and `dir="rtl"` on fully RTL containers. Use CSS logical properties (`padding-inline-start`, `text-align: start`, `border-inline-end`) instead of physical properties (`padding-left`, `text-align: left`) so your layout adapts automatically when direction changes. Avoid hard-coded `left` and `right` in RTL-capable layouts.

Use `data-*` attributes to drive JavaScript and CSS hooks. Name them with hyphens in HTML (`data-is-open`) and access them in JS via `dataset.isOpen` (camelCase conversion is automatic). In CSS, select by state: `[data-is-open='true'] { display: block; }`. Never use `data-*` to pass information that assistive technology needs to understand; use ARIA for that.

## Common mistake: Omitting lang on the html element

The single most common i18n oversight is writing `<html>` without a `lang` attribute. Screen readers then fall back to the user's OS language setting, which may bear no relation to the page's actual language. A German reader loads an Arabic page; the screen reader uses German phonetics to attempt Arabic pronunciation; the content becomes incomprehensible. This also fails WCAG 3.1.1, an AA requirement that most accessibility audits check first. The fix takes two seconds: `<html lang="en">`.

## Tip: Test your RTL layout in one step

Add `dir="rtl"` to your `<html>` element temporarily and reload the page. Every layout that breaks -- icons on the wrong side, text truncated from the wrong end, progress bars filling right-to-left when they should not -- reveals a hardcoded physical direction in your CSS. Fix each by switching to a logical property (`left` to `inline-start`, `right` to `inline-end`, `padding-left` to `padding-inline-start`). Remove the `dir` attribute when done. This test finds every physical-direction assumption in under a minute.

## Try it: Try it

Toggle `dir` between ltr and rtl and watch the layout mirror. Switch the active `lang` to see how the attribute is applied inline.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/global-attributes-and-i18n

## Check yourself

### Question 1

Which WCAG success criterion does a missing lang attribute on the html element violate?

- A. 3.1.1 Language of Page (Level AA)
- B. 1.3.1 Info and Relationships (Level A)
- C. 4.1.2 Name, Role, Value (Level A)

Correct answer: A

WCAG 3.1.1 (Language of Page, Level AA) requires that the default human language of each page can be programmatically determined. Setting `lang` on `<html>` satisfies this.

- Why not B: 1.3.1 covers structural information being programmatically conveyed, not language declaration. Language of Page is WCAG 3.1.1.
- Why not C: 4.1.2 covers interactive component names and states, not page language. Language of Page is WCAG 3.1.1.

### Question 2

What does setting dir="rtl" on a container affect beyond text alignment?

- A. The order of flex children and the meaning of inline logical CSS properties
- B. Only the horizontal text-align of inline text
- C. The lang attribute is automatically inferred from dir

Correct answer: A

`dir="rtl"` mirrors the inline layout: flex ordering, logical property sides, and text flow all reverse. Use CSS logical properties (`padding-inline-start` instead of `padding-left`) so your layout adapts automatically.

- Why not B: `dir="rtl"` affects the full inline layout direction: flex children that rely on start/end ordering are mirrored, logical properties like `margin-inline-start` swap sides, and the reading flow reverses.
- Why not C: `dir` and `lang` are independent attributes. `dir` controls layout direction; `lang` controls language for AT pronunciation. Setting one does not set the other.

### Question 3

An element has data-panel-open="true". How does JavaScript read this value?

- A. el.dataset.panelOpen
- B. el.getAttribute('data-panel-open')
- C. el.data['panel-open']

Correct answer: A

The `dataset` API on every DOM element exposes `data-*` attributes as a `DOMStringMap`. Hyphens in attribute names become camelCase: `data-panel-open` maps to `el.dataset.panelOpen`.

- Why not B: That also works, but `el.dataset.panelOpen` is the idiomatic modern API. The browser automatically converts hyphen-separated attribute names to camelCase in the `dataset` map.
- Why not C: There is no `el.data` property. The DOM API for `data-*` attributes is `el.dataset`, which exposes them as a DOMStringMap with camelCase keys.

## Remember this

- `lang` on `<html>` is required for WCAG 3.1.1 (AA). Use a valid BCP 47 tag like `"en"` or `"ar"`.
- `dir="rtl"` mirrors the full inline layout, not just text -- flex ordering and logical CSS properties are affected.
- `data-*` attributes store UI state for JS and CSS; they carry no semantic meaning for AT.
- `hidden` removes an element from both the visual layout and the accessibility tree.
