Alternating Row Colors in Bootstrap 3 - No Table

后端 未结 6 1970
天涯浪人
天涯浪人 2020-12-24 11:48

I am looking for a way to do alternating row colors in a responsive layout in Bootstrap 3. I cannot figure out how to do it without a LOT of extensive, confusing CSS and was

6条回答
  •  独厮守ぢ
    2020-12-24 12:46

    There isn't really a way to do this without the css getting a little convoluted, but here's the cleanest solution I could put together (the breakpoints in this are just for example purposes, change them to whatever breakpoints you're actually using.) The key is :nth-of-type (or :nth-child -- either would work in this case.)

    Smallest viewport:

    @media (max-width:$smallest-breakpoint) {
    
      .row div {
         background: #eee;
       }
    
      .row div:nth-of-type(2n) {
         background: #fff;
       }
    
    }
    

    Medium viewport:

    @media (min-width:$smallest-breakpoint) and (max-width:$mid-breakpoint) {
    
      .row div {
        background: #eee;
      }
    
      .row div:nth-of-type(4n+1), .row div:nth-of-type(4n+2) {
        background: #fff;
      }
    
    }
    

    Largest viewport:

    @media (min-width:$mid-breakpoint) and (max-width:9999px) {
    
      .row div {
        background: #eee;
      }
    
      .row div:nth-of-type(6n+4), 
      .row div:nth-of-type(6n+5), 
      .row div:nth-of-type(6n+6) {
          background: #fff;
      }
    }
    

    Working fiddle here

提交回复
热议问题