Skip to content

CSS Selectors

Master CSS selectors — 元素, 类, ID, attribute, pseudo-类, and combinators.

你将学到

  • 所有 selector 类型: 元素, 类, ID, attribute, universal
  • Combinators: descendant, child, adjacent sibling, 通用 sibling
  • Pseudo-类如 :hover, :focus, :首先-child, :nth-child()
  • 如何 specificity determines 哪个规则 wins conflicts

概念讲解

CSS Selectors

Selectors are how CSS targets elements. A selector is a pattern; every element matching the pattern gets the styles in the declaration block. Mastering selectors is the single most important CSS skill — they determine what you can style and how precisely you can target it.

Basic Selectors

  • Element — h1 { } targets all <h1> tags.
  • Class — .card { } targets elements with 类="card". Classes are reusable.
  • ID — #请求头 { } targets the one element with id="请求头". IDs must be unique.
  • Universal — * { } targets every element. Useful for resets, but slow if overused.
  • Attribute — [类型="email"] { } targets elements with that attribute value.

Combinators

Combinators express relationships between elements:

  • Descendant — nav { } — any <> *anywhere inside* <nav>.
  • Child — nav > { } — an <> that is a *direct child* of <nav>.
  • Adjacent sibling — h2 + p { } — a <p> *immediately after* an <h2>.
  • General sibling — h2 ~ p { } — any <p> *after* an <h2> sharing the same parent.

Pseudo-classes

Pseudo-classes target elements in a specific *state*:

  • :hover — when the mouse is over the element.
  • :focus — when the element has keyboard focus.
  • :首先-child / :最后-child — the first or last element among its siblings.
  • :nth-child(n) — the nth element (e.g., nth-child(奇数) for zebra stripes).
  • :不(selector) — elements that do *not* match the inner selector.

Specificity

When multiple rules target the same element, specificity decides which wins. The ranking from highest to lowest: inline styles > ID > class/attribute/pseudo-class > element/pseudo-element. A rule with one ID beats a rule with ten classes. When specificity ties, the later rule in the source wins. Understanding specificity is the key to debugging "为什么 isn't my 样式 applying?"

示例代码

              /* Attribute selector: style all email inputs */
input[type="email"] {
  border: 2px solid #2563eb;
}

/* Child combinator: only direct <li> children of <ul> */
ul > li {
  list-style: none;
}

/* Pseudo-class: zebra-striping table rows */
tr:nth-child(odd) {
  background: #f9fafb;
}

/* :not() — style links that are NOT in the nav */
a:not(nav a) {
  color: #2563eb;
  text-decoration: underline;
}

/* Adjacent sibling: paragraph right after a heading */
h2 + p {
  margin-top: 0;
  color: #6b7280;
}
            

Attribute selectors 目标输入通过类型. child combinator avoids 样式 nested 列表. nth-child(奇数) 创建 zebra stripes. :不() 排除 nav 链接. adjacent sibling 移除顶部 margin 的 paragraph 那个 immediately follows heading.

动手试一试

  • SVG Optimizer

    SVGs 是样式使用 CSS selectors — optimize one and inspect selectors 内部.

  • 镜像颜色 Picker

    选取颜色到 use in 你的 selector-based CSS 规则.

常见错误

错误写法

Overusing ID selectors, 哪个有 very 高 specificity and 是困难到 override.

正确做法

Prefer 类 selectors for 样式. IDs 是 for 唯一 identification (anchors, 表单 labels), 不 for CSS. 类 keep specificity 低 and 样式 reusable.

错误写法

编写 deeply nested selectors 如 .nav .ul .li . 那个是 brittle and 慢.

正确做法

Keep selectors 短. 目标类 directly on 元素你 想要到 样式. BEM naming (block__element) helps keep selectors flat and 特定不使用 nesting.

相关代码片段

相关速查表

练习 CSS Selectors

8 个练习

练习 CSS Selectors

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

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

查看错误