好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

nth 类型元素选择器

nth 元素选择

当我们要一组 class 同名,或者连续的一组元素的其中 一个 ,或者某种规律的元素 添加 单独样式的时候,不妨看看这类的元素选择器。

1. 官方定义

nth-child(n) 选择器匹配属于其父元素的第 N 个子元素; nth-last-child(n) 选择器匹配属于其元素的第 N 个子元素的每个元素,从最后 一个 子元素开始计数; nth-of-type(n) 选择器匹配属于父元素的 特定类型 的第 N 个子元素的每个元素。

2. 解释

nth-child(n) 、 nth-last-child(n) 、 nth-of-type(n) 都是用来匹配父元素内部子元素的。不过也有些区别:
nth-child 按照个数来算;
nth-of-type 按照类型来计算;
nth-last-child(n) 从最后 一个 子元素往前开始计算。

3. 语法

   .item  :nth-child(2n+1)   { 

 } 
  .item  :nth-of-type(n)   { 

 } 
  .item  :nth-last-child(2n)   { 

 } 

n 从  开始计数的正整数。
 

4. 兼容性

IE Edge Firefox Chrome Safari Opera ios android all all all all all all all all

5. 实例

选择 demo 内第 3 个子元素背景为红色。

使用 nth-child 。

   .item   { 
     width  :  px ; 
     height  :  px ; 
     text-align  :  center ; 
     line-height  :  px ; 
     border  :  px solid  #ccc  ; 
     background  :   #f2f2f2  ; 
 } 
  .item  :nth-child(3)   { 
     background  :  red ; 
 } 
 

效果 图:

第三个背景变红 效果 图

  <!DOCTYPE html> 
   < html   lang   =  " en "   >  
   < head  >  
       <  Meta    charset   =  " UTF-8 "   >  
       <  Meta    name   =  " viewport "    content   =  " width=device-width, initial-scale=1.0 "   >  
       < title  >  Document   </ title  >  
       < style  >   
          .item   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         } 
          .item  :nth-child(3)   { 
             background  :  red ; 
         } 
        </ style  >  
   </ head  >  
   < body  >  
       < div   class   =  " demo "   >  
           < div   class   =  " item "   >  1   </ div  >  
           < div   class   =  " item "   >  2   </ div  >  
           < div   class   =  " item "   >  3   </ div  >  
           < div   class   =  " item "   >  4   </ div  >  
       </ div  >  
   </ body  >  
   </ html  >  
 

使用 nth-last-child 。

   .item   { 
     width  :  px ; 
     height  :  px ; 
     text-align  :  center ; 
     line-height  :  px ; 
     border  :  px solid  #ccc  ; 
     background  :   #f2f2f2  ; 
 } 
  .item  :nth-last-child(2)   { 
     background  :  red ; 
 } 
 

效果 图

第三个背景变红 效果 图

  <!DOCTYPE html> 
   < html   lang   =  " en "   >  
   < head  >  
       <  Meta    charset   =  " UTF-8 "   >  
       <  Meta    name   =  " viewport "    content   =  " width=device-width, initial-scale=1.0 "   >  
       < title  >  Document   </ title  >  
       < style  >   
          .item   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         } 
          .item  :nth-last-child(2)   { 
             background  :  red ; 
         } 
        </ style  >  
   </ head  >  
   < body  >  
       < div   class   =  " demo "   >  
           < div   class   =  " item "   >  1   </ div  >  
           < div   class   =  " item "   >  2   </ div  >  
           < div   class   =  " item "   >  3   </ div  >  
           < div   class   =  " item "   >  4   </ div  >  
       </ div  >  
   </ body  >  
   </ html  >  
 

使用 nth-of-type 。

   .item   { 
     width  :  px ; 
     height  :  px ; 
     text-align  :  center ; 
     line-height  :  px ; 
     border  :  px solid  #ccc  ; 
     background  :   #f2f2f2  ; 
 } 
  .item  :nth-of-type(3)   { 
     background  :  red ; 
 } 
 

效果 图

第三个背景变红 效果 图

  <!DOCTYPE html> 
   < html   lang   =  " en "   >  
   < head  >  
       <  Meta    charset   =  " UTF-8 "   >  
       <  Meta    name   =  " viewport "    content   =  " width=device-width, initial-scale=1.0 "   >  
       < title  >  Document   </ title  >  
       < style  >   
          .item   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         } 
          .item  :nth-of-type(3)   { 
             background  :  red ; 
         } 
        </ style  >  
   </ head  >  
   < body  >  
       < div   class   =  " demo "   >  
           < div   class   =  " item "   >  1   </ div  >  
           < div   class   =  " item "   >  2   </ div  >  
           < div   class   =  " item "   >  3   </ div  >  
           < div   class   =  " item "   >  4   </ div  >  
       </ div  >  
   </ body  >  
   </ html  >  
 

6. 经验 分享

在实例中我们看到 nth-of-type 和 nth-child 同样都使用的是 (3), 那么它们的不同是什么呢?下面这个例子我们一起看下:

    < div   class   =  " demo "   >  
       < p   class   =  " item "   >  我是 p  标签    </ p  >  
       < div   class   =  " item "   >  1   </ div  >  
       < div   class   =  " item "   >  2   </ div  >  
       < div   class   =  " item "   >  3   </ div  >  
       < div   class   =  " item "   >  4   </ div  >  
   </ div  >  
   < div   class   =  " demo "   >  
       < p   class   =  " item-2 "   >  我是 p  标签    </ p  >  
       < div   class   =  " item-2 "   >  1   </ div  >  
       < div   class   =  " item-2 "   >  2   </ div  >  
       < div   class   =  " item-2 "   >  3   </ div  >  
       < div   class   =  " item-2 "   >  4   </ div  >  
   </ div  >  
 

      .demo   { 
            float  :  left ; 
        } 
         .item , .item-2   { 
            width  :  px ; 
            height  :  px ; 
            text-align  :  center ; 
            line-height  :  px ; 
            border  :  px solid  #ccc  ; 
            background  :   #f2f2f2  ; 
        }         
         .item  :nth-of-type(3)   { 
            background  :  red ; 
        } 
         .item-2  :nth-child(3)   { 
            background  :  red ; 
        } 
 

效果 图

`nth-of-type` 和 `nth-child` 效果 图

通过 效果 图我们就清楚的明白他们的差异了。
简述实例展现 效果 ,通过实例分析他们两个的区别

  <!DOCTYPE html> 
   < html   lang   =  " en "   >  
   < head  >  
       <  Meta    charset   =  " UTF-8 "   >  
       <  Meta    name   =  " viewport "    content   =  " width=device-width, initial-scale=1.0 "   >  
       < title  >  Document   </ title  >  
       < style  >   
          .demo   { 
             float  :  left ; 
         } 
          .item , .item-2   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         }         
          .item  :nth-of-type(3)   { 
             background  :  red ; 
         } 
          .item-2  :nth-child(3)   { 
             background  :  red ; 
         } 
        </ style  >  
   </ head  >  
   < body  >  
       < div   class   =  " demo "   >  
           < p   class   =  " item "   >  我是 p  标签    </ p  >  
           < div   class   =  " item "   >  1   </ div  >  
           < div   class   =  " item "   >  2   </ div  >  
           < div   class   =  " item "   >  3   </ div  >  
           < div   class   =  " item "   >  4   </ div  >  
       </ div  >  
       < div   class   =  " demo "   >  
           < p   class   =  " item-2 "   >  我是 p  标签    </ p  >  
           < div   class   =  " item-2 "   >  1   </ div  >  
           < div   class   =  " item-2 "   >  2   </ div  >  
           < div   class   =  " item-2 "   >  3   </ div  >  
           < div   class   =  " item-2 "   >  4   </ div  >  
       </ div  >  
   </ body  >  
   </ html  >  
 

下面是让所有偶数的背景变红。

    .item   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         } 
          .item  :nth-of-type(2n)   { 
             background  :  red ; 
         } 
 

效果 图:

偶数的背景变红 效果 图

  <!DOCTYPE html> 
   < html   lang   =  " en "   >  
   < head  >  
       <  Meta    charset   =  " UTF-8 "   >  
       <  Meta    name   =  " viewport "    content   =  " width=device-width, initial-scale=1.0 "   >  
       < title  >  Document   </ title  >  
       < style  >   
          .item   { 
             width  :  px ; 
             height  :  px ; 
             text-align  :  center ; 
             line-height  :  px ; 
             border  :  px solid  #ccc  ; 
             background  :   #f2f2f2  ; 
         } 
          .item  :nth-of-type(2n)   { 
             background  :  red ; 
         } 
        </ style  >  
   </ head  >  
   < body  >  
       < div   class   =  " demo "   >  
           < div   class   =  " item "   >  1   </ div  >  
           < div   class   =  " item "   >  2   </ div  >  
           < div   class   =  " item "   >  3   </ div  >  
           < div   class   =  " item "   >  4   </ div  >  
       </ div  >  
   </ body  >  
   </ html  >  
 

使用 nth-of-type(3n+1) 起作用,而 nth-of-type(1+3n) 不起作用,所以 n 一定要放在最前面。

calc 计算属性 ? ?before && after 位置

查看更多关于nth 类型元素选择器的详细内容...

  阅读:38次

上一篇

下一篇

第1节:CSS3简介    第2节:border 边框    第3节:borderImage 边框图片    第4节:border-radius 圆角    第5节:box-shadow 阴影    第6节:box-sizing 盒类型    第7节:gradients 渐变    第8节:text-justify 对齐    第9节:text-overflow 文字超出    第10节:text-shadow 文本阴影    第11节:word-break 文本打断    第12节:word-wrap 文本换行    第13节:letter-spacing 字间距    第14节:perspective 透视    第15节:transform 2D 空间转换    第16节:transform 3D 空间转换    第17节:transition 过渡    第18节:animation 动画    第19节:columns 字符分割    第20节:flex 弹性盒子布局    第21节:flex order 顺序    第22节:flex: grow、shrink、basis    第23节:flex-direction 排列方向    第24节:justify-content (轴内)对齐方式    第25节:flex-wrap 换行    第26节:align-items 竖直方向对齐方式    第27节:align-content    第28节:Grid 布局简介    第29节:Grid 行和列    第30节:media 媒体查询    第31节:only 元素选择    第32节:before && after 位置    第33节:nth 类型元素选择器    第34节:calc 计算属性