Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

Is it possible to select elements in CSS by their HTML5 data attributes (for example, data-role)?

1 Answer

0 votes
by (40.7k points)

If you want to use an attribute selector, then, do like this:

[data-role="page"] {

    /* Styles */

}

Note: There are many numbers of attribute selectors you can use for various scenarios that are all covered in the document I link to. Despite custom data attributes being a "new HTML5 feature". 

  • browsers will not have any problems with supporting non-standard attributes, so you should be able to filter them with attribute selectors; 
  • If you don't care about CSS validation either, as CSS doesn't bother about non-namespaced attribute names as long as they don't break the selector syntax.

Browse Categories

...