CSS3 伪类选择器

138 阅读1分钟
  • 伪类选择器

CSS 伪类(Pseudo-classes)

  • 常用伪类
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
  /*
    所有伪类都是以冒号(:)开头
    *:                // 通配
    :hover            // 鼠标经过时的状态
    :link             // 正常状态
    :active           // 点击时的状态
    :visited          // 点击之后的状态
  */
  .box {
    width: 100px;
    height: 100px;
    border: 1px solid red;
  }
  .box:hover {
    border-radius: 50%;
    background-color: yellow;
  }
  </style>
</head>
  <body>
    <div class="box"></div>
  </body>
</html>
  • 结构伪类
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>

  * {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  ul {
    width: 520px;
    height: auto;
    margin: 100px auto;
  }

  li {
    float: left;
    width: 70px;
    height: 70px;
    border: 1px solid #ccc;
    text-align: center;
    line-height: 70px;

    margin-left: -1px;
    margin-top: -1px;
  }

  /* 以父盒子为基准,每个父盒子里面的第一个指定标签 */
  li:first-child {
    background-color: pink;
  }
  /* 以父盒子为基准,每个父盒子里面的最后一个指定标签 */
  li:last-child {
    background-color: pink;
  }
  /* 以父盒子为基准,每个父盒子里面的第5个指定标签,默认编号从1开始 */
  li:nth-child(5) {
    background-color: pink;
  }
  /* 以父盒子为基准,每个父盒子里面顺序为奇数的标签 odd:奇数 */
  li:nth-child(odd) {
    background-color: pink;
  }
  /* 以父盒子为基准,每个父盒子里面顺序为偶数的标签 even:偶数 */
  li:nth-child(even) {
    background-color: yellow;
  }
  /* 以父盒子为基准,每个父盒子里面所有的指定标签,n:0,1,2,3,4,5,6,7,8,..... */
  li:nth-child(n) {
    background-color: goldenrod;
  }
  /* 以父盒子为基准,每个父盒子里面顺序为偶数的标签 2n:偶数 */
  li:nth-child(2n) {
    background-color: purple;
  }
  /* 以父盒子为基准,每个父盒子里面顺序为偶数的标签 2n+1:奇数 */
  li:nth-child(2n+1) {
    background-color: red;
  }
  /* 以父盒子为基准,每个父盒子里面按顺序前5个标签 -n+5: 前5个,-n 相当于 0,-1,-2,-3... + 5 */
  li:nth-child(-n+5) {
    background-color: orange;
  }
  /* 以父盒子为基准,每个父盒子里面按顺序5的倍数标签 5n: 能整除5的顺序标签 */
  li:nth-child(5n) {
    background-color: royalblue;
  }
  /* 以父盒子为基准,每个父盒子里面按顺序后5个标签 -n+5: 前5个,-n 相当于 0,-1,-2,-3... + 5 */
  li:nth-last-child(-n+5) {
    background-color: orange;
  }
  </style>
</head>
<body>
  <!-- 运行这行生成 ul li 的代码 -->
  ul>li{$}*35
</body>
</html>
  • empty伪类
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    div {
      width: 100px;
      height: 100px;
      margin-top: 20px;
      margin-left: 20px;
      border: 1px solid green;
    }
    /* 如果div是空的就会被设置背景颜色 */
    div:empty {
      background-color: yellow;
    }
  </style>
</head>
<body>
    <div></div>
    <div>
      <span>dzm</span>
    </div>
</body>
</html>
  • target伪类
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    /* :target 伪类 要配合锚点使用,表示被激活的状态 */
    #box:target {
      background-color: red;
    }
  </style>
</head>
<body>
  <a href="#box">锚点定位标题</a>
  <div id="box">标题</div>
</body>
</html>