0

I have the following HTML:

<table>
    <tr>
        <td class="FINAL">The first cell.</td>
        <td class="FINAL">The second cell.</td>
        <td class="DRAFT">The third cell.</td>
        <td class="DRAFT">The fourth cell.</td>
    </tr>
    <!-- more rows with the same cell setup -->
</table>

I want to accomplish the following:

  • The number of FINAL cells is variable, from 0 to unknown.
  • The number of DRAFT cells is variable, from 0 to unknown.
  • But there is always at least one cell (i.e. of either FINAL or DRAFT class)
  • Adding an additional class to mark the last FINAL (as suggested in the duplication reference) is currently impossible.
  • Must be a CSS-only solution.
  • Select the last p element with the class "FINAL", and style it.
  • Add a border between the last FINAL and the first DRAFT cell.

My naive CSS beginner's approach to this was the following selector:

td[class~=FINAL]:last-of-type {
    // CSS styles ehre
}

but that doesn't select anything except when I indeed have a FINAL td that is the last of its type in a table row.

https://stackoverflow.com/a/8539107/6000494 describes an interesting approach, but that doesn't fully apply here since I cannot guarantee that there will always be a first td element present.

SOLUTION:

The following selector does the trick (https://stackoverflow.com/a/33859457/6000494 pointed me to it):

.FINAL + :not(.FINAL) {
    border-left: 1px dotted black;
}

Update:

Edited the original description to a table context, which makes the suggested solution not work (AFAIK). Also clarified some of the constraints.

Update 2 The referenced question has an answer that - albeit not obvious to me - actually solves my problem: https://stackoverflow.com/a/33859457/6000494

A working jsFiddle demonstrates it.

Final update: Described the solution in the question. Can't mark the question as solved myself somehow...

Community
  • 1
  • 1
MDr
  • 101
  • 2
  • 10
  • I dont agree. all of the 4 "duplicate" answers are just workaround; not a final/definitive/good-enought solution. – L777 Mar 01 '16 at 03:40
  • ofcourse, this is not the duplicate – Nagaraja Thangavelu Mar 01 '16 at 03:52
  • “last-of-type” only refers to the element, not the attributes. One possible solution is add a div container for FINAL elements and DRAFT elements and then apply the style. Like

    Item 1

    Item 2>

    Item 3

    Item 4>

    and add the CSS sytle to it like .FINAL p:last-of-type { //css } Working JSFiddle: http://jsfiddle.net/67y0hqj3/
    – Nagaraja Thangavelu Mar 01 '16 at 03:59
  • The answer that you link to only applies to the use case of matching the first element - not the last. There is no similar solution for matching the last element. I could post an answer to the duplicate question, saying "there is no CSS-only solution", but that's about it. – BoltClock Mar 01 '16 at 10:26
  • @BoltClock I know that. :-) I referred to that answer since it might have come up as a solution (which it isn't). – MDr Mar 01 '16 at 10:31
  • The fiddle shows the first .DRAFT element being styled, as opposed to the last .FINAL as asked. – BoltClock Mar 01 '16 at 11:02
  • That's correct - I think my question was too unspecific from the beginning. I am new to posting questions on SO :) – MDr Mar 01 '16 at 11:07
  • I can reopen the question for you to post an answer yourself if you like. You will be able to mark it accepted in 2 days. – BoltClock Mar 01 '16 at 11:17

0 Answers0