gpt4 book ai didi

css - 使用 CSS Grid 每隔一行着色

转载 作者:太空宇宙 更新时间:2023-11-04 00:41:16 25 4
gpt4 key购买 nike

我想为我使用 CSS 网格构建的表格中的每一行着色。虽然我无法让它工作,但我只能让所有其他列着色。这是我想要做的事情的图片。会有更好的方法来构建它吗?我使用 CSS Grid 只是因为它是我想学习的新东西。

picture of how I want it to look

picture of my table

这是我当前的代码:

  .wrapper {
border-style: solid;
border-color: rgb(230, 230, 230);
font-weight: bold;
text-align: center;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(18, 35px);
grid-column-gap: 0px;
grid-row-gap: 0px;
}

.wrapper>div:nth-child(odd) {
background: #ddd;
<div class="container">
<div class="wrapper">
<div>Month</div>
<div>Overtime Hours</div>
<div>Compensation Hours</div>
<div>Vacation</div>
<div>Personal Hours</div>
<div>Sick Hours</div>

<div>Carry Over</div>
<div>0.00</div>
<div>-</div>
<div>35.00</div>
<div>-</div>
<div>-</div>


<div>Allotted</div>
<div>-</div>
<div>-</div>
<div>140.00</div>
<div>14.00</div>
<div>-</div>

<div>Starting Total</div>
<div>0.00</div>
<div>-</div>
<div>175.00</div>
<div>14.00</div>
<div>-</div>

<div>Jan</div>
<div>-</div>
<div>-</div>
<div>-</div>
<div>2.00</div>
<div>7.00</div>


<div>Feb</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>-</div>
<div>-</div>

<div>March</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>April</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>May</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jun</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jul</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Aug</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Sep</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Oct</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Nov</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Dec</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Yearly Total</div>
<div>0.00</div>
<div>0.00</div>
<div>150.50</div>
<div>10.50</div>
<div>28.00</div>

<div>Balance in Hours</div>
<div></div>
<div>0.00</div>
<div>24.50</div>
<div>3.50</div>
<div></div>

<div>Balance in Days</div>
<div></div>
<div>0.00</div>
<div>3.50</div>
<div>0.50</div>
<div></div>
</div>
</div>

最佳答案

如果你有一个六列网格,那么你每 6+1 个元素就有一个新行,对于每个奇数行的替代模式,那么你的重复模式每 12+1 个元素开始。 :nth-child(n+n) 会在这里帮助你:

.wrapper {
border-style: solid;
border-color: rgb(230, 230, 230);
font-weight: bold;
text-align: center;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(18, 35px);
grid-column-gap: 0px;
grid-row-gap: 0px;
}
/* 6 columns, odd rows starts every 12th element, a row is made of six elements, so here is 6 selectors to select an entire row*/
.wrapper>div:nth-child(12n+1),
.wrapper>div:nth-child(12n+2),
.wrapper>div:nth-child(12n+3),
.wrapper>div:nth-child(12n+4),
.wrapper>div:nth-child(12n+5),
.wrapper>div:nth-child(12n+6)
{
background: #ddd;
}
<div class="container">
<div class="wrapper">
<div>Month</div>
<div>Overtime Hours</div>
<div>Compensation Hours</div>
<div>Vacation</div>
<div>Personal Hours</div>
<div>Sick Hours</div>

<div>Carry Over</div>
<div>0.00</div>
<div>-</div>
<div>35.00</div>
<div>-</div>
<div>-</div>


<div>Allotted</div>
<div>-</div>
<div>-</div>
<div>140.00</div>
<div>14.00</div>
<div>-</div>

<div>Starting Total</div>
<div>0.00</div>
<div>-</div>
<div>175.00</div>
<div>14.00</div>
<div>-</div>

<div>Jan</div>
<div>-</div>
<div>-</div>
<div>-</div>
<div>2.00</div>
<div>7.00</div>


<div>Feb</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>-</div>
<div>-</div>

<div>March</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>April</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>May</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jun</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jul</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Aug</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Sep</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Oct</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Nov</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Dec</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Yearly Total</div>
<div>0.00</div>
<div>0.00</div>
<div>150.50</div>
<div>10.50</div>
<div>28.00</div>

<div>Balance in Hours</div>
<div></div>
<div>0.00</div>
<div>24.50</div>
<div>3.50</div>
<div></div>

<div>Balance in Days</div>
<div></div>
<div>0.00</div>
<div>3.50</div>
<div>0.50</div>
<div></div>
</div>
</div>

注意:如果您的任何元素跨越超过 1 个单元格:nth-child(n+x) 规则 会破裂

https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child#examples

:nth-child()

The :nth-child() CSS pseudo-class matches elements based on their position in a group of siblings.


如果网格有间隙,注释会显示一个可能的问题:

.wrapper {
border-style: solid;
border-color: rgb(230, 230, 230);
font-weight: bold;
text-align: center;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(18, 35px);
grid-column-gap: 0px;
grid-row-gap: 0px;
gap: 0.5em 1em;
overflow:hidden;
}
/* 6 columns, odd rows starts every 12th element, a row is made of six elements, so here is 6 selectors to select an entire row*/
.wrapper>div:nth-child(12n+1),
.wrapper>div:nth-child(12n+2),
.wrapper>div:nth-child(12n+3),
.wrapper>div:nth-child(12n+4),
.wrapper>div:nth-child(12n+5),
.wrapper>div:nth-child(12n+6)
{
background: #ddd;
box-shadow:1em 0 #ddd
}
<div class="container">
<div class="wrapper">
<div>Month</div>
<div>Overtime Hours</div>
<div>Compensation Hours</div>
<div>Vacation</div>
<div>Personal Hours</div>
<div>Sick Hours</div>

<div>Carry Over</div>
<div>0.00</div>
<div>-</div>
<div>35.00</div>
<div>-</div>
<div>-</div>


<div>Allotted</div>
<div>-</div>
<div>-</div>
<div>140.00</div>
<div>14.00</div>
<div>-</div>

<div>Starting Total</div>
<div>0.00</div>
<div>-</div>
<div>175.00</div>
<div>14.00</div>
<div>-</div>

<div>Jan</div>
<div>-</div>
<div>-</div>
<div>-</div>
<div>2.00</div>
<div>7.00</div>


<div>Feb</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>-</div>
<div>-</div>

<div>March</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>April</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>May</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jun</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Jul</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Aug</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Sep</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Oct</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Nov</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Dec</div>
<div>-</div>
<div>-</div>
<div>7.00</div>
<div>2.00</div>
<div>3.50</div>

<div>Yearly Total</div>
<div>0.00</div>
<div>0.00</div>
<div>150.50</div>
<div>10.50</div>
<div>28.00</div>

<div>Balance in Hours</div>
<div></div>
<div>0.00</div>
<div>24.50</div>
<div>3.50</div>
<div></div>

<div>Balance in Days</div>
<div></div>
<div>0.00</div>
<div>3.50</div>
<div>0.50</div>
<div></div>
</div>
</div>

关于css - 使用 CSS Grid 每隔一行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58251569/

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