饥人谷技术博客

1-7.CSS选择器

2017-04-19  本文已影响0人  guidetheorient

一、class 和 id 的使用场景

二、CSS常见选择器

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

这样可以方便地计算选择器的优先级,如:
div#id:after /0,1,0,2/
div.class /0,0,1,1/

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

按照如下顺序,因为样式层叠,以下选择器权重一样,所以依据a链接当前的状态应用不同的规则
1.a:link {color:blue;}
2.a:visited {color:blue;}
3.a:hover {color:red;}
4.a:active {color:yellow;}

五、以下选择器含义?

/*id为header元素*/
#header{
}
/*class为header元素*/
.header{
}
/*class为header元素后代中所有class为logo的元素*/
.header .logo{
}
/*class中同时含有header和mobile的元素*/
.header.mobile{
}
/*class为header元素后代中所有class为logo的元素*/
/*class为header元素后代中所有h3标签*/
.header p, .header h3{
}
/*id为header的元素后代所有class为nav元素中的直接子元素li*/
#header .nav>li{
}
/*id为header的元素后代所有a链接鼠标悬停其上时*/
#header a:hover{
}
/*id为header的元素后代所有class为logo的元素之后的所有同级p元素*/
#header .logo~p{
}
/*id为header的元素后代所有带有type="text"属性值的input标签*/
#header input[type="text"]{
}

六、列出你知道的伪类选择器

见问题二

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

div:first-child div:first-of-type

八、运行如下代码,解析下输出样式的原因。

原因:

<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>
运行输出
上一篇 下一篇

猜你喜欢

热点阅读