我用CSS来做这样的事情已经有一段时间了,但似乎这个特定的场景并不像它应该的那样工作.

我想要的是一个列表,就像你在Electron 表格中一样,每一行都从浅灰色到略深的灰色交替排列.

Spreadsheet lines 1-9, with alternating colors

<style>
  .spreadsheet {
    display: grid;
    grid-auto-flow: row;
  }
  .spreadsheet > div {
    padding: 0.5rem;
  }
  .spreadsheet > :first-of-type {
    border-radius: 1rem 1rem 0 0;
  }
  .spreadsheet > :only-of-type {
    border-radius: 1rem;
  }
  .spreadsheet > :last-of-type {
    border-radius: 0 0 1rem 1rem;
  }
  .spreadsheet > :nth-of-type(odd) {
    background-color: #fafafa
  }
  .spreadsheet > :nth-of-type(even) {
    background-color: #eaeaea
  }
</style>

<div class="spreadsheet">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div>Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>

正如预期的那样,上述一切都运行得很好.现在我的问题是...在我的场景中,我使用JavaScript根据筛选器更改某些行的类.这样,我只想将过滤出的行的display设置为none,同时保持与以前相同的交替 colored颜色 模式.这还应该使第一行和最后一行具有圆角边框.

Spreadsheet lines 2, 5, 6-7, and 9 with incorrect alternating colors

<style>
  .spreadsheet {
    display: grid;
    grid-auto-flow: row;
  }
  .spreadsheet > div {
    padding: 0.5rem;
  }
  .spreadsheet > :not(.hidden):first-of-type {
    border-radius: 1rem 1rem 0 0;
  }
  .spreadsheet > :not(.hidden):only-of-type {
    border-radius: 1rem;
  }
  .spreadsheet > :not(.hidden):last-of-type {
    border-radius: 0 0 1rem 1rem;
  }
  .spreadsheet > :not(.hidden):nth-of-type(odd) {
    background-color: #fafafa
  }
  .spreadsheet > :not(.hidden):nth-of-type(even) {
    background-color: #eaeaea
  }
  .hidden {
    display: none;
  }
</style>

<div class="spreadsheet">
  <div class="hidden">Line 1</div>
  <div>Line 2</div>
  <div class="hidden">Line 3</div>
  <div class="hidden">Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div class="hidden">Line 8</div>
  <div>Line 9</div>
</div>

你会注意到,Line 1hidden,所以Line 2应该是#fafafabackground-color1rem 1rem 0 0border-radius,但两者都没有.当相同 colored颜色 的线条紧挨着时,这一点非常明显.

你有什么办法让我做到这一点吗?

推荐答案

这个代码片段避免了css试图"决定"它是奇数元素还是偶数元素的问题,并且不需要额外的JS.

它只是将背景作为线性渐变,重复地放置在父元素上.

同样,边界半径也在父元素上,因此不需要计算元素是否为最后一个可见元素:

* {
  margin: 0;
}

.spreadsheet {
  display: grid;
  grid-auto-flow: row;
  border-radius: 1rem;
  background-image: linear-gradient(#fafafa 0 2rem, #eaeaea 2rem 4rem);
  background-size: 100% 4rem;
}

.spreadsheet>div {
  padding: 0.5rem;
  line-height: 1rem;
}
<div class="spreadsheet">
  <div>Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div>Line 4</div>
  <div>Line 5</div>
  <div>Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>
With some children display: none;
<div class="spreadsheet">
  <div style="display: none;">Line 1</div>
  <div>Line 2</div>
  <div>Line 3</div>
  <div style="display: none;">Line 4</div>
  <div>Line 5</div>
  <div style="display: none;">Line 6</div>
  <div>Line 7</div>
  <div>Line 8</div>
  <div>Line 9</div>
</div>

但是请注意(正如@omethinghere所指出的),这只适用于单行条目--例如,您可能会在足够宽的视区中从简单的Electron 表格中获得这些条目.

Html相关问答推荐

springBoot + Thymeleaf:属性中的UTF-8

如何才能只给没有孟加拉语Kar符号的字母上色?

将网格包装在css中

如何防止第二列中的内容影响第一列中内容的位置?

在窄屏幕上显示表格,每个单元格占一行

try 更新我的Node.js应用程序中的用户,但我收到错误.然而,当我使用postman 时,更新工作正常

减小HTML负载中base64字符串的大小

如何在黑暗模式切换中添加图标

如何根据剪辑路径边缘设置文本边距?

我在桌面版 html css 代码上给出了位置 margin-left margin-top 标签,如何将其删除到手机上以使其响应?

如何检测输入字段是否没有必填且没有占位符?

Bootstrap 轮播内容在箭头之间不显示

您如何动态更改 Vue 中更高级别的标签的 CSS

透明渐变凹矩形

两部分问题 - 是什么导致了这个空白?

为什么在使用src与srcdoc时 iframe 内容高度呈现不同?

如何将图像移动到父容器的右侧?

暗模式转换器

列宽等于最宽列宽度的无界容器

使用 htmloutput 在shiny 的应用程序中呈现文本