概述

全局属性 class 的值是一个以空格分隔的元素的类名(classes )列表,它允许 CSS 和 Javascript 通过类选择器 (class selectors) 或DOM方法( document.getElementsByClassName)来选择和访问特定的元素。

尽管对class 的命名没有要求,但 web 开发者最好使用可以表达元素语义目的的名称,而不是描述元素展现的名称(即使一个元素是斜体,但是 class 的命名也不应该是 italics)。语义化命名即使在页面展现发生改变时仍能合乎逻辑。

规范

具体条目 标准 备注
HTML Living Standard
class
Living Standard No change from latest snapshot, HTML 5.1
HTML 5.1
class
Recommendation Snapshot of HTML Living Standard, no change from HTML5
HTML5
class
Recommendation Snapshot of  HTML Living Standard. From HTML 4.01 Specification, class is now a true global attribute.
HTML 4.01 Specification
class
Recommendation Supported on all elements but <base>, <basefont>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 32IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 32Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support

注: 在Firefox 32及往后的版本中,class才成为真正的全局属性。

另见

文档标签和贡献者

标签: 
此页面的贡献者: eforegist, YehaiChen, ChuckZhang, Feihei
最后编辑者: eforegist,