I am trying to solve a problem without using JavaScript loops. I want to apply css rules to table cells. I struggle with applying the rules before my start cell.
I found this question Select all elements before element with class? I struggle with the implementation of this technique as my code would need a more sophisticated check. Especially when thinking of dealing with two bookings before the start.
If I could find the last booking before the start I could use the technique from that question.
Does anyone have an idea how to solve this without javascript?
table {
margin: auto;
width: 100%;
border: thick groove black;
border-radius: 5px;
text-align: center;
}
td {
border: thin groove gray;
width: 10vw;
height: 5vh;
}
td:hover {
cursor: pointer;
}
td.start {
background-color: green;
}
td.booked {
background-color: orange;
cursor: not-allowed;
}
td.start ~ td:not(.booked) {
background-color: lightblue;
}
td.start ~ td ~ td.booked:hover {
cursor: not-allowed;
}
td.start ~ td ~ td.booked ~ td {
background-color: red;
}
td.start ~ td ~ td.booked ~ td:hover {
cursor: not-allowed;
}
<br />
<table>
<thead>
<tr>
<td>Invalid</td>
<td>Invalid</td>
<td>Booked</td>
<td>OK</td>
<td>OK</td>
<td>Start</td>
<td>OK</td>
<td>OK</td>
<td>Booked</td>
<td>Invalid</td>
<td>Invalid</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td class="booked"></td>
<td></td>
<td></td>
<td class="start"></td>
<td></td>
<td></td>
<td class="booked"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p
style="text-align:center; border: thin groove gray; border-radius:5px; margin: 1.25rem 4vw;
padding: 1.25rem;
"
>
Relative to th start cell, cells after a booking are not allowed. Cells before a booking are allowed.
<br />
<br />
We can select the cells after the start cell with: <br /><br />
<code
style="background-color:lightgray; padding:.75rem;
border-radius: 5px
"
>td.start ~ td:not(.booked) { background-color: lightblue; }</code
>
<br />
<br />
We can select the cells after the booked cell after the start cell with:
<br /><br />
<code
style="background-color:lightgray; padding:.75rem;
border-radius: 5px
"
>td.start ~ td ~ td.booked ~ td { background-color: red; }</code
>
<br />
<br />
<br />
<strong>
Q. How can we select the cells before the start cell and apply the rules ?</strong
>
<br />
</p>