【问题标题】:Alternating Row Colors in Bootstrap 3 - No TableBootstrap 3中的交替行颜色 - 无表
【发布时间】:2014-09-27 01:44:02
【问题描述】:

我正在寻找一种在 Bootstrap 3 的响应式布局中交替行颜色的方法。如果没有大量广泛、令人困惑的 CSS,我无法弄清楚如何做到这一点,并希望有人有更好的解决方案。

这是一个简单的前提:12 个 div 在大屏幕上显示为 4 行 3,在小屏幕上显示为 6 行,每行 2,在移动设备上显示为 12 行。无论屏幕大小如何,行都需要具有交替的背景颜色。

Bootstrap 3 的 HTML 如下:

<div class="container">
    <div class="row">
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-01</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-02</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-03</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-04</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-05</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-06</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-07</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-08</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-09</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-10</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-11</div>
        <div class="col-md-4 col-sm-6 col-xs-12">Emp-12</div>
    </div>
</div>

任何想法/提示/帮助将不胜感激。

【问题讨论】:

标签: css twitter-bootstrap-3


【解决方案1】:

我在使用引导表条带类为表中的行着色时遇到问题,然后实现删除表条带类并在 css 文件中执行此操作

tr:nth-of-type(odd)
{  
background-color: red;
}
tr:nth-of-type(even)
{  
background-color: blue;
}

引导表条带化类将覆盖您的选择器。

【讨论】:

    【解决方案2】:

    线程有点旧。但从标题来看,我认为它可以满足我的需求。不幸的是,我的结构并不容易适用于第 n 个类型的解决方案。这是 Thymeleaf 解决方案。

    .back-red {
      background-color:red;
    }
    .back-green {
      background-color:green;
    }
    
    
    <div class="container">
        <div class="row" th:with="employees=${{'emp-01', 'emp-02', 'emp-03', 'emp-04', 'emp-05', 'emp-06', 'emp-07', 'emp-08', 'emp-09', 'emp-10', 'emp-11', 'emp-12'}}">
            <div class="col-md-4 col-sm-6 col-xs-12" th:each="i:${#numbers.sequence(0, #lists.size(employees))}" th:classappend'(${i} % 2) == 0?back-red:back-green"><span th:text="${emplyees[i]}"></span></div>
        </div>
    </div>
    

    【讨论】:

      【解决方案3】:

      我发现如果我指定.row:nth-of-type(..),我的其他行的元素(用于其他格式等)也会得到交替的颜色。因此,我会在我的 css 中定义一个全新的类:

      .row-striped:nth-of-type(odd){
        background-color: #efefef;
      }
      
      .row-striped:nth-of-type(even){
        background-color: #ffffff;
      }
      

      所以现在,当我将其类指定为 .row-striped 时,交替的行颜色将仅适用于行容器,而不是 row 中的元素。

      <!-- this entire row container is #efefef -->
      <div class="row row-striped">
          <div class="form-group">
              <div class="col-sm-8"><h5>Field Greens with strawberry vinegrette</h5></div>
              <div class="col-sm-4">
                  <input type="number" type="number" step="1" min="0"></input><small>$30/salad</small>
              </div>
          </div>
      </div>
      
      <!-- this entire row container is #ffffff -->
      <div class="row row-striped">
          <div class="form-group">
              <div class="col-sm-8"><h5>Greek Salad</h5></div>
              <div class="col-sm-4">
                  <input type="number" type="number" step="1" min="0"></input><small>$25/salad</small>
              </div>
          </div>
      </div>
      

      【讨论】:

        【解决方案4】:

        如果不让 css 变得有点复杂,真的没有办法做到这一点,但这是我可以放在一起的最干净的解决方案(这里的断点仅用于示例目的,将它们更改为你实际的任何断点使用。)关键是:nth-of-type(或:nth-child——在这种情况下都可以。)

        最小视口:

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

        中等视口:

        @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;
          }
        
        }
        

        最大视口:

        @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;
          }
        }
        

        工作小提琴here

        【讨论】:

          【解决方案5】:

          由于您使用的是 bootstrap,并且您希望为每种屏幕尺寸交替使用行颜色,因此您需要为所有屏幕尺寸编写单独的样式规则。

          /* For small screen */
          .row :nth-child(even){
            background-color: #dcdcdc;
          }
          .row :nth-child(odd){
            background-color: #aaaaaa;
          }
          
          /* For medium screen */    
          @media (min-width: 768px) {
              .row :nth-child(4n), .row :nth-child(4n-1) {
                  background: #dcdcdc;
              }
              .row :nth-child(4n-2), .row :nth-child(4n-3) {
                  background: #aaaaaa;
              }
          }
          
          /* For large screen */
          @media (min-width: 992px) {
              .row :nth-child(6n), .row :nth-child(6n-1), .row :nth-child(6n-2) {
                  background: #dcdcdc;
              }
              .row :nth-child(6n-3), .row :nth-child(6n-4), .row :nth-child(6n-5) {
                  background: #aaaaaa;
              }
          }
          

          工作FIDDLE
          我还在这里包含了引导 CSS。

          【讨论】:

          • 谢谢!这比我所拥有的要简单得多。由于某种原因,我无法将头绕在“第 n 个孩子”周围。
          • 在处理复杂的 nth-child 模式时进行计算;)
          • 这会交替行中 div 的颜色。不是整行。还是我错过了什么?
          • 如果每个 div 总是它自己的行,它就可以工作。但事实并非如此。问题是在不同的断点处有不同的行布局
          【解决方案6】:

          您可以使用此代码:

          .row :nth-child(odd){
            background-color:red;
          }
          .row :nth-child(even){
            background-color:green;
          }
          

          演示http://codepen.io/mouhammed/pen/rblsC

          【讨论】:

          • 理论上,当然,如果每个 div 总是它自己的行。但事实并非如此。问题是在不同的断点处有不同的行布局。
          猜你喜欢
          • 1970-01-01
          • 2012-12-30
          • 2010-10-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-13
          • 1970-01-01
          相关资源
          最近更新 更多