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 分钟前
基本数据类型Symbol的基本应用场景
前端·javascript·面试
_小九28 分钟前
【开源】耗时数月、我开发了一款功能全面【30W行代码】的AI图床
前端·后端·开源
just小千41 分钟前
HTML进阶——常用标签及其属性
前端·html
惜.己42 分钟前
html笔记(一)
前端·笔记·html
Lsx-codeShare1 小时前
一文读懂 Uniapp 小程序登录流程
前端·javascript·小程序·uni-app
曹牧1 小时前
HTML实体名称
前端·html
小胖霞1 小时前
Node+Express+MySQL 后端生产环境部署,实现注册功能(三)
前端·后端
一 乐1 小时前
农产品电商|基于SprinBoot+vue的农产品电商系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·spring boot
云鹤_1 小时前
【Amis源码阅读】低代码如何实现交互?(上)
前端·低代码