Skip to content

Selector

一句话解释

CSS selector 是 模式 那个 matches one or 更多 HTML 元素 所以 你 能 apply 样式 到 它们.

用大白话说

A selector is the first part of a CSS rule — it tells the browser which elements the styles should apply to. Selectors range from simple (element type, class, ID) to complex (combinations, pseudo-classes, attribute matchers).

The most common selectors are the type selector (p matches all <p> elements), the class selector (.intro matches elements with 类="intro"), and the ID selector (#请求头 matches the element with id="请求头"). You can combine them: div.intro matches <div> elements that also have the class intro.

Selectors are also used in JavaScript with document.querySelector() to find elements in the DOM. Mastering selectors is essential for efficient CSS and DOM scripting.

示例

css
/* Type selector — all <p> elements */
p { color: gray; }

/* Class selector — elements with class="highlight" */
.highlight { background: yellow; }

/* ID selector — the element with id="header" */
#header { font-size: 24px; }

/* Descendant selector — <a> inside <nav> */
nav a { text-decoration: none; }

Four selector 类型: 通过元素类型, 通过类, 通过 ID, and 通过 descendant 关系.

相关术语

相关资源

相关课程

  • CSS

    Selectors 是基础的 CSS

相关工具

解读错误信息

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

查看错误

← 返回术语表