HTML class 属性
属性介绍
全局属性 class 的值是一个以空格分隔的元素的类名(classes )列表,它允许 CSS 和 Javascript 通过类选择器 (class selectors) 或DOM方法( document.getElementsByClassName)来选择和访问特定的元素。
尽管对class 的命名没有要求,但 web 开发者最好使用可以表达元素语义目的的名称,而不是描述元素展现的名称(即使一个元素是斜体,但是 class 的命名也不应该是 italics)。语义化命名即使在页面展现发生改变时仍能合乎逻辑。
演示
<p>Narrator: This is the beginning of the play.</p>
<p class="note editorial">Above point sounds a bit obvious. Remove/rewrite?</p>
<p>Narrator: I must warn you now folks that this beginning is very exciting.</p>
<p class="note">[Lights go up and wind blows; Caspian enters stage right]</p>
.output {
font: 1rem 'Fira Sans', sans-serif;
}
.note {
font-style: italic;
font-weight: bold;
}
.editorial {
background: rgb(255, 0, 0, .25);
padding: 10px;
}
.editorial:before {
content: 'Editor: ';
}
扩展
更多HTML全局属性,请参考 HTML全局属性