Select elements by attribute in CSS

后端 未结 5 1795
情书的邮戳
情书的邮戳 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 07:56

    It's also possible to select attributes regardless of their content, in modern browsers

    with:

    [data-my-attribute] {
       /* Styles */
    }
    
    [anything] {
       /* Styles */
    }
    

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

    Works on a very significant percentage of browsers.

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

提交回复
热议问题