class


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

尽管对 class 的命名没有要求,但 web 开发者最好使用可以表达元素语义目的的名称,而不是描述元素展现的名称(即使一个元素是斜体,但是 class 的命名也不应该是 italics)。

语义化命名即使在页面展现发生改变时仍能合乎逻辑。


例子


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Simple styling of a simple document</title>
<style type="text/css">
html {background-color: white; }
.note {
font-style: italic;
font-weight: bold;
}
.editorial {
background: rgb(255, 0, 0, 0.25);
padding: 10px;
}
.editorial:before {
content: "Editor: ";
}
</style>
</head>
<body>
<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>
</body>
</html>
Published at:
May 17, 2025
Keywords:
html
ui