如何设置 CSS 角色的样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12892726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:30:44  来源:igfitidea点击:

How to style CSS role

css

提问by laggingreflex

In the following HTML

在下面的 HTML

<div id="content" role="main">

the id can be accessed through #contentin CSS. How do I access role="main".

id 可以通过#contentCSS访问。我如何访问role="main".

回答by sync

Use CSS attribute selectors:

使用 CSS 属性选择器:

https://developer.mozilla.org/en-US/docs/CSS/Attribute_selectors

https://developer.mozilla.org/en-US/docs/CSS/Attribute_selectors

e.g.:

例如:

div[role=main]

回答by pduersteler

Accessing it like this should work: #content[role="main"]

像这样访问它应该有效: #content[role="main"]

回答by Alessandro Minoccheri

Sure you can do in this mode:

当然你可以在这种模式下做:

 #content[role="main"]{
       //style
    }

http://www.w3.org/TR/selectors/#attribute-selectors

http://www.w3.org/TR/selectors/#attribute-selectors

回答by Patrick

The shortest way to write a selector that accesses that specific div is to simply use

编写访问该特定 div 的选择器的最短方法是简单地使用

[role=main] {
  /* CSS goes here */
}

The previous answers are not wrong, but they rely on you using either a div or using the specific id. With this selector, you'll be able to have all kinds of crazy markup and it would still work and you avoid problems with specificity.

以前的答案没有错,但它们依赖于您使用 div 或使用特定 id。使用这个选择器,您将能够拥有各种疯狂的标记,它仍然可以工作,并且您可以避免特定性问题。

[role=main] {
  background: rgba(48, 96, 144, 0.2);
}
div,
span {
  padding: 5px;
  margin: 5px;
  display: inline-block;
}
<div id="content" role="main">
  <span role="main">Hello</span>
</div>

回答by Saeed

please use :

请用 :

 #content[role=main]{
   your style here
}

回答by shrawan

we can use

我们可以用

 element[role="ourRole"] {
    requried style !important; /*for overriding the old css styles */
    }