CSS中常用的伪类选择器

一 、伪类(不存在的类,特殊的类)

-伪类用来描述一个元素的特殊状态

比如:第一个元素,被点击的元素,鼠标移入的元素···

-特点:一般请情况下,使用:开头

1、 :first-child 第一个子元素

2、 :last-child 最后一个子元素

3、 :nth-child() 选中第n个子元素

特殊值: n 所有的

2n或even 选中偶数

2n+1或odd 选中奇数

---以上这些伪类都是根据所有的子元素进行排序

1、:first-of-type

2、:last-of-type

3、:nth-of-type()

功能跟上面相似,

---不同的是,这是在同类型的子元素中去选择

二、:not() 否定伪类

-将符合条件的元素从选择器中去除

以下为代码演示

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>Document</title>
    <style>
      /*去掉项目符号*/
      ul {
        list-style: none;
      }
      /* 需求一:将ul里的第一个li自动一直设置为红色 */
      /* ul>li:first-child{color:red} */
      /* ul>li:last-child{color:blue} */
 
      /* ul>li:not(:nth-child(3)){color: green;} */
      /* ul>li:not(:nth-of-type(3)){color: green;} */
      /* li:nth-child(1) {
        color: pink;
      } */
      /* li:nth-of-type(1){
        background-color: red;
      } */
      li:not(:first-of-type){
        font-size: 50px;
      }
    </style>
  </head>
  <body>
    <h1>沁园春·雪</h1>
    <h3>毛泽东</h3>
    <ul>
      <span>测试</span>
      <p>北国风光,千里冰封,万里雪飘。</p>
      <li>望长城内外,惟余莽莽;大河上下,顿失滔滔。</li>
      <li>山舞银蛇,原驰蜡象,欲与天公试比高。</li>
      <li>须晴日,看红装素裹,分外妖娆。</li>
      <li>江山如此多娇,引无数英雄竞折腰。</li>
      <li>惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。</li>
      <li>一代天骄,成吉思汗,只识弯弓射大雕。</li>
    </ul>
  </body>
</html>
相关推荐
咕噜企业分发小米9 小时前
如何平衡服务器内存使用率和系统稳定性?
java·服务器·前端
前端无涯9 小时前
react组件(2)---State 与生命周期
前端·react.js
GoldenPlayer9 小时前
Web-Tech:CORS的触发机制
前端
AY呀9 小时前
Vite:现代前端构建工具的革命与实战指南
前端·vue.js·vite
爬山算法9 小时前
Netty(13)Netty中的事件和回调机制
java·前端·算法
前端无涯9 小时前
react组件(3)---组件间的通信
前端·react.js
讯方洋哥9 小时前
应用冷启动优化
前端·harmonyos
speedoooo9 小时前
未来的App不再需要菜单栏?
前端·ui·容器·小程序·web app
猿究院_xyz10 小时前
微信小程序与echarts联动安卓真机测试出现黑色阴影
前端·javascript·微信小程序·小程序·echarts
IT_陈寒10 小时前
Redis性能翻倍的5个冷门技巧,90%开发者都不知道的深度优化方案
前端·人工智能·后端