CHAPTER 53 · HTML

Global attributes, languages and text direction

Use shared HTML settings deliberately. Separate names, language, direction, editing and visibility instead of treating every attribute like a simple on/off switch.

Download example files (ZIP)

A quick map of shared attributes

A global attribute can be used across HTML elements, though its useful effect depends on the element and context. It does not mean every setting is appropriate everywhere. Here are common groups to recognise.

AttributeMain jobImportant limit
id and classIdentify one element or reusable groupsId must be unique in a document
titleSupply advisory informationNot a replacement for visible instructions
lang and dirDescribe language and text directionNeither translates the text
data-*Hold custom page dataNot a secret storage area
hidden and inertControl availability in different waysNot access control
contenteditableAllow editing of element contentDoes not save or submit changes
spellcheck and translateGive spelling or translation hintsTools decide how to apply them
tabindex and autofocusInfluence focusUse carefully; do not invent a confusing order
draggableDescribe dragging behaviourDoes not build a full drop workflow
styleAdd presentation declarationsPrefer maintainable CSS rules where appropriate

Identity and custom data

Id identifies one element for links, labels, styling or scripts. Class supplies reusable names and may contain several space-separated class names. An id value is not a global JavaScript variable you should depend on implicitly; look up the element deliberately.

Data-* attributes store custom text values in markup, such as data-topic=languages. A script can read that value through element.dataset.topic. It is visible to visitors and can be changed, so do not store passwords or trust it as proof of a permission. Data attributes do not create behaviour on their own.

Language and character encoding are different

Lang tells tools the language of the content. We use en for English, hi for Hindi and ar for Arabic. A smaller element can declare a language different from the surrounding page. This can help pronunciation, spelling tools and translation.

UTF-8 is the character encoding used to read saved bytes, not the spoken language. A UTF-8 document can contain all three greetings. Lang does not translate words, and declaring hi on English text does not convert it into Hindi.

Direction and mixed text

Dir=ltr means left-to-right direction, rtl means right-to-left, and auto lets the browser determine an appropriate direction from the text. Language alone does not necessarily set the direction you intend. The Arabic greeting explicitly uses rtl.

Bdi isolates a piece of text, such as a name, so its direction does not unexpectedly rearrange surrounding punctuation and words. Bdo can deliberately override direction, but overriding is a specialised task rather than the default solution for mixed-language text. Write the actual words in their natural order rather than reversing them in the source.

Hidden, inert and privacy

Ordinary hidden content is not currently rendered. Hidden="false" still leaves it hidden; remove the attribute for normal display. Hidden also has an until-found state that supporting browsers can reveal through find-in-page or fragment navigation. That newer state has different layout and support details; do not assume it behaves like ordinary hidden everywhere.

Inert leaves content visually present but makes its subtree unavailable for normal focus and interaction and removes it from the accessibility tree. It can help manage temporarily inactive interface areas. Neither setting deletes content from the source or authorises access. Do not hide the only explanation of an essential task.

Editable content does not mean a saved document

Contenteditable=true permits editing, false prevents editing at that level, and plaintext-only permits plain text editing without rich formatting where supported. These are enumerated values, not an ordinary boolean attribute rule. If the value is missing or invalid, editability can be inherited.

Our editable area has a textbox role, an accessible name, a description and a visible focus outline. A div cannot be labelled with for in the same native way as input, so it references the visible naming text through aria-labelledby. An ordinary textarea is usually simpler when collecting a form answer.

Spelling, translation, dragging and shortcuts

Spellcheck=true or false is a hint about spelling checks on editable content; availability depends on browser and language tools. Translate=no asks translation tools to preserve a term such as a brand. These attributes do not guarantee every tool behaves the same way.

Draggable=true can permit dragging, but accepting drops and updating application data needs a designed interaction. Draggable=false is meaningful; it is not the same attribute category as open. Accesskey can define a shortcut, but conflicts and operating-system differences make it unsuitable as the only route to a task. Keep normal controls available.

Focus and advisory information

Tabindex=0 participates in ordinary focus order where appropriate; -1 permits programmatic focus without adding the usual Tab stop. Positive values can create a confusing order. Autofocus can move focus automatically, so reserve it for a clear interaction such as the short modal example rather than stealing focus on every page.

Title may appear as a pointer tooltip, but is unreliable as the only instruction for keyboard or touch users. Put useful wording visibly on the page. Read attribute definitions individually: hidden, aria-expanded, spellcheck and contenteditable do not all interpret the text false in the same way.

Follow the idea

Choose a setting by its job
  1. 1Identity: id, class and custom data
  2. 2Language and direction: lang, dir and bdi
  3. 3Availability: hidden and inert
  4. 4Editing and focus: deliberate native behaviour

Shared attributes have different value rules; false does not mean the same thing everywhere.

Complete program: language-attributes.html

Save this as language-attributes.html. Open it in your browser.

language-attributes.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Language and Direction Practice</title>
  </head>
  <body>
    <h1>Describe words from different languages</h1>
    <main>
      <section id="greetings" class="practice-card" data-topic="languages">
        <h2>Three short greetings</h2>
        <p lang="en">Hello</p>
        <p lang="hi">नमस्ते</p>
        <p lang="ar" dir="rtl">مرحبا</p>
        <p>Practice name: <bdi lang="ar" dir="auto">نور</bdi> — Group A.</p>
        <p translate="no">Resume-Day</p>
      </section>
    </main>
  </body>
</html>
Browser previewDocument title: Language and Direction Practice

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: English, Hindi and Arabic greetings in one UTF-8 document. The Arabic paragraph uses right-to-left direction and the practice name is isolated from surrounding text. Lang and dir describe content; they do not translate it.

Complete program: editable-draft.html

Save this as editable-draft.html. Open it in your browser.

editable-draft.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An Editable Local Draft</title>

    <style>
      [contenteditable] {
        border: 1px solid #2458a6;
        padding: 1rem;
        min-height: 4rem;
      }
      [contenteditable]:focus-visible {
        outline: 3px solid #2458a6;
        outline-offset: 3px;
      }
    </style>
  </head>
  <body>
    <h1>Try a small editable draft</h1>
    <main>
      <p id="draft-label">Practice draft</p>
      <p id="draft-help">
        Edit the sample sentence. Changes are not saved or submitted; reloading restores the
        original file.
      </p>
      <div
        id="draft"
        contenteditable="plaintext-only"
        role="textbox"
        aria-multiline="true"
        aria-labelledby="draft-label"
        aria-describedby="draft-help"
        spellcheck="true"
      >
        My next practice topic is links.
      </div>
    </main>
  </body>
</html>
Browser previewDocument title: An Editable Local Draft

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result in a supporting browser: the bordered, named draft can receive focus and be edited as plain text. It does not become a saved file or submit a form. Reloading the saved document restores the original sentence.

Problem and solution

Problem: a student sets hidden="false" and expects a panel to become visible.

Solution: remove hidden for the normal visible state. Do not transfer rules from enumerated attributes such as contenteditable to hidden without checking their definitions.

Common mistakes and useful tips

  • Do not confuse lang with UTF-8.
  • Do not reverse natural text to imitate right-to-left direction.
  • Data attributes are visible and untrusted.
  • Contenteditable does not save work.
  • Do not rely on title or accesskey as the only instruction or route.
  • Check each attribute's value rules separately.

Practice questions

  1. What does global mean for an attribute?
  2. What does lang=hi describe?
  3. Does dir=rtl translate English into Arabic?
  4. Is data-topic a private secret?
  5. Which element is usually simpler for a multiline form answer?
  6. Save the draft example, edit its sentence, then reload. What happened and why?
Check your practice answers
  1. It is available across HTML elements, although useful behaviour depends on context.
  2. The content language is Hindi.
  3. No. It controls direction, not translation.
  4. No. It is visible page data and can be altered.
  5. Textarea, with its native label and form behaviour.
  6. The in-page draft changed temporarily, then reverted. No saving or submission was implemented.

Interview questions

How do id and class differ?

Id uniquely identifies one element in the document. Classes can be reused and combined to select groups.

Why use bdi for an inserted name?

It isolates that text's direction so mixed-direction content is less likely to disturb the surrounding wording or punctuation.

How do hidden and inert differ?

Ordinary hidden content is not rendered. Inert can remain visible while being unavailable for normal interaction and assistive navigation.

Why is contenteditable not a complete form control?

It enables editing but does not automatically supply native form submission, saving, validation or a suitable name. Use the right native control when possible.

Quick revision

  • Use attributes for specific jobs.
  • Separate encoding, language and direction.
  • Editing and visibility are not storage or security.
  • Keep useful instructions and routes visible.