Global Attributes & Custom Data Attributes (data-*, tabindex, contenteditable)

๐ŸŸก Intermediate

๐Ÿ“– Definition

Global Attributes are attributes that can be applied to ANY HTML element. Custom data-* attributes allow developers to store custom application data directly inside HTML elements, which can be easily accessed by CSS and JavaScript.

๐ŸŒ Multilingual Summary

English

Global attributes (id, class, title, lang, dir, hidden, tabindex, contenteditable, spellcheck) apply to any element. Custom data-* attributes store custom application data for JavaScript.

Hindi

Global attributes (id, class, title, lang, dir, hidden, tabindex, contenteditable) kisi bhi HTML element par apply hote hain. Custom data-* attributes JavaScript ke liye custom data store karte hain.

Marathi

Global attributes (id, class, title, lang, dir, hidden, tabindex, contenteditable) kontyahi HTML element var laagu hotat. Custom data-* attributes JavaScript sathi custom data store kartat.

๐Ÿค” Why Do We Use Global & Data Attributes?

Global attributes control element accessibility, focus order (tabindex), inline text editing (contenteditable), and element visibility (hidden). Custom data-* attributes bridge HTML markup with JavaScript interactivity without cluttering CSS class names.

๐Ÿงฑ Key Global Attributes Reference

Global AttributeDescription & FunctionalityCode Example
idUnique identifier for a single element on a page.<div id="unique-card">
classReusable space-separated class names for CSS styling.<div class="card shadow">
titleAdvisory hover tooltip text displayed on mouse hover.<span title="Tooltip text">Hover me</span>
langSpecifies element language (en, hi, mr, fr).<p lang="fr">Bonjour</p>
dirSpecifies text direction (ltr left-to-right, rtl right-to-left).<p dir="rtl">...</p>
hiddenHides element from visual layout and screen readers.<p hidden>Secret text</p>
tabindexControls keyboard focus order (tabindex="0" adds to tab order; tabindex="-1" allows JS focus).<div tabindex="0">Focusable div</div>
contenteditableMakes element text content directly editable by users in the browser ("true" or "false").<div contenteditable="true">Edit text</div>
spellcheckEnables browser spellchecking on editable elements ("true" or "false").<div spellcheck="true">...</div>
draggableEnables HTML5 drag-and-drop capability ("true" or "false").<div draggable="true">Drag me</div>
data-*Custom data attributes storing state or values for JS (data-user-id, data-category).<button data-user-id="101">

๐Ÿ”‘ Custom data-* Attributes in Detail

Custom data attributes must start with data- followed by lowercase attribute names (e.g. data-user-id, data-status, data-price):

<article 
  class="product-item" 
  data-id="p-99" 
  data-category="electronics" 
  data-price="299" 
  data-in-stock="true">
  <h3>Wireless Headphones</h3>
</article>

In JavaScript, you can read these values instantly via the element's .dataset object:

const item = document.querySelector('.product-item');
console.log(item.dataset.category); // Outputs: "electronics"
console.log(item.dataset.price);    // Outputs: "299"

In CSS, you can target elements based on data attributes:

article[data-in-stock="true"] {
  border-left: 4px solid green;
}

๐Ÿ’ป Code Example

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Global and Data Attributes Practice</title>
</head>
<body>

  <h1>Global Attributes Demonstration</h1>

  <!-- Content Editable Notes Block -->
  <section>
    <h2>Editable Notes Block</h2>
    <div contenteditable="true" spellcheck="true" style="border: 1px solid #ccc; padding: 10px;">
      Click inside this box to edit text directly in your browser!
    </div>
  </section>

  <br>

  <!-- Product Item using custom data-* attributes -->
  <section>
    <h2>Product Filter Target</h2>
    <div 
      id="product-card-1" 
      class="card product-card" 
      title="Hover for product details" 
      tabindex="0" 
      data-product-id="101" 
      data-category="gadgets" 
      data-discount="20">
      <h3>Smart Watch Model X</h3>
      <p>Custom data attributes store product ID (101) and discount (20%) for JavaScript.</p>
    </div>
  </section>

</body>
</html>

๐Ÿ‘€ Output / What You Will See

  • An interactive, editable text box where visitors can type directly on screen.
  • A focusable product card containing tooltip descriptions, keyboard tabbing capabilities, and stored data-* attributes for JavaScript processing.

๐Ÿงช Try It Yourself

1. Create a <div> with contenteditable="true" and spellcheck="true".

2. Create three list items (<li>) holding custom attributes data-category="fruits" and data-price="10".

3. Test keyboard navigation using tabindex="0".

โš ๏ธ Common Mistakes

  • Using uppercase letters in data-* names: data-userID is converted to lowercase dataset.userid in JavaScript, causing bug confusion. Use lowercase hyphens data-user-id.
  • Using positive tabindex values (e.g. tabindex="4"): Disrupts natural document keyboard navigation order.

๐ŸŒ Real-World Usage

Modern web applications use data-* attributes for filtering lists, storing UI modal states, tracking analytics click events, and configuring frontend JavaScript widgets.

๐Ÿ”— Related Topics

๐Ÿ’ก Remember

  • Global attributes apply to ALL HTML elements.
  • contenteditable="true" makes text editable in the browser.
  • Use data-* attributes to store custom data for JavaScript (element.dataset).
  • Avoid positive tabindex values.

๐Ÿงญ Navigation

โ† Previous: Interactive Elements | HTML Home | Next: HTML SEO & Open Graph โ†’