饥人谷技术博客

关于CSS选择器的一些知识

2017-05-14  本文已影响0人  李博洋li

1、class和id的使用场景分别是什么?

首先,一个HTML文件中class名字可以有多个相同的,但是id的名字是唯一的;其次,如果是写css样式,那么id选择器的优先级高于class选择器(就简单选择器来说,只单纯考虑单一样式书写,不包括复杂选择器情况下)。

2、CSS选择器常见的有几种?

1. 基础选择器
2. 组合选择器
3. 伪类选择器
4. 伪元素选择器
5. 属性选择器

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

优先级顺序
  1. 在属性后使用!important会使此属性拥有最高级别的优先级,换句话说,可以认为这个属性会覆盖其他所有影响此样式的重写样式。
  2. 写在html文件中的,某个元素的style属性,即为行内样式。
  3. id选择器
  4. class选择器
  5. 伪类选择器
  6. 属性选择器
    7.标签选择器
    8.通配符选择器
  7. 浏览器自定义

除此之外,有一个需要注意的:
针对同一个元素的样式,如果写多遍,那么写在后面的会覆盖前面的,比如:

div{ color: #f00; }
div{ color: #blue; }

那么这时,div的color属性值其实是蓝色的。

复杂场景的计算

我们可以定义一个优先级的规则:

.discount-compile .dc-cont>label input[type="checkbox"]:checked+span{position:absolute; top:.3rem; z-index:9; background:url(../img/radio.png) no-repeat center center; width:.8rem; height:.8rem; background-size: cover; left:.5rem;}

这里面有两个标签选择器(label,span),两个类选择器(.discount-compile ,.dc-cont),一个属性选择器(input[type="checkbox"]),一个伪类选择器(:checked)
可以得到:a=0;b=0;c=4;d=2

#discount-compile input[type="checkbox"]:checked+span{position:absolute; top:.3rem; z-index:9; background:url(../img/radio.png) no-repeat center center; width:.8rem; height:.8rem; background-size: cover; left:.5rem;}

这里面有一个id选择器(#discount-compile),一个标签选择器(span),一个属性选择器(input[type="checkbox"]),一个伪类选择器(:checked)
可以得到:a=0;b=1;c=2;d=1

看得时候abcd是层层递进的,a的数字一样就比较b的数字,以此类推。
那么,第二种的优先级比第一种高,

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

这四个的顺序分别是:
a:link
a:visited
a:hover
a:active
原因是如果顺序错乱会导致某些样式不能够显示,例如将a:visited写在第一位,那么a:link就不再起作用,无论这个链接是否被访问过;例如将a:visited写在第三位,那么a:link和a:hover都不再起作用,因为a:visited已经将这两个样式都覆盖;例如将a:visited写在第四位,那么a:link和a:hover和a:active都不在起作用,因为a:visited已经将所有样式都覆盖。这个的顺序决定了页面上的a标签的展现行为,不能出错。

5、以下选择器分别是什么意思?

#header{} —— 匹配id值为header的元素
.header{} —— 匹配class值为header的元素
.header .logo{} —— 匹配class为header的元素下所有的class值为logo的子元素
.header.mobile{} —— 匹配class值中既含有header又含有mobile的元素
.header p, .header h3{} —— 同时匹配class值为header的标签为p的子元素和header下的标签是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中所有的标签为input中的type属性是text的元素

6、写出你知道的伪类选择器

7、div:first-child、div:first-of-type、div :first-child和div :first-of-type的作用和区别

<style>
    div :nth-of-type(2){background:#f00;}
</style>
<div>
    <p>我是第一个p</p>
    <article>我是第一个article</article>
    <h5>我是第一个h5</h5>
    <p>我是第二个p</p>
    <article>我是第二个article</article>
    <p>我是第三个p</p>
    <article>我是第三个article</article>
    <h5>我是第二个h5</h5>
</div>

结果是“我是第二个p”,“我是第二个article”,“我是第二个h5”的背景颜色都会变成红色。

8、运行以下代码并解释输出样式的原因

<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>

运行结果如图:

微信图片_20170515132228.png
我们来分析一下:
首先看一下style中的内容:.item1:first-child{color: red;}意思是class为item1的元素作为第一个子元素(此处不管是什么类型的标签,浏览器不做判断,就是我们能看到的第一个),我的颜色是红色,得到第一行的aa字体颜色是红色;第二句.item1:first-of-type{background: blue;}意思是class为item1的元素作为子元素的情况,我们可以看到,div中包着三个标签,一个p标签,两个h3标签。那么.item1:first-of-type事实上取的是每一个不同标签的第一个元素,即第一个p标签和第一个h3标签。所以,aa和bb的背景色都变为蓝色,黑色的字是浏览器默认给的颜色。
上一篇 下一篇

猜你喜欢

热点阅读