Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nth-child for styling with a table with rowspan?

I have a table that has one row that uses rowspan. So,

<table>  <tr>   <td>...</td><td>...</td><td>...</td>  </tr>  <tr>   <td rowspan="2">...</td><td>...</td><td>...</td>  </tr>  <tr>               <td>...</td><td>...</td>  </tr>  <tr>   <td>...</td><td>...</td><td>...</td>  </tr> </table> 

I'd like to use the nth-child pseudo-class to add a background color to every other row, but the rowspan is messing it up; it adds the background color to the row below the row with the rowspan, when in fact I'd like it to skip that row, and move onto the next.

Is there a way to get nth-child to do something smart, or to use [rowspan] in the selector to get around this? So in this case, I'd like the background color to be on rows 1 and 4 but then after that on 6, 8, 10, and so on (since none of those have rowspans)? It's like if I see a rowspan, then I want nth-child to add 1 to n and then continue.

Probably no solution to this, but thought I'd ask :-)

like image 589
Elisabeth Avatar asked Apr 18 '12 00:04

Elisabeth


People also ask

How do you use the nth child table?

Definition and UsageThe :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

How do you target the nth child in SCSS?

Writing Complex :nth-child() Selectors It works exactly the same as :nth-child except that it starts from the end of the parent. For example, you can select the first three children of a parent by using the selector :nth-child(-n + 3) . You can use :nth-last-child(-n + 3) to select the last three.

When can you use Nth child?

The :nth-child selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

What is the difference between the nth child () and Nth of type () selectors?

As a general rule, if you want to select an interval of a selector regardless of the type of element it is, use nth-child . However, if you want to select a specific type only and apply an interval selection from there, use nth-of-type .


1 Answers

What seems to work for me is to put a td in the row below with display:none

<table>    <tr>       <td rowspan="2">2 rows</td>       <td>1 row</td>    </tr>    <tr>       <td style="display:none"></td>       <td>1 row</td>    </tr> </table> 
like image 99
Sebastian Forster Avatar answered Sep 22 '22 19:09

Sebastian Forster