html - 如何交替表格中的列背景颜色?

标签 html css twitter-bootstrap

这听起来很简单,但我找不到很多关于它的帖子。

基本上我想要第一列红色,第二列绿色,第三列红色等......

我如何在 css 中做到这一点?

这是我的 html 代码(我使用的是 bootstrap 3):

        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Welsh</th>
                    <th>English</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Shwmae <a href="#" onclick="playAudio('shwmae');"><span class="glyphicon glyphicon-volume-up play"></span></a></td>
                    <td>Hello</td>
                </tr>
                <tr>
                    <td>Bore da <a href="#" onclick="playAudio('boreda');"><span class="glyphicon glyphicon-volume-up play"></span></a></td>
                    <td>Good morning</td>
                </tr>
                <tr>
            </tbody>
        </table>

我试过使用以下 CSS,但它没有做任何事情:

col:first-child {background: #FF0}
col:nth-child(2n+3) {background: #CCC}

最佳答案

您需要通过添加列组来表明存在

HTML

<table>
    <colgroup>
    <col>
    <col>
    <col>
  </colgroup>
  <tr>
    <th>Descrizione</th>
    <th>Convenzione</th>
    <th>Privato</th>
  </tr>

  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>

  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>

  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>

  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>
  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>

  <tr>
    <td>Tomografia computerizzata delle arcate dentarie</td>
    <td>€ 36,15</td>
    <td>€ 100,00</td>
  </tr>  
</table>

CSS

col:nth-child(odd) {
  background:red;
}

col:nth-child(even) {
  background:green;
}

CODEPEN DEMO

关于html - 如何交替表格中的列背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19563454/

相关文章:

html - 为输入类型复选框添加单独的 CSS

html - 无序列表导航 - 悬停元素向左移动 1px

CSS - Wordpress 将两个 iframe 与文本并排放置

css - Flexbox:带有列换行的可变/动态高度元素

javascript - jQuery 对话框不适用于动态创建的元素

javascript - 在 IE10+ 中分别设置占位符透明度和输入文本元素的边框透明度?

javascript - 如何使用矢量作为坐标在 Canvas 上绘制对象?

javascript - 如何显示:block effects on transition:opacity?

javascript - TypeError : jQuery(. ..).find(...).carousel 不是函数

css - Twitter Bootstrap 2.3.2 中是否有用于导航栏的下拉类?