【问题标题】:How to add alternate colors to a table body(tbody) using css?如何使用 css 向表格主体(tbody)添加替代颜色?
【发布时间】:2020-07-03 00:37:54
【问题描述】:

我在下面有一个带有相应 css 的 html 表格。这些行来自 tbody 内的 jquery 函数。我想用不同的颜色制作交替行。 (例如,所有偶数行为红色)。我尝试使用 nth-child(even) 但由于它在内部,所以它将它应用于所有身体。我有溢出-y:自动。当我使用 nth-child(even) 时,它仅对偶数行启用滚动条,而不是对所有 tbody。如何在 .以下是我的代码:

CSS:

'''

.table-fixed thead {
    width: 100%;
}

.table-fixed tbody tr:nth-child(even) {
    height: 300px;
    overflow-y: auto;
    width: 100%;
    background-color: red;
}


.table-fixed thead, .table-fixed tbody, .table-fixed tr, .table-fixed td, .table-fixed th {
    display: block;
}

.table-fixed tbody td, .table-fixed thead > tr > th {
    float: left;
    border-bottom-width: 0;
}

.table-fixed th {
    background-color: #e5e5e5;
    color: #454545;
}

'''

HTML: '''

<table id="Table1" class="table table-fixed">
                        <thead>
                            <tr>
                                <th class="col-xs-1">Select</th>
                                <th class="col-xs-1" style="text-align:center;">Id</th>
                                <th class="col-xs-1" style="text-align:center;">Date</th>
                                <th class="col-xs-2" style="text-align:center;">Grade</th>
                                <th class="col-xs-7">Comments</th>
                            </tr>
                        </thead>
                        <tbody id="datagrid_tbody">
                            
                        </tbody>

'''

【问题讨论】:

    标签: html jquery css text-styling


    【解决方案1】:

    基于 MDN ZEBRA STYLING https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Styling_tables

    你可以使用:

    tbody tr:nth-child(odd) {
      background-color: #ff33cc;
    }
    
    tbody tr:nth-child(even) {
      background-color: #e495e4;
    }
    

    【讨论】:

    • 谢谢@dylan ...我试过了,但它分别为两个孩子添加了两个卷轴:奇数和偶数。由于我的文字较长,我无法移除滚动条。
    猜你喜欢
    • 2014-11-09
    • 2011-03-06
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    相关资源
    最近更新 更多