首页 > 其他分享 >Vue进阶(幺幺捌):CSS3 - 选择器first-child、last-child、nth-child、nth-last-child、nth-of-type

Vue进阶(幺幺捌):CSS3 - 选择器first-child、last-child、nth-child、nth-last-child、nth-of-type

时间:2023-06-08 10:03:48浏览次数:47  
标签:last 项目 h2 li nth child 列表

(文章目录)

1. first-child(IE7兼容)、last-child(IE8不兼容)

html:

<body>
  <h2>列表</h2>
  <ul>
    <li>列表项目1</li>
    <li>列表项目2</li>
    <li>列表项目3</li>
    <li>列表项目4</li>
  </ul>
</body>

css:

<style>
    ul {list-style: none;}
    li:first-child {/*给第一个列表项目设置样式 兼容IE7*/
      background-color: pink;
    }
    li:last-child { /*给最后一个列表项目设置样式 IE8不兼容*/
      background-color: red;
    }
</style>

解析: 一个页面中无论有几个ul列表,只要设置first-child、last-child,那么所有ul列表项的第一个和最后一个列表项目都会有设置的样式。

2.nth-child、nth-last-child (IE8不兼容)

html:

<body>
  <h2>列表</h2>
  <ul>
    <li>列表项目1</li>
    <li>列表项目2</li>
    <li>列表项目3</li>
    <li>列表项目4</li>
    <li>列表项目5</li>
    <li>列表项目6</li>
  </ul>
</body>

css:

<style>
    ul {list-style: none;}    
    li:nth-child(3) { /*表示li元素中,第三个元素 IE8不兼容*/
      background-color: pink;
    }
    li:nth-last-child(2) { /*表示li元素中,倒数第二个元素 IE8不兼容*/
      background-color: red;
    }
</style>

3. 对奇数、偶数使用样式

html:

<ul>
    <li>列表项目1</li>
    <li>列表项目2</li>
    <li>列表项目3</li>
    <li>列表项目4</li>
    <li>列表项目5</li>
    <li>列表项目6</li>
</ul>

css:

<style>
    ul {list-style: none;}
    li:nth-child(odd) {/*表示li元素中,从1开始 奇数为pink*/
      background-color: pink;
    }
    li:nth-child(even) {/*表示li元素中,从1开始 偶数为灰色*/
      background-color: #ccc;
    }
</style>

解析: li:nth-child(odd)含义:li父元素ul的儿子中,从1开始数,奇数儿子设置样式为xxx;

当父元素为列表时,因为只有列表项目一种子元素,不会出现问题;当父元素是div时,就不止一种子元素,会引起问题。如下:

例如:设置div元素中为奇数标题h2背景颜色。

html:

<div>
    <h2>文章标题A</h2>
    <p>我是一个小段落。。。</p>
    <h2>文章标题B</h2>
    <p>我是一个小段落。。。</p>
    <h2>文章标题C</h2>
    <p>我是一个小段落。。。</p>
    <h2>文章标题D</h2>
    <p>我是一个小段落。。。</p>
</div>

css:

h2:nth-child(odd) {
      background-color: pink;
}

解析:由于div元素下奇数元素为p,偶数元素为后,故 h2:nth-child(odd)执行结果为:h2的父元素div 的所有儿子中 为奇数的儿子 设置背景颜色;而不是所有h2中为偶数的h2设置样式; 解决方法: nth-of-type可以避免问题产生。

4. nth-of-type(IE8不兼容):只针对同类型的元素进行计算

css:

h2:nth-of-type(odd) { /*所有h2标签中为奇数的设置样式*/
    background-color: pink;
}
h2:nth-of-type(even) { /*所有h2标签中为偶数的设置样式*/
    background-color: #ccc;
}

解析: h2:nth-of-type(odd)含义:在所有h2标签中,只要是奇数h2就设置样式;只针对h2标签,与父元素无关;

5. 循环使用样式 li:nth-child(4n+1)

html:

<ul>
   <li>列表项目1</li>
   <li>列表项目2</li>
   <li>列表项目3</li>
   <li>列表项目4</li>
   <li>列表项目5</li>
   <li>列表项目6</li>
   <li>列表项目7</li>
   <li>列表项目8</li>
   <li>列表项目9</li>
   <li>列表项目10</li>
   <li>列表项目11</li>
   <li>列表项目12</li>
</ul>

css:

<style>
    ul {list-style: none;}
    li:nth-child(4n+1) { /*表示li元素中,4个li为一组循环 第一个li设置为*/
      background-color: red;
    }
    li:nth-child(4n+2) { /*表示li元素中,4个li为一组循环 第二个li设置为*/
      background-color: pink;
    }
    li:nth-child(4n+3) {
      background-color: #ccc;
    }
    li:nth-child(4n+4) {
      background-color: yellow;
    }
</style>

解析: 4n含义:四个li元素为一组循环; 4n+1含义:这一组循环中,第一个样式; 4n+2含义:这一组循环中,第二个样式;

4n+3含义:这一组循环中,第三个样式; 4n+4含义:这一组循环中,第四个样式;

拓展阅读

标签:last,项目,h2,li,nth,child,列表
From: https://blog.51cto.com/shq5785/6437672

相关文章

  • ElasticSearch-API-Index的使用亲测
    ElasticSearch-API-Index索引创建API允许初始化一个索引。ElasticSearch对多重索引提供了支持,包括跨多个索引执行操作。每个索引在创建时可以让一个特定的设置项与其关联。最简单的方式创建索引curl-XPUT‘http://localhost:9200/twitter/'在创建索引的时候指定分片和副本数量,参......
  • ElasticSearch
    端口9300组件间通信,9200http界面端口,5601kibana界面基本操作es不支持修改索引信息###创建索引shopping,es中的索引类似于关系数据库中的库curl-XPUThttp://localhost:9200/shopping###查看索引curl-XGEThttp://localhost:9200/shopping###查看所有索引curl......
  • elasticsearch查询
    elasticsearch查询,elasticsearch常见查询,elasticsearch命令elasticsearch常用命令1、查询索引GET_cat/indices 2、查看es磁盘使用情况GET_cat/allocation?v&pretty 3、查看es分词情况GET_analyze{"text":"中国"}elasticsearch常用查询查询基本语法,所......
  • JDK 1.6 与 1.8 中的 ConcurrentHashMap 学习
    ConcurrentHashMap使用segment(继承ReentrantLock)和volatile来保证线程安全性segment的数量为16,每个segment中桶(HashEntry[])的数量可以增加,桶中放的是由HashEntry组成的链表;count表示segment中元素的数量,modCount统计导致结构变化操作的次数(put、remove、replace......
  • Elasticsearch专题精讲—— REST APIs —— Document APIs —— Update By Query API
    RESTAPIs——DocumentAPIs—— UpdateByQueryAPIhttps://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-update-by-query.html#docs-update-by-queryUpdatesdocumentsthatmatchthespecifiedquery.Ifnoqueryisspecified,performsanupdateo......
  • Elasticjob 3.x 最新版本源码解读
    源码地址(含备注):https://gitee.com/ityml/elastic-job-zgc官方网站:https://shardingsphere.apache.org/elasticjob/ElasticJob是面向互联网生态和海量任务的分布式调度解决方案,由两个相互独立的子项目ElasticJob-Lite和ElasticJob-Cloud组成。它通过弹性调度、资源管......
  • Elasticsearch专题精讲—— REST APIs —— Document APIs —— Update API
    RESTAPIs——DocumentAPIs——UpdateAPIhttps://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-update.htmlUpdatesadocumentusingthespecifiedscript.使用指定的脚本更新文档。1、Request(请求)https://www......
  • 分布式搜索elasticsearch集群监控工具bigdesk
    bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu、内存使用情况,索引数据、搜索情况,http连接数等。项目git地址: https://github.com/lukas-vlcek/bigdesk。和head一样,它也是个独立的网页程序,使用方式和head一样。插件安装运行......
  • Elastic_Search 和java的入门结合
    1,pom文件添加依赖... 2,config配置文件  3,写接口文件 ......
  • Mysql 主从备份 Last_Errno: 1146 Last_Error: Error executing row event: 错误问题
    本人在做主从备份的时候发现了此问题! 1主数据库是已经把这个表删除了丛数据库也是没有备份这个表但是一直报这个错原因是bin-log日志有这个表 但是没记录到已经把这个表删除了 主从表同步实际从库是根据主库的bin-log二进制的SQL进行执行的 这是Mysql的一个BUG1......