CSS选择器


前言

CSS中,选择器用来指定网页上我们想要样式化的HTML元素。有 CSS 选择器提供了很多种方法,所以在选择要样式化的元素时,我们可以做到很精细的地步。本文详细介绍了css选择器的基础内容。


一、css选择器是什么?

要使用CSS对HTML页面中的元素实现一对一,一对多或者多对一的控制,这就需要用到CSS选择器。

每一条css样式定义由两部分组成,形式如下:<style>选择器{样式} </style> 在{}之前的部分就是“选择器”。 “选择器”指明了{}中的“样式”的作用对象,也就是“样式”作用于网页中的哪些元素

二、使用步骤

1.后代选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>后代选择器</title>
    <style>
        /*注释掉的两种写法也是相同效果*/
      /* .box em {
				color: red;
			} */
      .box ul li p em {
        color: blue;
      }
        /**/
      /* div ul li p em{
				color:aqua;
			} */
    </style>
  </head>
  <body>
    <div class="box">
      <ul>
        <li>
          <p class="spec">我是段落 <em>强调文字</em></p>
        </li>
      </ul>
    </div>
    <p>我是段落</p>
    <p>我是段落</p>
  </body>
</html>

2.交集选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>交集选择器</title>
    <style>
        /*p标签的顺序不可变,.damo和#pink可随意顺序*/
      p.damo#pink {
        color: pink;
      }
      p#red {
        color: red;
      }
    </style>
  </head>
  <body>
    <p>Hello World</p>
    <p class="damo" id="pink">这是pink色</p>
    <p class="damo">this is p3</p>
    <p id="red">这是红色</p>
  </body>
</html>

3.并集选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>并集选择器</title>
    <style>
        /*同时选中h1,p,div*/
      h1,
      p,
      div {
        color: orange;
      }
    </style>
  </head>
  <body>
    <h1>this is h1</h1>
    <div>this is div</div>
    <p>this is p</p>
  </body>
</html>

并集选择器是将需要选择的选择器用逗号隔开

4.通用兄弟选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>通用兄弟选择器</title>
    <style>
        /*选中h3之后的所有同级span,不是同级的无法选中*/
        h3~span{
            color:chartreuse;
            font-size:30px;
        }
    </style>
</head>
<body>
    <h3>this is h3</h3>
   <span>this is span1</span><br>
   <span>this is span2</span><br>
   <span>this is span3</span><br>
   <span>this is span4</span><br>
   <span>this is span5</span><br>
   <div><span>this is span6</span></div>
</body>
</html>

注意:选中的是h3之后的所有同级span,h3span是兄弟关系

5.相邻兄弟选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>相邻兄弟选择器</title>
    /*语法格式 a + b */
    <style>
        img+p{
            color:blue;
            font-size:30px;
        }
    </style>
</head>
<body>
    <div>
        <img src="" alt="">
        <p>this is p1</p>
        <p>thiis is p2</p>
    </div>   
</body>
</html>

注意:imgp同属一个父标签div.

选择的是紧跟img之后的一个标签.

6.子选择器

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>子选择器</title>
    <style>
      /* p是div的儿子 */
      div > p {
        color: red;
        font-size: 30px;
      }
      /* li是div的孙子 所以不会生效*/
      div > li {
        color: green;
        font-size: 30px;
      }
    </style>
  </head>
  <body>
    <div>
      <p>这段文字是红色</p>
      <p>这段也是红色</p>
      <ul>
        <li>这是绿色的吗?</li>
      </ul>
    </div>
  </body>
</html>

选择的是第一个元素的直接后代,孙子辈无法生效

7.:first-child

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>:first-child选择器</title>
    <style>
        /* 选择div的子元素p标签的第一个 */
        div p:first-child{
            font-size:large;
            color:green;
        }
    </style>
</head>
<body>
    <div>
        /*第一个子元素*/
        <p>this is p1</p>
        /*第二个子元素*/
        <p>this is p2</p>
        /*第三个子元素*/
        <p>this is p3</p>
        /*第四个子元素*/
        <p>this is p4</p>
        /*第五个子元素*/
        <p>this is p5</p>
    </div>
</body>
</html>

表示选择的是父元素的第一个子元素.

8.:last-child

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>:last-child选择器</title>
    <style>
        /* 选择div的子元素p标签的最后一个*/
        div p:last-child{
            font-size:large;
            color:green;
        }
    </style>
</head>
<body>
    <div>
         /*第一个子元素*/
        <p>this is p1</p>
        /*第二个子元素*/
        <p>this is p2</p>
        /*第三个子元素*/
        <p>this is p3</p>
        /*第四个子元素*/
        <p>this is p4</p>
        /*第五个子元素*/
        <p>this is p5</p>
    </div>
</body>
</html>

表示选择的是父元素的最后一个子元素.

9.:first-of-type

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>:first-of-type</title>
    <style>
      p:first-of-type {
        color: red;
        font-size: 60px;
      }
    </style>
  </head>
  <body>
    <div>
      <span>this is span</span>
      <p>this is p1</p>
      <p>this is p2</p>
      <p>this is p3</p>
      <p>this is p4</p>
      <p>this is p5</p>
      <p>this is p6</p>
    </div>
  </body>
</html>

选择当前同类标签的第一个

10.:last-of-type

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>:last-of-type</title>
    <style>
        /*选择最后一个p标签*/
      p:last-of-type {
        color: green;
        font-size: 60px;
      }
    </style>
  </head>
  <body>
    <div>
      <span>this is span 1</span>
      <p>this is p1</p>
      <p>this is p2</p>
      <p>this is p3</p>
      <p>this is p4</p>
      <p>this is p5</p>
      <span>this is span2</span>
      <p>this is p6</p>
      <span>this is span3</span>
    </div>
  </body>
</html>

选择当前同类标签的最后一个

11.:only-child

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>:only-child</title>
    <style>
      /* 选择的元素是其父级的唯一子元素 */
      p:only-child {
        font-size: 60px;
        color: green;
      }
    </style>
  </head>
  <body>
    <div>
      <p>被选中了吗</p>
    </div>
    <div>
        <p>this is p1</p>
        <p>this is p2</p>
        <p>this is p3</p>
        <p>this is p4</p>
        <p>this is p5</p>
        <p>this is p6</p>
    </div>
    
  </body>
</html>

父级元素除了当前元素p不可以再有任何其他的子元素

12.:only-of-type

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>only-of-type</title>
    <style>
      /* 选择的元素是同类标签中的唯一一个 */
      div p:only-of-type {
        color: red;
        font-size: 60px;
      }
    </style>
  </head>
  <body>
    <div>
      <span>this is span</span>
      <span>this is span</span>
      <span>this is span</span>
      <p>被选中了吗</p>
      <span>this is span</span>
    </div>
  </body>
</html>

父元素的同类子元素中唯一的一个元素

不可再有第二个p标签,否则无效过

13.:nth-child

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>:nth-child</title>
    <style>
      /* 可以选择任意序号的子元素 */
        /*当前选择的是div下的第6个子元素*/
      div :nth-child(6) {
        color: orange;
        font-size: 60px;
      }
        /* 选择奇数序号的p元素
        div :nth-child(2n+1) {
        color: orange;
        font-size: 60px;
      }*/
    </style>
  </head>
  <body>
    <div>
      	<!-- 1-->
      <p>this is p1</p>
        <!-- 2-->
      <span>this is span</span>
        <!-- 3-->
      <p>this is p2</p>
        <!-- 4-->
      <p>this is p3</p>
        <!-- 5-->
      <p>this is p4</p>
        <!-- 6-->
      <p>this is p5</p>
        <!-- 7-->
      <p>this is p6</p>
        <!-- 8-->
      <p>this is p7</p>
        <!-- 9-->
      <p>this is p8</p>
        <!-- 10-->
      <p>this is p9</p>
    </div>
  </body>
</html>

选择父元素下的任意序号的子元素.

14.:nth-of-type

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>:nth-of-type</title>
    <style>
        p:nth-of-type(3){
            color:red;
            font-size:30px;
        }
    </style>
</head>
<body>
    	<!--1-->
    <p>this is p1</p>
    <span>this span</span>
   		 <!--2-->
    <p>this is p2</p>
   		 <!--3-->
    <p>this is p3</p>
   		 <!--4-->
    <p>this is p4</p>
    	<!--5-->
    <p>this is p5</p>
</body>
</html>

同种标签的任意序号元素.

15.:nth-last-child

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>nth-last-child</title>
    <style>
        /* 倒数第二个p标签 */
        p:nth-last-child(2){
            color:blue;
            font-size:50px;
        }
    </style>
</head>
<body>
    <div>
    <p>this is p1</p>
    <p>this is p2</p>
    <p>this is p3</p>
    <p>this is p4</p>
    <p>this is p5</p>
</div>
</body>
</html>

需要注意的是选中的标签顺序不可被其他标签占用,否则无效果

举例代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>nth-last-child</title>
    <style>
        /* 倒数第二个p标签 */
        p:nth-last-child(2){
            color:blue;
            font-size:50px;
        }
    </style>
</head>
<body>
    <div>
    <p>this is p1</p>
    <p>this is p2</p>
    <p>this is p3</p>
    <p>this is p4</p>
    <span>选中的位置被span占用</span>
    <p>this is p5</p>
</div>
</body>
</html>

16.:nth-last-of-type

代码如下(示例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>nth-last-of-type</title>
    <style>
        /*倒数第二个p标签被选中*/
        p:nth-last-of-type(2){
            color:red;
            font-size:30px;
        }
    </style>
</head>
<body>
    <div>
        <p>this is p1</p>
        <p>this is p2</p>
        <p>this is p3</p>
        <!-- this is p4将会被选中 -->
        <p>this is p4</p>
        <span>this is span</span>
        <p>this is p5</p>
        <h3>this is h3</h3>
    </div>
</body>
</html>

同类型标签的倒序序号查找选择,不需要父元素


总结

以上就是今天要讲的内容,本文仅仅简单介绍了css选择器的使用语法规范。CSS 中,选择器由 CSS 选择器规范加以定义。就像是 CSS 的其他部分那样,它们需要浏览器的支持才能工作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Duck不必︎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值