类型选择器

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

CSS 的类型选择器通过节点名称匹配元素。换句话说,它选择文档中给定类型的所有元素。

css
/* All <a> elements. */
a {
  color: red;
}

当使用 @namespace 时,类型选择器可以包含命名空间。当处理包含多个命名空间的文档时,例如带有内联 SVG 或 MathML 的 HTML,或者混合使用多种词汇的 XML,这会很有用。

  • ns|h1 - 匹配命名空间 ns 中的 <h1> 元素
  • *|h1 - 匹配所有 <h1> 元素
  • |h1 - 匹配所有没有声明任何命名空间的 <h1> 元素

语法

css
element { style properties }

示例

CSS

css
span {
  background-color: skyblue;
}

HTML

html
<span>Here's a span with some text.</span>
<p>Here's a p with some text.</p>
<span>Here's a span with more text.</span>

结果

命名空间

在此示例中,选择器将只匹配示例命名空间中的 <h1> 元素。

css
@namespace example url("http://www.example.com/");
example|h1 {
  color: blue;
}

规范

规范
选择器 Level 4
# 类型选择器

浏览器兼容性

另见