Select elements by attribute in CSS

后端 未结 5 1737
情书的邮戳
情书的邮戳 2020-11-22 07:49

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

5条回答
  •  感情败类
    2020-11-22 08:00

    If you mean using an attribute selector, sure, why not:

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

    There are a variety of attribute selectors you can use for various scenarios which are all covered in the document I link to. Note that, despite custom data attributes being a "new HTML5 feature",

    • browsers typically don't have any problems supporting non-standard attributes, so you should be able to filter them with attribute selectors; and

    • you don't have to worry about CSS validation either, as CSS doesn't care about non-namespaced attribute names as long as they don't break the selector syntax.

提交回复
热议问题