CSS选择器

2017-05-11  本文已影响0人  姚小帅

id与class的使用场景

id选择器,匹配特定id的元素
类选择器,匹配class包含(不是等于)特定类的元素
id具有唯一性,每个id只能对应一个元素
class 选择器用于描述一组元素的样式,class 选择器有别于id选择器,class可以在多个元素中使用。

CSS选择器

选择器类型

基础选择器
组合选择器
属性选择器
伪类选择器
伪元素选择器

基础选择器

|选择器| 含义|
|---------|:--------:|
|* |通用元素选择器,匹配页面任何元素(这也就决定了我们很少使用)|
|#id |id选择器,匹配特定id的元素|
|.class |类选择器,匹配class包含(不是等于)特定类的元素|
|element |标签选择器|

* {
    font-family: '微软雅黑';
}
#id-selector{
    color: #333;
}
.class-selector{
    background: #f1f1f1;
}
p {
    height: 50px;
    line-height: 50px;
}
组合选择器

最后两种情况会省略‘,’

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

多元素选择器

p,p1{
    background-color:yellow;
}

后代选择器

div p
{
  background-color:yellow;
}

子元素选择器

div>p
{
  background-color:yellow;
}

直接相邻选择器

div+p
{
  background-color:yellow;
}

普通相邻选择器

div~p
{
  background-color:yellow;
}
属性选择器
选择器 含义
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的元素

<a id="weilie1"></br></a>

伪类选择器
选择器 含义
E:first-child 匹配元素E的第一个子元素
E:link 匹配所有未被点击的链接
E:visited 匹配所有已被点击的链接
E:active 匹配鼠标已经其上按下、还没有释放的E元素
E:hover 匹配鼠标悬停其上的E元素
E:focus 匹配获得当前焦点的E元素

<a id="weilie2"></br></a>

其他不常见的

选择器 含义
E:lang(c) 匹配lang属性等于c的E元素
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:last-child 匹配父元素的最后一个子元素,等同于:nth-last-child(1)
E:first-of-type 匹配父元素下使用同种标签的第一个子元素,等同于:nth-of-type(1)
E:last-of-type 匹配父元素下使用同种标签的最后一个子元素,等同于:nth-last-of-type(1)
E:only-child 匹配父元素下仅有的一个子元素,等同于:first-child:last-child或 :nth-child(1):nth-last-child(1)
E:only-of-type 匹配父元素下使用同种标签的唯一一个子元素,等同于:first-of-type:last-of-type或 :nth-of-type(1):nth-last-of-type(1)
E:empty 匹配一个不包含任何子元素的元素,文本节点也被看作子元素
E:not(selector) 匹配不符合当前选择器的任何元素

#######n的取值

E:nth-child(n)
E:nth-last-child(n)
E:nth-of-type(n)
E:nth-last-of-type(n)

1,2,3,4,5
2n+1, 2n, 4n-1
odd, even

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

选择器优先级,与复杂场景优先级的运算

优先级:多条CSS规则作用到同一个元素上,改元素选择优先级最高的使用

  1. !important:在属性后面使用 !important 会覆盖页面内任何位置定义的元素样式

复杂场景优先级的运算

每个选择器本身有优先级,越具体优先级越高
同优先级划分为a b c d 依次统计a,b,c,d的个数,并进行比较

下面的选择器优先级依次变大

*             {}  /* a=0 b=0 c=0 d=0 -> 0,0,0,0 */
p             {}  /* a=0 b=0 c=0 d=1 -> 0,0,0,1 */
a:hover       {}  /* a=0 b=0 c=0 d=2 -> 0,0,0,2 */
ul li         {}  /* a=0 b=0 c=0 d=2 -> 0,0,0,2 */
ul ol+li      {}  /* a=0 b=0 c=0 d=3 -> 0,0,0,3 */
h1+input[type=hidden]{}  /* a=0 b=0 c=1 d=2 -> 0,0,1,1 */
ul ol li.active   {}  /* a=0 b=0 c=1 d=3 -> 0,0,1,3 */
div#header:after  {}  /* a=0 b=1 c=0 d=2 -> 0,1,0,2 */
#ct .box p        {}  /* a=0 b=1 c=1 d=1 -> 0,1,1,1 */
style=""          /* a=1 b=0 c=0 d=0 -> 1,0,0,0 */

a:link, a:hover, a:active, a:visited 的顺序是怎样的? 为什么?

正确地顺序是

a:link
a:visited
a:hover
a:active

这几个属性会相互覆盖,
link和visited是常驻属性所以要放在hover和active的前面
active是hover状态的一部分为了防止active被hover覆盖active需要再hover之后
visited属性只有active过才会永久激活,所以不用担心visited覆盖link
因此正确地顺序是a:link, a:visited, a:hover, a:active

以下选择器的意思

#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的元素所有后代中找到class为.nav的元素,然后匹配其子元素为li的元素*/
}
#header a:hover{  /* id为header的元素的后代元素 a 的鼠标悬停状态 */
}
#header .logo~p{   /* id为header的元素的后代元素中寻找class包含logo的元素,然后找到与其同一个父节点的P元素(弟弟节点,只有在其后面的才会匹配到) */
}
#header input[type="text"]{    /* id为header的元素的后代元素中寻找type值为“text”的input元素*/
}

列出你知道的伪类选择器

详情见常见的伪类选择器,其他伪类选择器

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

  • div:first-child 所有节点第一个子元素为div时此div的颜色

运行以下代码并说出结果和原因

<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的元素并且是父节点第一个子节点 即aa 显示为红色
.item1:first-of-type 匹配class为item1的元素并且是父节点的第一个同类型子节点  aa属于第一个p节点 bb 属于第一个h3节点故其背景为蓝色(h3和h4不属于同类型)
上一篇下一篇

猜你喜欢

热点阅读