I've been using CSS
for stuff like this for a while, but it seems this one specific scenario doesn't work how it should.
What I want to have a list like you would in a spreadsheet, with each line alternating from a light grey to a slightly darker grey.
<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>
Everything above works just fine, as anticipated. Now here's my issue... In my scenario, I'm using JavaScript
to change the classes of certain lines, based on a filter. With this, I simply want to set the display
of lines that are filtered out to none
, whilst keeping the same pattern of alternating colors as before. This should also render the first and last lines to have rounded borders.
<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>
You'll notice, Line 1
is hidden
, so Line 2
should have a background-color
of #fafafa
and a border-radius
of 1rem 1rem 0 0
, but it has neither. This is very obvious when the same colored lines are right next to each other.
Any ideas on how I can accomplish this?
span
, place it where theli
you want to remove is, and then add thelit
in there. Then use> *:nth-of-type
selectors so you only count theli
. It changes nothing to your list in essense, and all you need to do to show something is reinsert theli
where the span is and remove the span. Just an idea though.even
orodd
class to the visible elements when you're in there. No need to remove anything from the DOM.