OnlineCBT Logo OnlineCBT
HTML Tutorial
HTML · LESSON 49

Custom Data Attributes (data-*)

By Ratan Updated: 16 Oct 2026

Store custom private information inside HTML elements using data-* attributes.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

1. Custom Data Attributes: data-*

HTML5 introduced custom data attributes (prefixed with data-) to store extra, private custom data directly on HTML elements without hacking non-standard attributes or abusing CSS classes.

2. Structure and Syntax

The attribute name must start with data- followed by lowercase characters and hyphens:


3. Reading Custom Data via JavaScript dataset

In modern JavaScript, all data-* attributes are accessible through the element's dataset property:

const button = document.querySelector(".question-nav-btn");

// Accessing values
console.log(button.dataset.questionId); // "q-401"
console.log(button.dataset.subject);    // "physics"
console.log(button.dataset.marks);      // "4"

4. Styling Elements Based on Data Attributes

/* Highlight hard questions in exam palette */
button[data-difficulty="hard"] {
  border-left: 4px solid #ef4444;
}

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat