Striped table

Hello there, are there any better way of doing a striped table? this works but I feel like I have overcomplicated it https://codepen.io/Etsi0/pen/XWvXoeM
17 Replies
ἔρως
ἔρως3w ago
what are you even trying to do? im not reading 2kb of selectors to understand what it is trying to do, im sorry
Etsi0
Etsi03w ago
i'm making a striped table and it works like it should, but i feel like i have overcomplicated it
ἔρως
ἔρως3w ago
from the result, it seems like you want to apply the same striping to the thead and tbody, but you want to continue the same stripping order too
Etsi0
Etsi03w ago
yes, exactly
ἔρως
ἔρως3w ago
why have a thead then? for the bold and centered text?
Etsi0
Etsi03w ago
idk, i was just thinking that it was maybe more semantically correct :idkpepe:
ἔρως
ἔρως3w ago
it is but usually the thead doesnt have the same background color as the stripping because its confusing as hell
Jochem
Jochem3w ago
you can usually assume a thead has a single row in it too
ἔρως
ἔρως3w ago
indeed and when it has multiple, the background is the same
Jochem
Jochem3w ago
so you can just set the color for the thead however you want, and then start the alternating colors underneath for the tbody with the other color yup. Cause the thead should be visually distinct
ἔρως
ἔρως3w ago
actually, you dont need that you can just set a background color and give the odd/even a background separately so, basically, you need 3 selectors: - table - background of the odds comes here - table > thead - background of all the thead rows - table > tbody > tr:nth-child(even) - the stipped background comes here HOWEVER.... you should use this instead, for browsers that support it: table > tbody > tr:nth-child(even of :not([hidden])) this lets you hide rows with the hidden attribute and the stripping continues
Etsi0
Etsi03w ago
good to know, thanks for the tips
ἔρως
ἔρως3w ago
you're welcome
Choo♚𝕂𝕚𝕟𝕘
That could be simplified very substantially to this:
thead tr:nth-child(odd),
tbody tr:nth-child(even) {
background-color: #f0f0f0;
}

thead tr:nth-child(even),
tbody tr:nth-child(odd) {
background-color: #f9f9f9;
}
thead tr:nth-child(odd),
tbody tr:nth-child(even) {
background-color: #f0f0f0;
}

thead tr:nth-child(even),
tbody tr:nth-child(odd) {
background-color: #f9f9f9;
}
Choo♚𝕂𝕚𝕟𝕘
The thead only has one row, so that could be simplified even more, but I kept that original part from your code due to laziness.
ἔρως
ἔρως3w ago
that is smart but you know what is even smarter? replace the first selector with table the default background is transparent, and this way, you are making use of that and only style what needs to be styled but yeah, well done
Want results from more Discord servers?
Add your server