
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
Using Data-Attributes (data-*) in CSS
We can store extra information about elements using data-* attribute. The following examples illustrate CSS data-* attribute.
Example
<!DOCTYPE html> <html> <head> <style> dl { margin: 2%; } p { width: 60%; background-color: lightgreen; padding: 2%; color: white; text-align: center; } dt { font-weight: bold; } dt:hover { cursor: pointer; } dd { font-style: italic; } </style> </head> <body> <dl> <dt onmouseover="showDescription(this)" data-food-type="beverages">Tea</dt> <dd>Hot Spicy Tea or Ice Lemon Tea </dd> <dt onmouseover="showDescription(this)" data-food-type="snacks">Toast</dt> <dd>Hot Garlic Butter Toast</dd> </dl> <p>(hover over food item)</p> </body> <script> function showDescription(food) { let foodType = food.getAttribute("data-food-type"); document.querySelector('p').textContent = ("We have " + food.innerHTML + " in " + foodType + "."); } </script> </html>
Output
This will produce the following result −
Example
<!DOCTYPE html> <html> <head> <style> section { margin: 8%; box-shadow: inset 0 0 20px red; width: 300px; padding: 2%; } section[data-number='77'] { height: 120px; border-radius: 15px; } section::before { content: attr(data-user); font-size: 1.2em; } section::after { content: attr(data-number); } </style> </head> <body> <section data-number="77" data-user="Client"> <p>Demo Text</p> </section> </body> </html>
Output
This will produce the following result −
- Related Articles
- HTML data-* Attributes
- Attributes of Data Warehouse
- Attributes and its types in Data Analytics
- How to use jQuery selectors on custom data attributes using HTML5?
- Escaping/encoding single quotes in JSON encoded HTML5 data attributes
- what are the different attributes of MySQL ENUM data type?
- How do we embed custom data attributes on all HTML elements?
- Store and retrieve arrays into and from HTML5 data attributes with jQuery?
- How to define multiple CSS attributes in jQuery?
- Data Conversion Using valueOf() In Java.
- Base64 Data Encodings using Python
- Data analysis using Python Pandas
- Python - Data visualization using Bokeh
- Olympics Data Analysis Using Python
- Rectangle Data in Data Structure

Advertisements