在线文档教程
CSS

类选择器 | Class selectors

Class selectors

CSS 类选择器根据其class属性的内容匹配元素。

/* All elements with class="spacious" */ .spacious { margin: 2em; } /* All <li> elements with class="spacious" */ li.spacious { margin: 2em; }

语法

.class_name { style properties }

请注意,这相当于以下内容attribute selector

[class~=class_name] { style properties }

实例

CSS

div.classy { background-color: skyblue; }

HTML

<div class="classy">This div has a special class on it!</div> <div>This is just a regular div.</div>

结果

规范

SpecificationStatusComment
Selectors Level 4The definition of 'class selectors' in that specification.Working DraftNo changes
Selectors Level 3The definition of 'class selectors' in that specification.Recommendation
CSS Level 2 (Revision 1)The definition of 'child selectors' in that specification.Recommendation
CSS Level 1The definition of 'child selectors' in that specification.RecommendationInitial definition

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support??(Yes)????