简体   繁体   English

在 CSS 中按属性选择元素

[英]Select elements by attribute in CSS

是否可以通过 HTML5 数据属性(例如data-role )在 CSS 中选择元素?

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",请注意,尽管自定义数据属性是“新的 HTML5 功能”,

  • 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.您也不必担心 CSS 验证,因为 CSS 不关心非命名空间属性名称,只要它们不破坏选择器语法。

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例如: 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请注意,这也可以在 JQuery 选择器中使用,或使用document.querySelector

It's worth noting CSS3 substring attribute selectors值得注意的是 CSS3 子字符串属性选择器

[attribute^=value] { /* starts with selector */
/* Styles */
}

[attribute$=value] { /* ends with selector */
/* Styles */
}

[attribute*=value] { /* contains selector */
/* Styles */
}

You can combine multiple selectors and this is so cool knowing that you can select every attribute and attribute based on their value like href based on their values with CSS only..您可以组合多个选择器,这很酷,因为您可以根据它们的值选择每个属性和属性,例如基于 CSS 的值的href

Attributes selectors allows you play around some extra with id and class attributes属性选择器允许你玩一些额外的idclass属性

Here is an awesome read on Attribute Selectors这是关于属性选择器的精彩读物

Fiddle小提琴

 a[href="http://aamirshahzad.net"][title="Aamir"] { color: green; text-decoration: none; } a[id*="google"] { color: red; } a[class*="stack"] { color: yellow; }
 <a href="http://aamirshahzad.net" title="Aamir">Aamir</a> <br> <a href="http://google.com" id="google-link" title="Google">Google</a> <br> <a href="http://stackoverflow.com" class="stack-link" title="stack">stack</a>

Browser support:浏览器支持:
IE6+, Chrome, Firefox & Safari IE6+、Chrome、Firefox 和 Safari

You can check detail here .您可以在此处查看详细信息。

    [data-value] {
  /* Attribute exists */
}

[data-value="foo"] {
  /* Attribute has this exact value */
}

[data-value*="foo"] {
  /* Attribute value contains this value somewhere in it */
}

[data-value~="foo"] {
  /* Attribute has this value in a space-separated list somewhere */
}

[data-value^="foo"] {
  /* Attribute value starts with this */
}

[data-value|="foo"] {
  /* Attribute value starts with this in a dash-separated list */
}

[data-value$="foo"] {
  /* Attribute value ends with this */
}

CSS attribute selectors with small snippet and working examples 带有小片段的CSS属性选择器和有效示例

1 - [attribute~="value"] 1- [attribute〜=“ value”]

2 - [attribute^="value"] 2- [attribute ^ =“ value”]

3 - [attribute$="value"] 3- [attribute $ =“ value”]

4 - [attribute|="value"] 4- [attribute | =“ value”]

5 - [attribute*="value"] 5- [attribute * =“ value”]

 /* Seven (because it search in all words)*/ div[class="items"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

~ is used to select elements with an attribute value containing a specified word 〜用于选择具有包含指定单词的属性值的元素

 /* Five Six Seven (because it search in all words)*/ div[class~="items"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

^ is used to select elements whose attribute value begins with a specified value. ^用于选择属性值以指定值开头的元素。

 /* One Three Five Seven (because it search in beginning)*/ div[class^="items"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

$ selector is used to select elements whose attribute value ends with a specified value. $选择器用于选择属性值以指定值结尾的元素。

 /* Two Four Six Seven (Because it search from end) */ div[class$="items"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

| | is used to select elements with the specified attribute starting with the specified value. 用于选择具有指定属性(从指定值开始)的元素。

 /* One Seven (because it start from beggining and search -(hyphen)). ignore started from end */ div[class|="items"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

* is used to select elements whose attribute value contains a specified value. *用于选择属性值包含指定值的元素。

 /* One Two Three Four Five Six (because it search all group)*/ div[class*="group"] { color: red; } 
 <div class="items-group">One</div> <div class="groupitems">Two</div> <div class="itemsgroup">Three</div> <div class="group-items">Four</div> <div class="items group">Five</div> <div class="group items">Six</div> <div class="items">Seven</div> 

Is it possible to select elements in CSS by their HTML5 data attributes?是否可以通过 HTML5 数据属性选择 CSS 中的元素? This can easily be answered just by trying it, and the answer is, of course, yes .只需尝试一下就可以很容易地回答这个问题,答案当然是肯定的。 But this invariably leads us to the next question, ' Should we select elements in CSS by their HTML5 data attributes?'但这总是将我们引向下一个问题,“我们应该根据 HTML5 数据属性选择 CSS 中的元素吗?” There are conflicting opinions on this.对此有不同意见。

In the 'no' camp is (or at least was , back in 2014) CSS legend Harry Roberts.在“不”阵营中是(或至少,早在 2014 年)CSS 传奇人物 Harry Roberts。 In the article, Naming UI components in OOCSS , he wrote:在《 在 OOCSS 中命名 UI 组件》一文中,他写道:

It's important to note that although we can style HTML via its data-* attributes, we probably shouldn't.需要注意的是,尽管我们可以通过其 data-* 属性设置 HTML 样式,但我们可能不应该这样做。 data-* attributes are meant for holding data in markup, not for selecting on. data-* 属性用于在标记中保存数据,而不是用于选择。 This, from the HTML Living Standard (emphasis mine):这来自HTML 生活标准(强调我的):

"Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements ." “自定义数据属性旨在存储页面或应用程序私有的自定义数据,没有更合​​适的属性或元素。”

The W3C spec was frustratingly vague on this point, but based purely on what it did and didn't say, I think Harry's conclusion was perfectly reasonable. W3C 规范在这一点上含糊不清,令人沮丧,但纯粹基于它了什么和没说什么,我认为 Harry 的结论是完全合理的。

Since then, plenty of articles have suggested that it's perfectly appropriate to use custom data attributes as styling hooks, including MDN's guide, Using data attributes .从那时大量文章建议使用自定义数据属性作为样式挂钩非常合适,包括 MDN 的指南, 使用数据属性 There's even a CSS methodology called CUBE CSS which has adopted the data attribute hook as the preferred way of adding styles to component ' exceptions ' (known as modifiers in BEM ).甚至还有一种称为CUBE CSS的 CSS 方法,它采用数据属性挂钩作为向组件“异常”(在BEM中称为修饰符)添加样式的首选方式。

Thankfully, the WHATWG HTML Living Standard has since added a few more words and even some examples (emphasis mine):值得庆幸的是, WHATWG HTML 生活标准已经添加了更多的词,甚至一些示例(强调我的):

Custom data attributes are intended to store custom data, state, annotations, and similar, private to the page or application, for which there are no more appropriate attributes or elements. 自定义数据属性旨在存储页面或应用程序私有的自定义数据、状态、注释和类似内容,没有更合​​适的属性或元素。

In this example, custom data attributes are used to store the result of a feature detection for PaymentRequest, which could be used in CSS to style a checkout page differently .在此示例中,自定义数据属性用于存储 PaymentRequest 的功能检测结果,可用于 CSS 中以不同的方式设置结帐页面的样式

Authors should carefully design such extensions so that when the attributes are ignored and any associated CSS dropped, the page is still usable.作者应该仔细设计这样的扩展,以便在忽略属性并且删除任何关联的 CSS时,页面仍然可用。


TL;DR: Yes, it's okay to use data-* attributes in CSS selectors, provided the page is still usable without them. TL;DR:是的,可以在 CSS 选择器中使用data-*属性,只要页面在没有它们的情况下仍然可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM