CSS常用属性之选择器

css选择器

序号 选择器 例子 例子描述
1 .class .intro 选择class="intro"的所有元素
2 #id #firstname 选择id="firstname"的元素
3 element p 选择所有<p>元素
4 element,element div,p 选择所有<div>元素和所有<p>元素
5 element element div p 选择<div>元素内部的所有<p>元素
6 element>element div>p 选择父元素为<div>元素的所有<p>元素
7 [attribute] [target] 选择带有target属性所有元素
8 [attribute=value] [target=_blank] 选择target="_blank"的所有元素
9 [attribute~=value] [title~=flower] 选择title属性包含单词"flower"的所有元素
10 [attribute|=value] [lang|=en] 选择lang属性值以"en"开头的所有元素
11 :link a:link 选择所有未被访问的链接
12 :visited a:visited 选择所有已被访问的链接
13 :active a:active 选择活动链接
14 :hover a:hover 选择鼠标指针位于其上的链接
15 :focus input:focus  选择获取焦点的input元素 
16 :first-letter p:first-letter 选择每个元素的首字母
17 :first-line p:first-line 选择每个<p>元素的首行
18 :before p:before 在每个<p>元素的内容之前插入内容
19 :after p:after 在每个<p>元素的内容之后插入内容
20 :lang(language) p:lang(it) 选择带有以"it"开头的lang属性值的每个<p>元素
21 element1~element2 p~ul 选择前面有<p>元素的每个<ul>元素
22 [attribute^=value] a[src^="https"] 选择其src属性值以"https"开头的每个<a>元素
23 [attribute$=value] a[src$=".pdf"] 选择其src属性以".pdf"结尾的所有<a>元素
24 :first-of-type p:first-of-type 选择属于父元素里面的首个<p>元素
25 :last-of-type p:last-of-type 选择属于父元素里面的最后一个<p>元素
26 :only-of-type p:only-of-type 选择父元素里面只有一个<p>元素
27 :only-child p:only-child 选择父元素只有一个子元素的<p> 元素
28 :nth-child(n) p:nth-child(2) 选择属于其父元素里面的第二个子元素<p>(索引从1开始)
29 :nth-last-child(n) p:nth-last-child(2) 同上,从最后一个元素开始计数
30 :nth-of-type(n) p:nth-of-type(2) 选择属于其父元素的二个<p>元素的每个<p>元素
31 :nth-last-of-type(n) p:nth-last-of-type(2) 同上,但是从最后一个元素计数
32 :last-child p:last-child 选择属于其父元素最后一个子元素每个<p>元素
33 :root :root 选择文档的根元素
34 :empty p:empty 选择没有子元素的每个<p>元素(包括文本节点)
35 :target #news:target 选择当前活动的元素
36 :enabled input:enabled 选择每个启用的<input>元素
37 :disabled input:disabled 选择每个被禁用的<input>元素
38 :checked input:checked 选择每个被选中的<input>的元素
39 :not(selector) :not(p) 选择非<p>元素的每个元素
40 ::selection ::selection 选择被用户选取的元素部分
       
原文地址:https://www.cnblogs.com/litings/p/8312910.html