使用 CSS Grid 每隔一行着色
Posted
技术标签:
【中文标题】使用 CSS Grid 每隔一行着色【英文标题】:Coloring every other row using CSS Grid 【发布时间】:2020-02-03 16:30:10 【问题描述】:我想为我正在使用 CSS Grid 构建的表格中的每一行着色。我无法让它工作,我只能让其他每一列都有颜色。这是我想要做的图片。会有更好的方法来构建它吗?我只使用 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>
【问题讨论】:
老实说对于一个表格来说,使用表格元素是最好的选择。然后,您可以使用tr:nth-child(even)
(或奇数)来设置行的样式。如果您想学习网格,请构建网格布局,而不是表格。类似于侧边栏,画廊中的图像网格。
【参考方案1】:
如果您有一个六列网格,那么每 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()
:nth-child()
CSS 伪类根据元素在一组兄弟元素中的位置匹配元素。
【讨论】:
:nth-child(n+n)
上的信息链接(即使看起来很明显)会有所帮助。很难搜索这样的东西,因为我们没有提供该技术的名称。
@BBaysinger 好点,添加了指向 mdn 的链接以供参考。【参考方案2】:
您正在为每一行定义一个固定的高度,以便您可以轻松地考虑重复渐变:
.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);
background:
repeating-linear-gradient(#ddd 0 35px,transparent 35px 70px);
<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 Grid 每隔一行着色的主要内容,如果未能解决你的问题,请参考以下文章
使用 facet_grid 时如何对 geom_rect 进行一致的着色?