Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

类选择器

概述

在一个HTML文档中,CSS类选择器会根据元素的类属性中的内容匹配元素。类属性被定义为一个以空格分隔的列表项,在这组类名中,必须有一项与类选择器中的类名完全匹配,此条样式声明才会生效。

语法

.类名 {样式声明 }

注意它与下面的语句等价  attribute selector:

[class~=类名] {样式声明 }

示例

span.classy {
  background-color: DodgerBlue;
}

上面的CSS作用于下面的HTML代码:

  <span class="classy">Here's a span with some text.</span>
  <span>Here's another.</span>

则会产生这样的效果:

Here's a span with some text.

Here's another.

规范

Specification Status Comment
CSS Selectors Level 3 Recommendation  
CSS 2.1 Recommendation  
CSS 1 Recommendation  

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ? ?

See also

文档标签和贡献者

标签: 
 此页面的贡献者: fscholz, teoli, alimon
 最后编辑者: fscholz,