Building upon the code provided by @king neo, I attempted to use box-shadow: inset and ::before to create a second line that is separate from the border itself. However, my efforts were unsuccessful as the bottom-border got stuck at the corners when it met the border-left and border-right.
The only solution I could come up with was to utilize a background with a gradient. Although this creates a fake border and any elements inside the td cell will appear on top of it. To address this issue, padding can be added, but if padding is only added on selection, the table will jump around. Therefore, padding must also be added on non-selected rows.
In addition, I made use of CSS variables for easy customization of multiple elements in one place.
var prevRow = null;
function toggle(it) {
if (it.className.substring(0, 3) == "sel")
{
it.className = it.className.substring(3, 6);
prevRow = null;
}
else
{
it.className = "sel" + it.className;
if (prevRow != null)
{
prevRow.className = prevRow.className.substring(3, 6);
}
prevRow = it;
}
}
:root {
--color-cyan: #00ffff;
--color-yellow: #ffff00;
--border-width: 2px;
}
tr:nth-child(odd) {
background-color: lightblue;
}
tr:nth-child(even) {
background-color: lightgrey;
}
td {
padding: var(--border-width);
}
.selodd > td,
.selevn > td {
--yellow-end-point: var(--border-width);
--cyan-end-point: calc(2 * var(--border-width));
--transparent-starting-point: calc(3 * var(--border-width));
background: linear-gradient(to top,
var(--color-yellow) var(--yellow-end-point),
var(--color-cyan) var(--cyan-end-point),
transparent var(--transparent-starting-point));
background-repeat: no-repeat;
}
<table border>
<tr class="odd" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr class="evn" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr class="odd" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr class="evn" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr class="odd" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr class="evn" onclick="toggle(this)">
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
</table>