gpt4 book ai didi

css - 对表 : how to alternate row colors with two or more columns 使用 DIV

转载 作者:行者123 更新时间:2023-11-28 10:48:54 27 4
gpt4 key购买 nike

我非常成功地使用 DIV 创建了一个表,并让列交替显示颜色。但是,我正在尝试交替行。我知道我很接近,但我现在正用头撞墙。代码有点多,所以我创建了一个包含代码的 jsfiddle.net 元素,您可以看到我得到的结果。

. Here's the link jsFiddle

这是 HTML:

<body>
<div>
<div class="list-table">
<div class="title top">Membership List</div>
<div class="heading-row">
<div class="col-heading-1 col-background">Mbr ID</div>
<div class="col-heading-2 col-background">Member Name</div>
<div class="col-heading-3 col-background">Member Email</div>
<div class="col-heading-4 col-background">Action</div>
</div>
<div class="clear"></div>
<div class="detail">
<div class="col-1">1</div>
<div class="col-2">John Doe</div>
<div class="col-3">jd@johndoe.com</div>
<div class="col-4">Delete</div>
<div class="col-5">Edit</div>
</div>
<div class="clear"></div>
<div class="detail">
<div class="col-1">2</div>
<div class="col-2">John Wayne</div>
<div class="col-3">jw@jw.com</div>
<div class="col-4">Delete</div>
<div class="col-5">Edit</div>
</div>
<div class="clear"></div>
<div class="detail">
<div class="col-1">3</div>
<div class="col-2">Sally Smith</div>
<div class="col-3">sally@sallysmith.com</div>
<div class="col-4">Delete</div>
<div class="col-5">Edit</div>
</div>
<div class="clear"></div>
<div class="title bottom">End of data</div>
</div>
</div>

这是 CSS:

.list-table {
width: 800px;
margin: 0 auto;
}
.list-table .title {
width: 100%;
font-size: large;
font-weight: bolder;
text-align: center;
/* IE10 Consumer Preview */
background-image: -ms-linear-gradient(top, #2AA6FF 0%, #A5F0EF 50%, #2AA6FF 100%);
/* Mozilla Firefox */
background-image: -moz-linear-gradient(top, #2AA6FF 0%, #A5F0EF 50%, #2AA6FF 100%);
/* Opera */
background-image: -o-linear-gradient(top, #2AA6FF 0%, #A5F0EF 50%, #2AA6FF 100%);
/* Webkit (Safari/Chrome 10) */
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, 2AA6FF), color-stop(0.5, #A5F0EF), color-stop(1, #2AA6FF));
/* Webkit (Chrome 11+) */
background-image: -webkit-linear-gradient(top, #2AA6FF 0%, #A5F0EF 50%, #2AA6FF 100%);
/* W3C Markup, IE10 Release Preview */
background-image: linear-gradient(to bottom, #2AA6FF 0%, #A5F0EF 50%, #2AA6FF 100%);
}
.list-table .top {
border-top-left-radius: 10px;
border-top-right-radius: 10px;
}
.list-table .bottom {
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
}
.list-table .heading-row {
}
.list-table .heading-row .col-heading-1 {
width: 10%;
font-size: small;
text-align: center;
vertical-align: middle;
float: left;
}
.list-table .heading-row .col-heading-2 {
width: 35%;
font-size: small;
text-align: center;
vertical-align: middle;
float: left;
}
.list-table .heading-row .col-heading-3 {
width: 35%;
font-size: small;
text-align: center;
vertical-align: middle;
float: left;
}
.list-table .heading-row .col-heading-4 {
width: 20%;
font-size: small;
text-align: center;
vertical-align: middle;
float: left;
}
.list-table .heading-row .col-background {
/* IE10 Consumer Preview */
background-image: -ms-linear-gradient(top, #808080 0%, #D3D3D3 50%, #808080 100%);
/* Mozilla Firefox */
background-image: -moz-linear-gradient(top, #808080 0%, #D3D3D3 50%, #808080 100%);
/* Opera */
background-image: -o-linear-gradient(top, #808080 0%, #D3D3D3 50%, #808080 100%);
/* Webkit (Safari/Chrome 10) */
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #808080), color-stop(0.5, #D3D3D3), color-stop(1, #808080));
/* Webkit (Chrome 11+) */
background-image: -webkit-linear-gradient(top, #808080 0%, #D3D3D3 50%, #808080 100%);
/* W3C Markup, IE10 Release Preview */
background-image: linear-gradient(to bottom, #808080 0%, #D3D3D3 50%, #808080 100%);
}
.list-table .clear {
clear: both;
}
.list-table .detail {
background-color: #eee;
}
.list-table .detail div:nth-child(odd) {
background-color:#eee;
}
.list-table .detail div:nth-child(even) {
background-color:#fff;
}
.list-table .detail .col-1 {
width: 10%;
text-align: center;
float: left;
}
.list-table .detail .col-2 {
width: 35%;
float: left;
}
.list-table .detail .col-3 {
width: 35%;
float: left;
}
.list-table .detail .col-4 {
width: 10%;
text-align: center;
float: left;
}
.list-table .detail .col-5 {
width: 10%;
text-align: center;
float: left;
}

最佳答案

我想这正是您要找的。

为了让事情更简单,我把所有的“行”都放在了一个名为“行”的容器 div 中。我还根据需要应用了背景颜色并保留了替代颜色。我添加了边距和填充以使颜色明显(我知道它看起来不错,但它是为了说明目的而完成的)。

这些是我使用的 2 个 CSS 类:

.detail {
background-color: Gray;
color: Blue;
height:20px;
width:100%;
padding:25px 0;
}
.rows > div:nth-child(4n) {
background-color:Orange;
color: red;
}

然后我修改了(如前所述只是添加了一个名为 row 的容器)HTML:

<div class="rows">
<div class="clear"></div>
<div class="detail">
<div class="col-1">1</div>
<div class="col-2">John Doe</div>
<div class="col-3">jd@johndoe.com</div>
<div class="col-4">Delete</div>
<div class="col-5">Edit</div>
</div>
..Other 'detail' classes
</div>

您可以在这里查看:http://jsfiddle.net/8bLUH/1/

我的想法是,我为您提供了名为“detail”(odd row) 和 rows 的 2 个备用类。 div:nth-child(4n) (偶数行)。您可以在它们上应用您想要的任何样式。

希望这有帮助!!!

关于css - 对表 : how to alternate row colors with two or more columns 使用 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24024529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com