CSS3选择器

目录

属性选择器

E[att^=value]属性选择器

E[att$=value]属性选择器

E[stt*=value]属性值选择器

关系选择器

子元素选择器

兄弟选择器(+ ~)

结构化伪类选择器

:root选择器

:not选择器

:only-child选择器

:first-child和:last-child选择器

:nth-child(n)和:nth-last-child(n)

:nth-of-type(n)和:nth-last-of-type(n)选择器

(用于匹配属于父元素的 特定类型 的第n个子元素和倒数第n个子元素)

:empty选择器

伪元素选择器

:before伪元素选择器

:after伪元素选择器


属性选择器

E[att^=value]属性选择器

选择名称为E的标签,且该标签定义了att属性,att属性值包含前缀为value的子字符串。

E可以省略,如果省略则表示可以匹配满足条件的任意标签。

例如:div[id^=section]表示匹配包含id属性,且id属性值是以“section”字符串开头的div元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>E[att^=value]属性选择器</title>
    <style type="text/css">
        p[id^="one"]{    /*只要p元素中的id属性值是以"one"开头就会被选中*/
            color:red;
            font-family:"微软雅黑";
            font-size:20px;
        }
    </style>
</head>
<body>
    <p id="one">看日出</p>
    <p id="one1">看日出</p>
    <p id="two">看日出</p>
</body>
</html>

E[att$=value]属性选择器

例如:div[id$=section]表示匹配包含id属性,且id属性值是以"section"字符串结尾的div元素。

E[stt*=value]属性值选择器

例如:div[id*=section]表示匹配包含id属性,且id属性值包含"section"字符串的div元素。

关系选择器

子元素选择器

用来选择某个元素的第一级子元素。

例如选择只作为h1标签子元素的strong标签,可以写为:h1>strong

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子元素选择器</title>
    <style type="text/css">
        h1>strong{
            color:red;
            font-size:20px;
            font-family:"微软雅黑";
        }
    </style>
</head>
<body>
    <h1>这个<strong>知识点</strong>很<strong>重要</strong></h1> /*strong标签为h1标签的子元素,代码中设置的样式只对该句有效*/
    <h1>首都<em><strong>人们</strong></em>欢迎您</h1> /*strong标签为h1标签的孙元素 (无效)*/
</body>
</html>

兄弟选择器(+ ~)

临近兄弟选择器

该选择器使用“+”来连接前后两个选择器,选择器中的两个元素有同一个父元素,而且第2个元素必须紧跟第1个元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>临近兄弟选择器</title>
    <style type="text/css">
        p+h2{
            color:red;
            font-size:20px;
            font-family:"微软雅黑";
        }
    </style>
</head>
<body>
    <h2>《赠汪伦》</h2>
    <p>李白乘舟将欲行</p>
    <h2>忽闻岸上踏歌声</h2> /*只有紧跟p元素的h2元素应用了代码中设定的样式*/
    <h2>桃花</h2>
</body>
</html>

普通兄弟选择器

使用"~"来连接前后两个选择器。选择器中的两个元素有同一个父亲,但第2个元素不必紧跟第1个元素。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>普通兄弟选择器</title>
    <style type="text/css">
        p~h2{                /*为p标签的所有兄弟元素定义文本样式*/
            color:red;
            font-size:20px;
            font-family:"微软雅黑";
        }
    </style>
</head>
<body>
    <p>李白乘舟将欲行</p>
    <h2>忽闻岸上踏歌声</h2> 
    <h2>桃花</h2>
</body>
</html>

结构化伪类选择器

:root选择器

用于匹配文档根标签,在HTML中,根标签始终是html。即使用“:root选择器”定义的样式对所有页面标签都生效。对于不需要该样式的标签,可以单独设置样式进行覆盖。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>:root选择器</title>
    <style type="text/css">
        :root{color:red;} /*将页面所有的文本设置为红色*/
        h2{color:blue;} /*为h2标签设置蓝色文本,以覆盖上行代码设置的红色文本样式*/
    </style>
</head>
<body>
    <h2>面朝大海</h2>
    <p>
        幸福的人
        面朝大海
    </p>
</body>
</html>

:not选择器

对某个结构标签使用样式,排除这个结构元素下面的子结构元素,让子结构元素不使用这个样式。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>:not选择器</title>
    <style type="text/css">
        body *:not(h3){ 
            color:orange; 
            font-size:20px; 
            font-family:"宋体"; 
        }
    </style>
  </head>
  <body>
    <h3>我</h3>
    <p>世界</p>
    <p>世界</p>
  </body>
</html>
/*body *:not(h3)选择器用于排除body结构中的子结构元素h3,使其不应用设置的文本样式。
  只有h3标签所定义的文字内容没有添加新的样式。
*/

:only-child选择器

用于匹配属于某父元素的唯一子元素,即如果某父元素仅有一个子元素,则使用该选择器去选择这个子元素。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>:only-child选择器</title>
    <style type="text/css">
        strong:only-child{ color:red; }
    </style>
  </head>
  <body>
    <p>
      <strong>宗师</strong>
      <strong>英雄</strong>
    </p>
    <p>
      <strong>高管</strong>
    </p>
  </body>
</html>
/*:only-child选择器"strong:only-child",用于选择作为p标签唯一子元素的strong标签,并设置其文本颜色为红色*/

:first-child和:last-child选择器

分别用于选择父元素中的第一个和最后一个子元素。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>:first-child和:last-child选择器</title>
    <style type="text/css">
        p:first-child{
            color:pink;
            font-size:16px;
            font-family:"宋体";
        }
        p:last-child{
            color:biue;
            font-size:16px;
            font-family:"微软雅黑";
        }
    </style>
  </head>
  <body>
    <p>第一</p>
    <p>第二</p>
    <p>第三</p>
  </body>
</html>
/*分别使用了选择器"p:first-child"和"p:last-child",用于选择父选择器的第一个子元素和最后一个子元素(本案例中父元素为body),然后为它们设置特殊的文本样式*/

:nth-child(n)和:nth-last-child(n)

可以选择某个父元素中第二个或倒数第二个子元素。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>:first-child和:last-child选择器</title>
    <style type="text/css">
      p:nth-child(2) {
        color: pink;
        font-size: 16px;
        font-family: "宋体";
      }
      p:nth-last-child(2) {
        color: blue;
        font-size: 26px;
        font-family: "微软雅黑";
      }
    </style>
  </head>
  <body>
    <p>第一</p>
    <p>第二</p>
    <p>第三</p>
    <p>第四</p>
  </body>
</html>

:nth-of-type(n)和:nth-last-of-type(n)选择器

(用于匹配属于父元素的 特定类型 的第n个子元素和倒数第n个子元素)

<!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(n)和:nth-last-of-type(n)选择器</title>
    <style type="text/css">
      h2:nth-of-type(odd) { /*将所有h2中奇数行的字体颜色设置为玫红色*/
        color: #f09;
      }
      h2:nth-of-type(even) { /*将所有h2中偶数行的字体颜色设置为绿色*/
        color: #12ff65;
      }
      p:nth-last-of-type(2) { /*将倒数第2个p标签的字体加粗显示*/
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <!-- 玫红色 -->
    <h2>网页设置</h2>

    <p>网站功能策划</p>

    <!-- 绿色 -->
    <h2>java</h2>

    <!-- 加粗显示 -->
    <p>面向对象的程序设计语言</p> 
    
    <!-- 玫红色 -->
    <h2>php</h2>

    <p>开源脚本语言</p>
  </body>
</html>

:empty选择器

<!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>:empty选择器</title>
    <style type="text/css">
      p {
        width: 150px;
        height: 30px;
      }
      :empty {
        background-color: #999;
      }
    </style>
  </head>
  <body>
    <p>第一</p>
    <p>第二</p>
    <p></p> /*没有内容的p元素被添加了灰色背景*/
    <p>第三</p>
  </body>
</html>

伪元素选择器

:before伪元素选择器

用于在被选元素的内容前面插入内容,必须配合content属性来指定要插入的具体内容。

<元素>:before
{
    content:文字/url();
}
<!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>:before选择器</title>
    <style type="text/css">
      p::before {    /*:before可以写为::before(伪元素的标准写法)*/
        content: "学习it";
        color: #c06;
        font-size: 20px;
        font-family: "微软雅黑";
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <p>专注于JAVA、PHP,网页设计和平面设计</p>
  </body>
</html>

:after伪元素选择器

在某个元素之后插入一些内容。

<!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>:after选择器</title>
    <style type="text/css">
      p::after {
        content: url(photo.png/买辣椒也用券_起风了_4.jpg);
      }
    </style>
  </head>
  <body>
    <p>起风了</p>
  </body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值