Using attribute selectors to select elements in CSS
P粉143640496
P粉143640496 2023-08-21 17:08:53
0
2
455
<p>In CSS, is it possible to select elements via the HTML5 data attribute (e.g., <code>data-role</code>)? </p>
P粉143640496
P粉143640496

reply all(2)
P粉148434742

It is also possible to select attributes regardless of their content in modern browsers.

Use the following code:

[data-my-attribute] {
   /* 样式 */
}

[anything] {
   /* 样式 */
}

For example: http://codepen.io/jasonm23/pen/fADnu

Applicable to most browsers.

Note that this can also be used within a JQuery selector, or using document.querySelector.

P粉543344381

If you mean using the attribute selector , of course you can:

[data-role="page"] {
    /* 样式 */
}

There are a variety of attribute selectors that can be used in different scenarios, and these are detailed in the documentation I provide. Please note that although custom data attributes are a "new HTML5 feature",

  • Browsers generally have no problem supporting non-standard attributes, so you should be able to filter using attribute selectors; and

  • You don't have to worry about CSS validation either, since CSS doesn't care about non-namespace attribute names as long as it doesn't break selector syntax.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!