css3学习笔记002--选择器语法

摘录翻译自 https://drafts.csswg.org/selectors-3/

日期 2023-09-03

默认选中文档中所有符合条件的元素
所有选择器语法在 ASCII 范围内不区分大小写(即 [a-z] 和 [A-Z] 是等效的)

简单选择器包括类型选择器、通用选择器、属性选择器、类选择器、ID 选择器和伪类

通配选择器:

* 任意元素

原文 any element
例子:比如 *{margin:0; padding:0;}设置所有元素内外边距为0,之后再单独设置一些其他元素的内外边距。

类型选择器

E 任意E类型的元素

原文 an element of type E
例子:比如p 选中所有<p>xxx</p>

类选择器

E.warning class包含warning的E元素

原文 an E element whose class is “warning” (the document language specifies how class is determined).

id选择器

E#myid id包含myid的E元素

原文 an E element with ID equal to “myid”.

属性选择器

E[foo] 有’foo’属性的E元素

原文 an E element with a “foo” attribute
例子:<p id="xxx"></p> 可以用 p[id]选中

E[foo="bar"] ‘foo’属性精确的是‘bar’的E元素

原文 an E element whose “foo” attribute value is exactly equal to “bar”

E[foo~="bar"] ‘foo’属性的一个值是’bar’的E元素

原文 an E element whose “foo” attribute value is a list of whitespace-separated values, one of which is exactly equal to “bar”
例子:<p class='bg1 sz2'></p> 可以用 p[class=bg1选中

E[foo^="bar"] 'foo’属性的值以’bar’开头的E元素

原文 an E element whose “foo” attribute value begins exactly with the string “bar”
例子:<div class='nightMoon'></div><div class='nightStar'></div> 可以用 div[class^=night] 选中

E[foo$="bar"] 'foo’属性的值以’bar’结尾的E元素

原文 an E element whose “foo” attribute value ends exactly with the string “bar”

E[foo*="bar"] 'foo’属性的值包含’bar’的E元素

原文 an E element whose “foo” attribute value contains the substring “bar”

E[foo|="en"] 'foo’属性的值是’en’或者以’en-'开头的E元素

原文 an E element whose “foo” attribute has a hyphen-separated list of values beginning (from the left) with “en”

伪类

E:root 一个E元素,文档的‘根’

原文 an E element, root of the document
例子::root 表示文档的根元素

E:nth-child(n) E元素的第n个子元素

原文 an E element, the n-th child of its parent

E:nth-last-child(n) E元素的倒数第n个子元素

原文 an E element, the n-th child of its parent, counting from the last one

E:nth-of-type(n) 几个同级E元素中的第n个E元素

原文 an E element, the n-th sibling of its type

E:nth-last-of-type(n) 几个同级E元素中的倒数第n个E元素

原文 an E element, the n-th sibling of its type, counting from the last one

E:first-child 是其父元素的第一个子元素的E元素

原文 an E element, first child of its parent

E:last-child 是其父元素的最后一个子元素的E元素

原文 an E element, last child of its parent

E:first-of-type == E:nth-of-type(1)

原文 an E element, first sibling of its type

E:last-of-type == E:nth-last-of-type(1)

原文 an E element, last sibling of its type

E:only-child 是其父元素的唯一子元素的E元素 (独生子女)

原文 an E element, only child of its parent

E:only-of-type 没有同级E元素的E元素

原文 an E element, only sibling of its type

E:empty 空伪类,没有子元素的E元素

原文 an E element that has no children (including text nodes)

链接伪类

E:link 尚未访问的链接 E:visited 已经访问的链接

原文 an E element being the source anchor of a hyperlink of which the target is not yet visited (:link) or already visited (:visited)

用户操作伪类

E:active 活动的E元素 E:hover 鼠标指针在其上的E元素 E:focus (输入)聚焦的E元素

原文 an E element during certain user actions

目标伪类

E:target 作为‘引用 URI ’目标的 E 元素

原文 an E element being the target of the referring URI

语言伪类

E:lang(fr) 语言为fr或以fr-开头的E元素,语言由 <body lang=fr>这里确定

原文 an element of type E in language “fr” (the document language specifies how language is determined)

UI 元素状态伪类

E:enabled E:disabled 启用或禁用的用户界面元素E

原文 a user interface element E which is enabled or disabled

E:checked 比如选中的单选或复选框

原文 a user interface element E which is checked (for instance a radio-button or checkbox)

否定伪类

E:not(s) 与简单选择器s不匹配的E元素

原文 an E element that does not match simple selector s

伪元素

E::first-line E元素的第一行

原文 the first formatted line of an E element

E::first-letter E元素的首字母

原文 the first formatted letter of an E element

E::before 之前伪元素

原文 generated content before an E element.

E::after 之后伪元素

原文 generated content after an E element

组合器 (简单选择器 空格/+/>/~ 简单选择器)

空格 E F E元素的后代F元素

原文 an F element descendant of an E element
例子 h1 em<h1>This <span class="myclass">headline is <em>very</em> important</span></h1>
(后代不仅包括“儿子”及子元素,还包括“孙子”及子元素的子元素等等)

大于号U+003E E > F E元素的F子元素

原文 an F element child of an E element

加号U+002B E + F 紧挨在E元素后面的F元素

原文 an F element immediately preceded by an E element

波浪号U+007E E ~ F 一个前面有E元素的F元素

原文 an F element preceded by an E element

选择器组

以逗号(U+002C)分隔的选择器列表表示列表中的每个单独选择器选择的所有元素的并集
例子

h1 { font-family: sans-serif }
h2 { font-family: sans-serif }
h3 { font-family: sans-serif }
相当于:
h1, h2, h3 { font-family: sans-serif }

选择器例子

… …

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夕何夕2112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值