CSS选择器

2017-06-10  本文已影响0人  QQQQQCY

1. class 和 id 的使用场景

2. CSS选择器常见的有几种

1.基础选择器

选择器 含义
* 通用元素选择器,匹配页面任何元素
#id id选择器,匹配特定id的元素
.class 类选择器,匹配class包含(不是等于)特定类的元素
element 标签选择器

2.组合选择器

选择器 含义
E,F 多元素选择器,用 , 分隔,同时匹配元素E和F
E F 后代选择器,用空格分隔,匹配元素E所有的后代(不只是子元素、子元素向下递归)元素F
E>F 子多元素选择器,用 >分隔,匹配E元素的所有直接子元素
E+F 直接相邻选择器(弟弟选择器),匹配E元素之后的同级元素F
E~F 直接相邻选择器(弟弟选择器),匹配E元素之后的同级元素F(无论直接相邻与否)
.class1.class2 id和class选择器连写的时候中间没有分隔符,.和#本身充当分隔的元素

3.属性选择器

选择器 含义
E[attr] 匹配所有具有属性attr的元素,div[id]就能取到所有有id属性的div
E[attr = value] 匹配属性attr值为value的元素,div[id = test],匹配id = test的div
E[attr ~= value] 匹配所有属性attr具有多个空格分隔、其中一个值等于value的元素
E[attr ^= value] 匹配属性attr的值以value开头的元素
E[attr $= value] 匹配属性attr的值以value结尾的元素
E[attr *= value] 匹配属性attr的值包含value的元素

4.伪类选择器

选择器 含义
E:first-child 匹配父元素的第一个子元素
E:lastt-child 匹配父元素的最后一个子元素
E:link 匹配所有未被点击的链接
E:visited 匹配已被点击的链接
E:active 匹配鼠标已经按下但还未释放的E元素
E:hover 匹配鼠标悬停其上的E元素
E:focus 匹配活的当前焦点的E元素
E:lang(c) 匹配lang属性等于c的元素
E:enabled 匹配表单中可以用的元素
E:disabled 匹配表单中禁用的元素
E:checked 匹配表单中被选中的radio或checkbox元素
E:selection 匹配用户当前选中的元素
E:root 匹配文档的根元素,对于HTML文档,就是HTML元素
E:nth-child(n) 匹配其父元素的第n个子元素,第一个编号为1
E:nth-last-child(n) 匹配其父元素的倒数第n个元素,第一个编号为1
E:nth-of-type(n) 与:nth-child()作用类似,但是仅匹配使用同种标签的元素
E:nth-last-of-type(n) 与:nth-last-child() 作用类似,但是仅匹配使用同种标签的元素
E:only-child 匹配父元素下使用同种标签的唯一一个子元素
E:empty 匹配一个不包含任何子元素的元素,文本节点也被看作子元素
E:not(selector) 匹配不符合当前选择器的任何元素

5.伪元素选择器

选择器 含义
E:first-line 匹配E元素内容的第一行
E:first-letter 匹配E元素内容的第一个字母
E:before 在E元素之前插入生成的内容
E:after 在E元素之后插入生成的内容

3. 选择器的优先级是怎样的?对于复杂场景如何计算优先级?

优先级从高到低依次为

  1. 在属性后加上!important会覆盖页面内任何位置定义的元素样式
  2. 作为style属性写在元素上的内联样式
  3. id选择器
  4. 类选择器
  5. 伪类选择器
  6. 属性选择器
  7. 标签选择器
  8. 通配符选择器
  9. 浏览器自定义

对于复杂的场景,把选择器分为如下四种情况

选择器 等级
行内样式 <div style="xxx"></div> a
ID 选择器 b
类,属性选择器和伪类选择器 c
标签选择器、伪元素 d

然后统计每个选择器合集的等级分布
再按照abcd依次比较,先比a,如果数量相同,再比b,依次推导,如果完全相同,顺序在后面的优先

5. 练习题

(1). 以下选择器分别是什么意思?

| 选择器 | 作用 |
|-| |
| #header{} | 选择id为header的元素 |
| .header{} | 选择class为header的元素 |
| .header .logo{} | 选择class为header的元素的所有子元素中,class为logo的元素|
| .header.mobile{} | 选择class同时为header、mobile 的元素|
|.header p, .header h3{}| 选择class为header的元素中所有的p元素和h3元素|
| #header .nav>li{}| 选择id为header的元素的所有nav元素的直接子元素li|
| #header a:hover{} | 选择id为header的元素中处于hover状态的子元素a |
| #header .logo~p{} | 选择id为header的元素中,class为logo的元素之后所有的同级的p元素|
| #header input[type="text"]{}| 选择id为header的元素中所有type="text"的input元素 |

(2). 列出你知道的伪类选择器?

:active
:any
:checked
:default
:dir()
:disabled
:empty
:enabled
:first
:first-child
:first-of-type
:fullscreen
:focus
:hover
:indeterminate
:in-range
:invalid
:lang()
:last-child
:last-of-type
:left
:link
:not()
:nth-child()
:nth-last-child()
:nth-last-of-type()
:nth-of-type()
:only-child
:only-of-type
:optional
:out-of-range
:read-only
:read-write
:required
:right
:root
:scope
:target
:valid
:visited

(3). div:first-child、div:first-of-type、div :first-child和div :first-of-type的作用和区别 (注意空格的作用)

作用:

|选择器|作用|
|-||
|div:first-child |选择满足既为div同时又是兄弟元素中第一个的元素|
|div:first-of-type |选择满足既为div同时又是同类兄弟元素中第一个的元素 |
|div :first-child |选择所有div元素中的第一个子元素 |
|div :first-of-type |选择所有div元素的子元素中每一类的第一个子元素 |

区别:

|选择器|作用|
|-||
|:first-child| 匹配的是某父元素的第一个子元素,可以说是结构上的第一个子元素。|
|first-of-type|匹配的是某父元素下相同类型子元素中的第一个,比如 p:first-of-type,就是指所有类型为p的子元素中的第一个。这里不再限制是第一个子元素了,只要是该类型元素的第一个就行了。|

(4). 运行如下代码,解析下输出样式的原因

<style>
.item1:first-child{
  color: red;
}
.item1:first-of-type{
  background: blue;
}
</style>
 <div class="ct">
   <p class="item1">aa</p>
   <h3 class="item1">bb</h3>
   <h3 class="item1">ccc</h3>
 </div>

.item1:first-child 选择的是class名为item1的同级元素中的第一个,所以只选中<p class="item1">aa</p>
.item1:first-of-type选中的是class名为item1的同级同类元素中的第一个,因此<p class="item1">aa</p><h3 class="item1">bb</h3>均被选中

上一篇 下一篇

猜你喜欢

热点阅读