饥人谷技术博客

CSS选择器

2016-07-19  本文已影响28人  Nicklzy

CSS选择器常见的有几种?

选择器 含义
* 通用元素选择器,匹配页面任何元素(这也就决定了我们很少使用)
#id id选择器,匹配特定id的元素
.class 类选择器,匹配class包含(不是等于)特定类的元素
element 标签选择器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
   *{
   font-weight:bold;
 }
#head{
  color:red;
}
.content{
  border:1px solid #000;
}
p{
  background: rgba(0,255,0,0.5);
}
</style>
</head>
<body>
内容均受到*选择器影响而加粗
<div id="head">我是头</div>
<div class="content">我是内容</div>
<p>我是段落</p>
</body>
</html>
效果.jpg
选择器 含义
E,F 多元素选择器,用,分隔,同时匹配元素E或元素F
E F 后代选择器,用空格分隔,匹配E元素所有的后代(不只是子元素、子元素向下递归)元素F
E>F 子元素选择器,用>分隔,匹配E元素的所有直接子元素
E+F 直接相邻选择器,匹配E元素之后的相邻的同级元素F
E~F 普通相邻选择器(弟弟选择器),匹配E元素之后的同级元素F(无论直接相邻与否)
.class1.class2 id和class选择器和选择器连写的时候中间没有分隔符,. 和 # 本身充当分隔符的元素

代码地址

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
 <style>
    .box{
  border:1px solid blue;
}
.box h1{
  border:1px solid red;
}
.box > h1{
  background-color:rgb(0,255,0)
}
.box + h1{
  font-size:12px;
}
.box ~ h1{
  font-family:微软雅黑;
}
.content h1{
  background-color:blue;
}
 </style>
</head>
<body>
  <h1>我是大哥</h1>
<div class="box">
  <h1>我也是大哥</h1>
  <div class="content">
    <h1>我是二哥</h1>
  </div>
  <h1>我是三哥</h1>
</div>
<h1>我是老小</h1>
<h1 class="second">我是老二小</h1>
<h1>我也是老小</h1>
</body>
</html>
效果 效果

由于状态是动态变化的,所以一个元素达到一个特定状态时,它可能得到一个伪类的样式;当状态改变时,它又会失去这个样式。由此可以看出,它的功能和class有些类似,但它是基于文档之外的抽象,所以叫伪类。

https://segmentfault.com/a/1190000000657084

选择器 含义
E:first-child 匹配元素E的第一个子元素
E:link 匹配所有未被点击的链接
E:visited 匹配所有已被点击的链接
E:active 匹配鼠标已经其上按下、还没有释放的E元素
E:hover 匹配鼠标悬停其上的E元素
E:focus 匹配获得当前焦点的E元素
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)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<style>
a:link{
  color: green;
}
a:visited{
  color: #ccc;
}
a:hover{
  color: red;
}
a:active{
  background-color:blue;
}
input[type="text"]:focus{
   background-color:yellow;
}
</style>
</head>
<body>
<a href="#">我就是一链接</a> 
<a href="#">我也是一链接</a> <br>
<form name="myform" method="post">
  姓名:<input type="text"><br>
  密码:<input type="text"<br>
</form>  
</body>
</html>
效果
选择器 含义
E::first-line 匹配E元素内容的第一行
E::first-letter 匹配E元素内容的第一个字母
E::before 在E元素之前插入生成的内容
E::after 在E元素之后插入生成的内容

学到后面再补充

选择器的优先级是怎样的?

从高到低分别是

  1. 在属性后面使用!important 会覆盖页面内任何位置定义的元素样式
  2. 作为style属性写在元素标签上的内联样式

class 和 id 的使用场景?

使用CSS选择器时为什么要划定适当的命名空间?

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

  #header{选择id="header''的元素 
}
.header{选择class="header''的元素 
}
.header .logo{ 选择“id=header”元素内的"id=logo"元素,包括logo内的全部子元素
}
.header.mobile{ 选择class中同时具有header和mobile的元素
}
.header p, .header h3{ 选择祖先为class="header"的p元素, 同时选择祖先为class="header"的h3元素 
}
#header .nav>li{选择祖先为id="header",class=""nav内的同级别的li元素
}
#header a:hover{选择id="header"所有后代元素a的鼠标悬停的效果
}

列出你知道的伪类选择器

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

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <style>
      h1:first-child{
  color:red;
}
h1:first-of-type{
  background:green;
}
h1:nth-child(2){
  font-family:微软雅黑;
}
h1:nth-of-type(2){
 font-size:16px;
}
  </style>
</head>
<body>
  <div class="box">
  <h1>我是大标题</h1>
  <h1>我也是大标题</h1>
  <div>
    <h1>我是小标题</h1>
    <h1>我也是小标题</h1>
  </div>
</div>
<h1>我是末尾</h1>
<h1>我是末尾</h1>
</body>
</html>

代码

效果

E:first-child为在E元素上的父元素内,寻找第一个元素是否为E
E:first-of-type为在E元素上的父元素内,寻找第一个为E的元素,不管第一个元素是否为E

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

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

蓝色背景:first-of-type,即在item1的父元素内寻找第一个item1元素,其中有P元素,以及H3元素。
红色文字:first-child,即在item1的父元素内寻找第一个元素,发现吻合item1。

text-align: center的作用是什么,作用在什么元素上?能让什么元素水平居中

QQ截图20160719171315.jpg

如果遇到一个属性想知道兼容性,在哪查看?


本文版权归饥人谷_Nick和饥人谷所有,转载请注明来源,谢谢

上一篇 下一篇

猜你喜欢

热点阅读