css-functions伪类选择器系列二

news2024/9/30 3:26:46

一张图浏览CSS Functions

概述

本文主要讲述CSS的部分伪类选择器第二篇,包括::nth-child:nth-last-child:nth-of-type:nth-last-of-type

:nth-child()

:nth-child伪类是根据父元素的子元素列表中的索引来选择元素。

语法

:nth-child是以一个参数nth来描述匹配兄弟元素列表中元素索引的模式。元素索引从1开始。

:nth-child(nth) {
  /** */
}

其中nth可以是关键字也可以是函数符号

  • 关键字odd表示奇数,如 1、3、5…;even表示偶数,如 2、4、6。

  • 函数符号

    • <An+B>A表示步长;B表示偏移量;n表示从 0 开始的整数,如 5n+1就是包含 1、6、11…

    • nth of <selector>:表示选择与<selector>选择器匹配的第nth个元素,这时的nth可以是<An+B>的模式或者非负整数

  • 非负整数<nth>也可以就是一个数值,如 1,3,6,8…

示例
  • 效果
    在这里插入图片描述

  • 代码如下:

<style>
  .first {
    .children.pre span:nth-child(-n + 3) {
      background: rgb(30, 120, 223);
    }

    .children.odd span:nth-child(odd) {
      background: red;
    }

    .children.odd span:nth-child(2n + 1) {
      border: 2px solid #000;
    }

    .children.even span:nth-child(even) {
      background: orange;
    }

    .children.even span:nth-child(2n) {
      border: 4px dotted green;
    }

    .children span:nth-child(-n + 3 of .im) {
      background-color: gold;
    }
  }
</style>
<div class="order">
  <h2><code>:nth-child</code></h2>
  <div class="content first">
    <div class="item">
      <div class="children">
        <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
        <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
      </div>
    </div>
    <div class="item">
      <h5>-n+3:前3个背景为蓝色</h5>
      <div class="children pre">
        <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
        <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
      </div>
    </div>
    <div class="item">
      <h5>odd:背景为红色;2n+1:边框加粗且颜色为黑色</h5>
      <div class="children odd">
        <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
        <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
      </div>
    </div>
    <div class="item">
      <h5>odd:背景为橘色;2n+1:边框改为虚线加粗且颜色为绿色</h5>
      <div class="children even">
        <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
        <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
      </div>
    </div>
    <div class="item">
      <h5>of &lt;selector /&gt; :子元素前3个`im`类的背景色改为金色</h5>
      <div class="children of">
        <span>1</span><span class="im">2</span><span>3</span><span>4</span
        ><span class="im">5</span> <span class="im">6</span><span>7</span
        ><span>8</span><span class="im">9</span><span>10</span>
      </div>
    </div>
  </div>
</div>

:nth-last-child()

:nth-last-child():nth-child的语法类似,不过:nth-last-child()是从最后倒着计数,和后者相反。

示例
  • 效果
    在这里插入图片描述

  • 代码如下:

<style>
  .last {
    .children.pre span:nth-last-child(-n + 3) {
      background: rgb(30, 120, 223);
    }

    .children.odd span:nth-last-child(odd) {
      background: red;
    }

    .children.odd span:nth-last-child(2n + 1) {
      border: 2px solid #000;
    }

    .children.even span:nth-last-child(even) {
      background: orange;
    }

    .children.even span:nth-last-child(2n) {
      border: 4px dotted green;
    }

    .children span:nth-last-child(-n + 3 of .im) {
      background-color: gold;
    }
  }
</style>
<h2><code>:nth-last-child</code></h2>
<div class="content last">
  <div class="item">
    <div class="children">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>-n+3:后3个背景为蓝色</h5>
    <div class="children pre">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>odd:背景为红色;2n+1:边框加粗且颜色为黑色</h5>
    <div class="children odd">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>odd:背景为橘色;2n+1:边框改为虚线且颜色为绿色</h5>
    <div class="children even">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>of &lt;selector /&gt; :子元素后3个`im`类的背景色改为金色</h5>
    <div class="children of">
      <span>1</span><span class="im">2</span><span>3</span><span>4</span
      ><span class="im">5</span> <span class="im">6</span><span>7</span
      ><span>8</span><span class="im">9</span><span>10</span>
    </div>
  </div>
</div>

:nth-of-type()

:nth-of-type()伪类是基于相同类型(标签名称)的兄弟元素中的位置来匹配元素。

语法
:nth-of-type(nth) {
  /** */
}

nth表示匹配元素的模式,同上差不多。可以是关键词oddeven,也可以是<An+B>,还可以是非负整数。

示例
  • 效果
    在这里插入图片描述

  • 代码如下:

<style>
  .first {
    .children.odd span.light:nth-of-type(odd) {
      color: red;
    }

    .children.even span.light:nth-of-type(even) {
      color: rgb(99, 32, 32);
      font-weight: bolder;
    }

    .children.blue span:nth-of-type(2n + 1) {
      color: rgb(14, 53, 224);
      font-weight: bolder;
    }
  }
</style>
<h2><code>:nth-of-type</code></h2>
<div class="content first">
  <div class="item">
    <div class="children">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>odd:颜色为红色</h5>
    <div class="children odd">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>even:颜色为棕色</h5>
    <div class="children even">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>

  <div class="item">
    <h5>2n+1:颜色为蓝色</h5>
    <div class="children blue">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>
</div>

:nth-last-of-type()

:nth-last-of-type():nth-of-type的语法类似,不过:nth-last-of-type()是从最后倒着计数,和后者相反。

示例
  • 效果
    在这里插入图片描述

  • 代码如下:

<style>
  .last {
    .children.odd span.light:nth-last-of-type(odd) {
      color: red;
    }

    .children.even span.light:nth-last-of-type(even) {
      color: rgb(99, 32, 32);
      font-weight: bolder;
    }

    .children.blue span:nth-last-of-type(2n + 1) {
      color: rgb(14, 53, 224);
      font-weight: bolder;
    }
  }
</style>
<h2><code>:nth-of-last-type</code></h2>
<div class="content last">
  <div class="item">
    <div class="children">
      <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span>
      <span>6</span><span>7</span><span>8</span><span>9</span><span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>odd:颜色为红色</h5>
    <div class="children odd">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>
  <div class="item">
    <h5>even:颜色为棕色</h5>
    <div class="children even">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>

  <div class="item">
    <h5>2n+1:颜色为蓝色</h5>
    <div class="children blue">
      <span>1</span>
      <span class="light">2</span>
      <span>3</span>
      <span class="dark">4</span>
      <span>5</span>
      <span class="light">6</span>
      <span class="light">7</span>
      <span>8</span>
      <span class="light">9</span>
      <span>10</span>
    </div>
  </div>
</div>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2166945.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

wireshark使用要点

目录 IP过滤 端口过滤 内容过滤 过滤udp 过滤tcp IP过滤 ip.src XXX.XXX.XXX.XXX 只显示消息源地址为XXX.XXX.XXX.XXX的信息 ip.dst XXX.XXX.XXX.XXX 只显示消息目的地址为XXX.XXX.XXX.XXX的信息 ip.addr XXX.XXX.XXX.XXX显示消息源地址为XXX.XXX.XXX.XXX&#xff0…

LInux操作系统安装Jenkins

1、什么是Jenkins Jenkins是一个开源软件项目&#xff0c;是基于Java开发的一种持续集成工具&#xff0c;用于监控持续重复的工作&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件项目可以进行持续集成。 2、Jenkins的作用 持续的软件版本发布/测试项目。 监控…

零基础入门ComfyUI(一)初识ComfyUI

前言 AIGC 中文本生成图片的两大阵营&#xff0c;Stable Diffusion 和 Midjourney 。Midjourney 于 22 年3 月面世&#xff0c;22 年 7 月份的公测 v3 版本火出圈&#xff0c;迅速成为讨论焦点。同年7月Stable Diffusion问世&#xff0c;解决了绘画的细节及效率问题&#xff0c…

数字游戏C++

题目: 题目就是让你输入一个长度为88的0101串&#xff0c;让你求出中间1有几个。 wo一看到这题&#xff0c;立马想到暴力求解&#xff1a;循环八遍&#xff0c;每次输入一个char&#xff0c;减掉四十八加一起&#xff0c;输出&#xff0c; 细细一想&#xff1a;诶&#xff0c;…

【数据结构】基数排序高位优先(MSDF)

基数排序常用写法是低位优先(LSD)&#xff0c;在网上有很多&#xff0c;还有一种写法是高位优先排序(MSDF) 高位优先资料比较少&#xff0c;而且老师布置了一个高位优先的题目&#xff0c;所以也尝试了高位优先的写法&#xff0c;下面来说说吧&#xff0c;程序可以实现功能&…

【Midjourney】如何使用Midjourney生成惊艳的艺术作品:从提示词到完美图像的全攻略

文章目录 一、Midjourney简介1.1 Midjourney的工作原理1.2 Midjourney的应用场景 二、如何使用Midjourney生成图像2.1 选择Midjourney生成工具2.2 提示词的构建2.2.1 避免简单描述2.2.2 使用详细描述 2.3 提示词示例2.4 使用GPT生成提示词 三、参数解释3.1 版本 (Version)3.2 宽…

NRF21540—低功耗蓝牙,蓝牙mesh、Thread和Zigbee和2.4 GHz私有协议范围扩展射频前端模块

nRF21540是一款射频前端模块(FEM)&#xff0c;可用于改善短距离无线产品的传输范围和连接鲁棒性。作为一款辅助性设备&#xff0c;nRF21540是一种“即插即用型”的无线传输范围扩展器&#xff0c;可与nRF52和nRF53系列的高级多协议无线SoC搭配使用&#xff0c;所需的外部器件数…

【MATLAB】TOA/TDOA测距定位,三维任意(>3)个锚节点,对一个未知点进行定位

文章目录 摘要引言理论基础TOA定位原理TDOA定位原理三维定位模型TOA方程TDOA方程算法实现完整代码运行结果摘要 随着无线通信和定位技术的快速发展,时间到达(TOA)和时间差到达(TDOA)定位方法在多种应用场景中得到了广泛应用。本文讨论了在三维空间中使用TOA/TDOA测距定位…

25 基于51单片机的温度电流电压检测系统(压力、电压、温度、电流、LCD1602)

目录 一、主要功能 二、硬件资源 三、程序编程 四、实现现象 一、主要功能 基于51单片机&#xff0c;通过DS18B20检测温度&#xff0c;滑动变阻器连接数模转换器模拟电流、电压&#xff0c;通过LCD1602显示&#xff0c;程序里设置温度阈值为40&#xff0c;电流阈值为60&am…

ModelScan:一款大模型序列化安全扫描工具

ModelScan是由AI初创公司ProtectAI提供的一个开源项目&#xff0c;旨在扫描机器学习模型以确定它们是否包含不安全的代码。它是首个支持多种模型格式的扫描工具&#xff0c;目前支持H5、Pickle和SavedModel格式。这个工具用于保护使用PyTorch、TensorFlow、Keras、Sklearn、XGB…

代码随想录 -- 回溯 -- 非递减子序列

491. 非递减子序列 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;重点是去重 收集结果&#xff1a;每次进入递归先判断path中的元素数量&#xff0c;如果大于1了&#xff0c;就将path收集到result中。 递归参数&#xff1a;nums&#xff0c;index&#xff0c;pa…

王炸!二合一商业落地系统搞钱教程(StableDiffusion+Midjourney)AIGC零基础入门到商业实战教程!

刚接触Ai绘画时&#xff0c;身边就有大佬告诉我&#xff0c;这是个随随便便能月入过W的副业&#xff01;当时我就立志必须拿下。 很多人还在问什么是Ai绘画&#xff1f; 简单来说就是通过应用Ai工具&#xff0c;譬如是MJ&#xff0c;SD等工具&#xff0c;轻松生产出ai图片内容…

使用tar包下载安装mysql

1.官网下载mysql MySQL :: Download MySQL Community Server (Archived Versions)https://downloads.mysql.com/archives/community/ 2.上传到服务器解压 解压tar包 tar -zxvf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz 将解压后的包改名为mysql&#xff0c;这样更加简易…

SpringCloud源码:客户端分析(一)- SpringBootApplication注解类加载流程

总结一句话 用EnableDiscoveryClient注解客户端-启动类&#xff0c;配合springbootapplication&#xff0c;完成两个步骤&#xff1a; 自动读取spring-factories文件的全限定类名内容通过selectImport对这些类进行初始化 背景 spring.factories作用 在maven依赖&#xff1a; sp…

学Python再学C++是走弯路?

随着编程教育的普及&#xff0c;越来越多的家长和学生开始选择学习编程语言。Python作为一种简洁易学、应用广泛的编程语言&#xff0c;成为许多编程初学者的首选。然而&#xff0c;随着学习的深入&#xff0c;很多人会考虑转向更复杂、更底层的语言&#xff0c;如C。这就引发了…

golang qq邮件发送验证码

验证码的使用场景 注册/登录&#xff1a;使用验证码可以有效减少垃圾账号注册和恶意登录&#xff1b;短信接口保护&#xff1a;高效减少防止短信接口被刷情况&#xff1b;提交/投票&#xff1a;有效减少恶意刷单、恶意提交、恶意投票等情况&#xff1b;密码找回&#xff1a;用…

JVS-Logic逻辑引擎:为外包项目需求变更提供80%的成本节约方案

在这个数字化时代&#xff0c;在当今数字化时代&#xff0c;业务逻辑在处理复杂性和多样性的应用程序和系统中都非常重要。逻辑引擎作为一种高效、灵活的工具&#xff0c;通过可视化编排和原子服务&#xff0c;实现了业务规则的自动化处理&#xff0c;极大地缩短了开发、部署和…

react crash course 2024(7) react router dom

安装 npm i react-router-dom 引入 import {Route,createBrowserRouter,createRoutesFromElements,RouterProvider} from react-router-dom 在app.jsx const router createBrowserRouter(createRoutesFromElements(<Route index element {<h1>My App</h1>…

客户文章|DAP-seq助力揭示GhSBI1调控棉花果枝节间伸长的分子机制

2024年7月26日&#xff0c;中国农业科学院棉花研究所张永山研究员团队在Plant Biotechnology Journal&#xff08;影响因子10.1&#xff09;杂志上发表了题为“GhSBI1, a CUP-SHAPED COTYLEDON 2 homologue, modulates branch internode elongation in cotton”的文章&#xff…

PMP与CMMI:两种管理方法的对比

PMP与CMMI&#xff1a;两种管理方法的对比 PMP&#xff1a;专注于项目管理CMMI&#xff1a;组织过程改进的框架总结&#xff1a;互补而非替代 在现代企业管理中&#xff0c;项目管理和组织能力成熟度模型集成&#xff08;CMMI&#xff09;是两个经常被提及的概念。虽然它们都是…