如何设置CSS角色的样式

时间:2012-10-15 09:25:58

标签: css

在以下HTML

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

可以通过CSS中的#content访问ID。我如何访问role="main"

7 个答案:

答案 0 :(得分:148)

使用CSS属性选择器:

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

e.g:

div[role=main]

答案 1 :(得分:15)

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

答案 2 :(得分:13)

当然可以在这种模式下进行:

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

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

答案 3 :(得分:10)

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

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

之前的答案没有错,但他们依赖于您使用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>

答案 4 :(得分:4)

答案 5 :(得分:1)

请使用:

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

答案 6 :(得分:1)

我们可以使用

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