在CSS中按属性选择元素 [英] Select elements by attribute in CSS
问题描述
是否可以通过CSS5的HTML5数据属性(例如,data-role
)选择元素?
Is it possible to select elements in CSS by their HTML5 data attributes (for example, data-role
)?
推荐答案
如果您的意思是使用属性选择器,当然,为什么不呢?
If you mean using an attribute selector, sure, why not:
[data-role="page"] {
/* Styles */
}
有多种属性选择器可用于各种方案,这些都在我链接到的文档中进行了介绍.请注意,尽管自定义数据属性是新的HTML5功能",
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
您也不必担心CSS验证,因为CSS不关心非命名空间的属性名称,只要它们不破坏选择器语法即可.
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.
这篇关于在CSS中按属性选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!