You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: files/en-us/web/html/how_to/use_data_attributes/index.md
+76-4Lines changed: 76 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -92,15 +92,87 @@ article[data-columns="4"] {
92
92
}
93
93
```
94
94
95
-
You can see all this working together [in this JS Bin example](https://jsbin.com/ujiday/2/edit).
95
+
Data values are strings. Number values must be quoted in the selector for the styling to take effect.
96
96
97
-
Data attributes can also be stored to contain information that is constantly changing, like scores in a game. Using the CSS selectors and JavaScript access here this allows you to build some nifty effects without having to write your own display routines. See [this screencast](https://www.youtube.com/watch?v=On_WyUB1gOk) for an example using generated content and CSS transitions ([JS Bin example](https://jsbin.com/atawaz/3/edit)).
97
+
## Examples
98
98
99
-
Data values are strings. Number values must be quoted in the selector for the styling to take effect.
99
+
### Style variants
100
+
101
+
Imagine you have a `callout` class. Now you want to implement different variants, such as "note" and "warning". Traditionally, people just use different class names.
102
+
103
+
```html
104
+
<divclass="callout callout--note">...</div>
105
+
<divclass="callout callout--warning">...</div>
106
+
```
107
+
108
+
```css
109
+
.callout {
110
+
margin: 0.5em0;
111
+
padding: 0.5em;
112
+
border-radius: 4px;
113
+
border-width: 2px;
114
+
border-style: solid;
115
+
}
116
+
117
+
.callout--note {
118
+
border-color: rgb(1515235);
119
+
background-color: rgb(1515235 / 0.2);
120
+
}
121
+
.callout--warning {
122
+
border-color: rgb(2351515);
123
+
background-color: rgb(2351515 / 0.2);
124
+
}
125
+
```
126
+
127
+
With data attributes, here's an alternative you can consider:
- It eliminates a lot of invalid states, such as applying `callout--note` without also adding `callout`, or applying multiple variants simultaneously.
164
+
- A separate `data-variant` attribute allows static analysis for valid values via linting or type checking.
165
+
- Toggling the variant is more intuitive: you can use `div.dataset.variant = "warning";` instead of manipulating the [`classList`](/en-US/docs/Web/API/Element/classList) which requires multiple steps.
166
+
167
+
### Associating arbitrary data with DOM elements
168
+
169
+
Many web apps have JavaScript data as the source-of-truth for their UI state. In these cases, you only add HTML attributes necessary for rendering. Data attributes are useful in the cases where everything is present in the markup, and JavaScript is only needed for handling events, syncing state, etc.
170
+
171
+
For example, in our [carousel with scroll margin](/en-US/docs/Web/API/IntersectionObserver/scrollMargin#carousel_with_scroll_margin) example, we have an HTML page already populated with many `<img>` elements. The image's source is initially stored in `data-src` to prevent any request being fired, and the real `src` is only added when the `<img>` scrolls into view. The data (image source) is colocated with the element, and the JavaScript is only responsible for defining behavior.
100
172
101
173
## Issues
102
174
103
-
Do not store content that should be visible and accessible in data attributes, because assistive technology may not access them. In addition, search crawlers may not index data attributes' values.
175
+
Do not store content that should be visible and accessible in data attributes, because assistive technology may not access them. In addition, search crawlers may not index data attributes' values. Often, if you only intend for the data attribute to be displayed, you can directly manipulate [`textContent`](/en-US/docs/Web/API/Node/textContent).
0 commit comments