Skip to content

Commit 73021b2

Browse files
Josh-Cenaestelle
andauthored
Internalize data attributes examples (#40405)
* Internalize data attributes examples * Update index.md Co-authored-by: Estelle Weyl <[email protected]> --------- Co-authored-by: Estelle Weyl <[email protected]>
1 parent e509776 commit 73021b2

File tree

1 file changed

+76
-4
lines changed
  • files/en-us/web/html/how_to/use_data_attributes

1 file changed

+76
-4
lines changed

files/en-us/web/html/how_to/use_data_attributes/index.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,87 @@ article[data-columns="4"] {
9292
}
9393
```
9494

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.
9696

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
9898

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+
<div class="callout callout--note">...</div>
105+
<div class="callout callout--warning">...</div>
106+
```
107+
108+
```css
109+
.callout {
110+
margin: 0.5em 0;
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(15 15 235);
119+
background-color: rgb(15 15 235 / 0.2);
120+
}
121+
.callout--warning {
122+
border-color: rgb(235 15 15);
123+
background-color: rgb(235 15 15 / 0.2);
124+
}
125+
```
126+
127+
With data attributes, here's an alternative you can consider:
128+
129+
```html live-sample___callout-data-attr
130+
<div class="callout">...</div>
131+
<div class="callout" data-variant="note">...</div>
132+
<div class="callout" data-variant="warning">...</div>
133+
```
134+
135+
```css live-sample___callout-data-attr
136+
.callout {
137+
margin: 0.5em 0;
138+
padding: 0.5em;
139+
border-radius: 4px;
140+
border-width: 2px;
141+
border-style: solid;
142+
}
143+
144+
/* Default style */
145+
.callout:not([data-variant]) {
146+
border-color: rgb(15 15 15);
147+
background-color: rgb(15 15 15 / 0.2);
148+
}
149+
.callout[data-variant="note"] {
150+
border-color: rgb(15 15 235);
151+
background-color: rgb(15 15 235 / 0.2);
152+
}
153+
.callout[data-variant="warning"] {
154+
border-color: rgb(235 15 15);
155+
background-color: rgb(235 15 15 / 0.2);
156+
}
157+
```
158+
159+
{{EmbedLiveSample("callout-data-attr", "", "200")}}
160+
161+
There are multiple benefits of this:
162+
163+
- 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.
100172

101173
## Issues
102174

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).
104176

105177
## See also
106178

0 commit comments

Comments
 (0)