导读:

CSS3 中有许多伪类选择器,其中一些伪类选择器的作用近似却又不完全一样,下面我们就来看一看到底有什么不一样。

1、:only-child:only-of-type

      测试的代码:
<div>
<p>something</p>
</div>
<div>
<div>other something</div>
<p>something</p>
</div>
  • :only-child 是指匹配属于父元素中唯一子元素的元素。设置 p:only-child { background-color:#00ff00; } 后表现为:

    something

    other something

    something

  • :only-of-type是指匹配指定元素的父元素中唯一相同类型的子元素(该父元素可以有很多子元素,而与指定匹配的元素属于同一种类型的子元素是唯一的,则为其设定样式)。设置 p:only-of-type { background-color:#00ff00; } 后表现为:

    something

    other something

    something

2、:first-child:first-of-type

      测试的代码:
<div>
<p>something</p>
<div>other something</div>
</div>
<div>
<div>other something</div>
<p>something</p>
</div>
  • :first-child 是指匹配的对象是其所在的父元素的第一个元素。设置 p:first-child { background-color:#00ff00; } 后表现为:

    something

    other something
    other something

    something

  • :first-of-type 是指匹配的对象是其所在的父素的第一个相同类型元素。设置 p:first-of-type { background-color:#00ff00; } 后表现为:

    something

    other something
    other something

    something

3、:last-child:last-of-type 可以参考 :first-child:first-of-type 的思路

4、:nth-child(n):nth-of-type(n)

      测试的代码:
<div>
<div>other something</div>
<p>something</p>
<p>something</p>
<p>something</p>
</div>
  • nth-child(n) 是指匹配的对象是其所在的父元素的第 n 个元素。设置 p:nth-child(n) { background-color:#00ff00; } 后表现为:

    • 当 n 为 1(数字) 时:没有被选中的元素。
    • 当 n 为 2(数字) 时:
      other something

      something

      something

      something

    • 当 n 为 n+3(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 -n+3(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 2n(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 2n+1(公式) 时:
      other something

      something

      something

      something

  • tips:上面中的 n 的取值可以是数字,一个关键字,一个公式(此时 n 是一个从0开始的计数器)。当 n 取值 -n+2 表示小于等于2;当 n 取值 n+2 表示大于等于2;当 n 取值 2n 表示取偶数,也可以用 even 代替;当 n 取值 2n-1 表示去奇数,也可以用 odd 代替。
  • nth-of-type(n) 是指匹配的对象是其所在的父素的第 n 个相同类型元素。设置 p:nth-of-type(n) { background-color:#00ff00; } 后表现为:
    • 当 n 为 1(数字) 时:

      other something

      something

      something

      something

    • 当 n 为 2(数字) 时:
      other something

      something

      something

      something

    • 当 n 为 n+3(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 -n+3(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 2n(公式) 时:
      other something

      something

      something

      something

    • 当 n 为 2n+1(公式) 时:
      other something

      something

      something

      something

5、:nth-last-child(n):nth-last-of-type(n) 可以参考 :nth-child(n):nth-of-type(n) 的思路

若是文中有什么错误,欢迎大家指正批评,愿与大家在问题的交流之中共同进步。愈激烈,愈深刻。

CSS 中功能相似伪类间的区别的更多相关文章

  1. CSS中的一些伪类

    一.:nth-child 和 :nth-of-type (1):nth-child() :nth-child(n) 选择器选取某任意一父元素的第 n 个子元素( p:nth-child(n) 即选中任 ...

  2. 学习css常用基本层级伪类属性选择器

    常见的css选择器包含:常用选择器.基本选择器.层级选择器.伪类选择器.属性选择器,其中常用选择器分为:1.html选择符*{}//给页面上所有的标签设置模式:2.类选择符.hcls{}//给clas ...

  3. 妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些

    妙味css3课程---1-2.css3中新增的伪类和伪元素有哪些 一.总结 一句话总结: 1.div:target{}是什么意思? 比如a标签的锚点链接到div,div:target{}就可以找到这个 ...

  4. css中px,em和rem的区别

    css中px,em和rem的区别 今天,突然间发现一个特别有意思的问题,就是无意间看到一个网站中的em并不是16px,下面展开了对于px和em以及rem的探究. 首先,px是绝对长度单位,是相对于显示 ...

  5. css中word-break、word-wrap和white-space的区别

    css中word-break.word-wrap和white-space的区别 :https://baijiahao.baidu.com/s?id=1578623236521030997&wf ...

  6. 前端笔记之HTML5&CSS3(中)选择器&伪类伪元素&CSS3效果&渐变背景&过渡

    一.CSS3选择器 CSS3是CSS的第三代版本,新增了很多功能,例如:强大的选择器.盒模型.圆角.渐变.动画.2D/3D转换.文字特效等. CSS3和HTML5没有任何关系!HTML5骨架中,可以用 ...

  7. CSS设计指南之伪类

    伪类这个叫法源自它们与类相似,但实际上并没有类会附加到标记中的标签上.伪类分两种. UI伪类会在HTML元素处于某个状态时(比如鼠标指针位于链接上),为该元素应用CSS样式. 结构化伪类会在标记中存在 ...

  8. Html学习之十(CSS选择器的使用--伪类选择器)

    伪类选择器 一.基于a标签. 1.:hover 选择鼠标滑过的超链接元素 2.:active 选择鼠标单击中的超链接元素 3.:link 选择未被访问的超链接元素 4.:visited 选择已经被访问 ...

  9. css 04-CSS选择器:伪类

    04-CSS选择器:伪类 #伪类(伪类选择器) 伪类:同一个标签,根据其不同的种状态,有不同的样式.这就叫做"伪类".伪类用冒号来表示. 比如div是属于box类,这一点很明确,就 ...

随机推荐

  1. Ubuntu解压缩rar格式文件

    解压缩rar文件时,出现问题 解决方法: sudo apt-get install unrar

  2. java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;

    java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L ...

  3. leetcode.排序.215数组中的第k个最大元素-Java

    1. 具体题目 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 : 输入: [3,2,1,5,6,4] 和 k = ...

  4. centos netstat 查看是否开放了端口

    netstat命令各个参数说明如下: -a :所有 -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protoc ...

  5. C++中初始化列表的使用

    1,初始化列表是在 C++ 中才引入的: 2,以“类中是否可以定义 const 成员?”这个问题来引入初始化列表: 1,const 这个关键字可以定义真正意义上的常量,也可以在某些情况下定义只读变量: ...

  6. Java开发中的23种设计模式详解(2)结构型

    我们接着讨论设计模式,上篇文章我讲完了5种创建型模式,这章开始,我将讲下7种结构型模式:适配器模式.装饰模式.代理模式.外观模式.桥接模式.组合模式.享元模式.其中对象的适配器模式是各种模式的起源,我 ...

  7. SQL数据库—<6>存储过程

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  8. CentOS7.6系统安装详解(含真机装系统的采坑之旅)!

    刚开始学习linux操作系统是总是很茫然,无所适从,以下是自己总结的工作经验,仅供参考! 一.准备资源 安装前需要准备的资源有linux系统centos7.6发行版系统镜像,vmware workst ...

  9. Nginx动静分离基本概述

    Nginx动静分离基本概述 动静分离,通过中间将动静分离和静态请求进行分离: 通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同时能减少请求的延时. 通过中间件将动态请求和静态请求分离, ...

  10. windows下如何使用Git上传代码

    首先,在使用Git的同时,我们需要拥有码云账号,在官网注册即可(官网:https://gitee.com/). 注册结束后创建一个代码仓库,最好和要上传的文件夹名字一样: 1.首先在电脑上安装wind ...