如何设置 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
How to style CSS role
提问by laggingreflex
In the following HTML
在下面的 HTML
<div id="content" role="main">
the id can be accessed through #content
in CSS. How do I access role="main"
.
id 可以通过#content
CSS访问。我如何访问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
}
回答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 Chandra Sekhar Walajapet
follow this thread for more information
关注此线程以获取更多信息
CSS Attribute Selector: Apply class if custom attribute has value? Also, will it work in IE7+?
CSS 属性选择器:如果自定义属性有价值,则应用类?另外,它可以在 IE7+ 中工作吗?
and learn css attribute selector
并学习 css 属性选择器
回答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 */
}