Skip to content

类 (HTML)

一句话解释

类 attribute assigns label 到 HTML 元素所以它 能是 样式 or 选择 together 使用 others.

用大白话说

class attribute lets you group HTML elements under a shared name. You can then style all elements with that class using a single CSS rule, or select them all at once in JavaScript. This is the primary way to apply reusable styles across a page.

A class is written as 类="button" on the element, and targeted in CSS with a dot: .button { ... }. An element can have multiple classes separated by spaces: 类="button 主要大". This lets you combine styles modularly.

Unlike IDs, classes are not unique — many elements can share the same class. This makes classes the right choice for repeating components like buttons, cards, or list items.

示例

html
<button class="btn btn-primary">Save</button>
<button class="btn btn-secondary">Cancel</button>

<style>
  .btn { padding: 8px 16px; border-radius: 4px; }
  .btn-primary { background: #2563eb; color: white; }
  .btn-secondary { background: gray; color: white; }
</style>

两者都 buttons share btn 类 for 基础 样式, and 每个 有 其次 类 for 它的 variant.

常见混淆

  • 容易混淆的对象: id

    关键区别: 类 能 是 shared 通过 许多 元素 and 是 targeted 使用 .类 in CSS; id 必须 是 唯一 on page and 是 targeted 使用 #id. Use 类 for reusable 样式, ids for 单个 特定 元素.

相关术语

相关资源

相关课程

  • CSS

    类 是 大多数 常见 CSS targeting mechanism

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表