0

Question

Is there a clean way to apply a style for all sibling elements between two HTML elements?

Background

I have a table with bootstrap's .table-striped class, however, I want an arbitrary number of rows to be striped together as a group. My solution was to create a custom element using

document.registerElement("seq-tr");

and extend the tr:nth-child(odd/even) to tr:nth-of-type(odd/even), as well as to ...tr:nth-of-type(odd/even) + seq-tr:

.table.table-striped {
    > thead, tbody, tfoot {
        > tr {
            ~ seq-tr > td {
                border-top: none;
                padding-top: 0;
                white-space: nowrap;
            }

            &:nth-of-type(even) {
                &, + seq-tr {
                    background-color: @table-bg;
                }
            }

            &:nth-of-type(odd) {
                &, + seq-tr {
                    &:extend(.table-striped > tbody > tr:nth-child(odd));

                    > td:extend(.table-striped > tbody > tr:nth-child(odd) > td) {
                    }
                }
            }
        }
    }
}

Aside: Originally I had tried using the ~ selector instead of +, but that applied both :nth-of-type(even) and :nth-of-type(odd) to every <seq-tr> after the second row and whichever was later in the compiled CSS file took precedent, rather than being smart about it and looking for whether the closest sibling <tr> was even or odd.

So the above code works for the first <seq-tr> element, but for the following <seq-tr>s, it does not.

Is there a clever way to make this work for any number of consecutive <seq-tr>s?

I could use a seq-tr.striped class, but I would rather not.

Edit

It just occurred to me I could simply use multiple <tbody> elements, and style the rows based on even/oddness of those, rather than the rows themselves.

Can we have multiple <tbody> in same <table>?

I'd still like the answer to my question for other purposes, but it's less pressing now.

Community
  • 1
  • 1
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
  • 2
    The potential advantage of the `tbody` option is that it may enhance accessibility. – DA. Feb 06 '15 at 20:16
  • 1
    as far as i understand there is not wrong with your `&:nth-of-type(even) { &, + seq-tr {`, also see: http://codepen.io/anon/pen/YPExMZ. So possible some BS table styles with a higher specificity override you styles? Could you create a live example which shows the issue? – Bass Jobsen Feb 06 '15 at 22:01

0 Answers0