How to retain "zebra-striped" table after hiding rows with CSS [duplicate] - css

This question already has answers here:
Can I combine :nth-child() or :nth-of-type() with an arbitrary selector?
(8 answers)
Closed 2 years ago.
I have a table (with id table) where each row has the attribute data-tagged, when created (by JS) it is set to "true", and can be set to "false" by various functions. I am using the following CSS to hide rows for which the data-tagged attribute is "false":
#table tr[data-tagged="false"] {
display: none;
}
And I was using the following CSS to produce a 'zebra-striped' effect, i.e: giving alternating rows a different background colour:
#table tr:nth-child(even) {
background-color: var(--other-background);
}
...until I realised that the row's colours were unchanged as other rows were hidden and un-hidden, which makes sense, since it's still only the even children that are affected by the CSS, so I tried the following selector instead:
#table tr[data-tagged="true"]:nth-of-type(even) { ... }
...thinking that this would only affect every other tr with attribute data-tagged equal to "true" (the desired outcome!), but I was wrong, and it made no difference. Is this not do-able in the CSS alone, or is there a solution that I'm not seeing?

Maybe try:
#table tr:not([data-tagged="false"]):nth-child(even){background:#fff;}
#table tr:not([data-tagged="false"]):nth-child(odd){background:#eee;}

Related

What's data-sizing in css selector here? [duplicate]

This question already has answers here:
Select elements by attribute in CSS
(6 answers)
Closed 1 year ago.
Here's the link to css code I am curious about : https://codepen.io/charlesxiao/pen/NWjgQQm.
Do you know what does the following css code means?
.awesome[data-sizing="intrinsic"] {
width: min-content;
}
What's this data-sizing attribute? I can't find it anywhere.
Thanks!
Much like how your selectors can target classes (.class) and ids (#id), your CSS can also target attributes, including data-*. It's common practice for javascript to target data-* attributes rather than going through the rigmarole of adding/removing classes. There's some particulars choosing between the two.
width: min-content; simply sets the element to the smallest possible size — the word "awesome" is the largest element and that's used as the width.

How to combine multiple selectors for the same rule [duplicate]

This question already has answers here:
What do commas and spaces in multiple classes mean in CSS?
(9 answers)
Closed 3 years ago.
I can't combine these selectors into a single CSS rule. I'm trying to edit the width of certain form fields.
input#input_9_2 {
max-width: 500px;
}
input#input_9_3 {
max-width: 500px;
}
When I try to combine them, the rule does not apply to either. But they work when written separately, as above.
To group CSS selectors in a style sheet, you use commas to separate multiple grouped selectors in the style. In this example, the style affects two classes input#input_9_2 and input#input_9_3.
input#input_9_2,
input#input_9_3
{
max-width: 500px;
}
The comma means "and", so this selector applies to all input#input_9_2 elements and input#input_9_3 elements. If the comma were missing, the selector would instead apply to all input#input_9_3 elements that are a child of an input#input_9_2. That is a different kind of selector, so the comma is important.
Any form of the selector can be grouped with any other selector.
use a selector list
input#input_9_2, input#input_9_3 {
max-width: 500px;
}

Complex Table with Rowspan Hover and Zebra effect

I'm trying to make a hover effect for a table with multiple rowspan but I don't manage to make it fully work.
The css as described in another stackoverflow is not working (see solution here https://codepen.io/cimmanon/pen/KqoCs ).
The example here (rowspan on multiple columns) : https://codepen.io/anon/pen/rJXgzW
The hover css effect is defined as :
tbody:hover td[rowspan], tr:hover td {
background: red;
}
Any suggestions?
The trick in the working example is to use multiple <tbody> elements in the table where each table body contains one table cell spanning multiple rows. That way
tbody:hover td[rowspan] { background: red; }
makes it magically appear as requested. This doesn't work with the second example in the same way, as there are (1) multiple row-spanning elements and (2) it's using <th> elements (which is easy to address, though).
To get it working using CSS only, you would need to nest tables inside table cells.

How do you group similar n-th child selectors? [duplicate]

This question already has answers here:
Specifying a list of arbitrary children (no pattern) for nth-child and nth-of-type
(2 answers)
Closed 6 years ago.
How can I sum these css selectors together?
td:nth-child(1), td:nth-child(4), td:nth-child(5) {
font-size: 25px;
}
I thought of something like
td:nth-child(1,4,5) {
font-size: 25px;
}
but this doesnt work. Is there a way to condense this?
If you can't use a an+b formula to target them (which is the case in your particular example), then the code you wrote is the shortest version.
If you wish to simplify the CSS code, you could switch to classes... that however will make your HTML less clean.

How do I select every other row in an HTML table using *CSS2*, not CSS3? [duplicate]

This question already has answers here:
Alternate table row color using CSS?
(11 answers)
Closed 9 years ago.
How do I select every other row in an HTML table using CSS2? If that is not possible, an answer for CSS3 is welcome as well.
Sadly there is no solution purely using CSS2.
You can, however, use :odd and :even selectors in CSS3 to determine every row.
tr:nth-child(even) {
// if it's even - rows 2,4,6 etc - apply styles
}
tr:nth-child(odd) {
// if it's odd - rows 1,3,5 etc - apply styles
}
nth-child even/odd is supported in all major browsers, but not in IE8 and before.
If you want a way to make it work for IE8 and earlier, then check out this article on making nth-child work everywhere.
tr:nth-child(even) {
/* stub */
}
or
tr:nth-child(odd) {
/* stub */
}
See here for browser support

Resources