全局属性 class 的值是一个以空格分隔的元素的类名(classes )列表,它允许 CSS 和 Javascript 通过类选择器 (class selectors) 或DOM方法( document.getElementsByClassName
)来选择和访问特定的元素。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
尽管对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> . |
浏览器兼容性
BCD tables only load in the browser
另见
- 所有全局属性。