I have a table, where data is hidden or shown based on whether the checkbox is checked or unchecked. here is the fiddle : https://jsfiddle.net/2xw3tsrh/2/
I am trying to bring the checkbox inside 'td' , right infront of "Click me" text. I cannot use 'data-label' or, use an id for each row to traverse and use something "input#row1:checked". I dont want to hide the checkbox and tie the label text to the checkbox. Is there a way to achieve it ?
Thanks in advance.
Code :
body {
margin: 0;
padding: 0;
min-width: 100%;
width: 100%;
max-width: 100%;
min-height: 100%;
height: 100%;
max-height: 100%;
background: rgb(231, 207, 192);
min-height: 100vh;
}
@charset "UTF-8";
/* #row1,#row2 {
display: none;
} */
.tab-label {
/* display: flex;
justify-content: space-between;
padding: 1em; */
background: #b9ce44;
font-weight: bold;
/* cursor: pointer; */
/* Icon */
}
/* .tab-label:hover {
background: #1a252f;
} */
/* .tab-label::after {
content: "❯";
width: 1em;
height: 1em;
text-align: center;
transition: all 0.35s;
} */
.tab-content {
overflow: hidden;
max-height: 0;
padding: 0 1em;
color: #2c3e50;
background: white;
transition: all 0.35s;
}
/* input:checked ~ .tab-label {
background: #1a252f;
} */
/* input:checked ~ .tab-label::after {
transform: rotate(90deg);
} */
input:checked~.tab-content {
max-height: 100vh;
padding: 1em;
}
<div id="page-wrap" style="margin: 50px;background: cornflowerblue;">
<h1 style="margin: 0;line-height: 3;text-align: center;font: 30px/1.4 Georgia, Serif;">Table</h1>
<table role="presentation" style="width: 100%;border-collapse: collapse;">
<thead>
<tr>
<th rowspan="2" colspan="1" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="1" colspan="4" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="1" colspan="4" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="1" colspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
<tr>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
</thead>
<tbody>
<!-- Ist Row -->
<tr>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">
<label class="tab-label" for="row1"> (Bring checkbox here) Click Me</label>
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
</tr>
<!-- Ist accordion -->
<tr>
<td colspan="11" style="border: solid 1px white;text-align: center;padding: 0;">
<input id="row1" type="checkbox">
<div class="tab-content">
<table role="presentation" style="border-collapse: collapse;margin: 10px auto;background-color: aqua;">
<thead>
<tr>
<th rowspan="2" colspan="1" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="1" colspan="4" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<!-- <th rowspan="1" colspan="4"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th> -->
<th rowspan="1" colspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
<tr>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<!-- <th rowspan="2"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th> -->
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
</thead>
<tr>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<!-- <td style="padding: 6px;border: 1px solid #ccc;text-align: center;">Athens</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">Edlund, Ben
(July 1996).</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td> -->
</tr>
</table>
</div>
</td>
</tr>
<!-- 2nd Row -->
<tr>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">
<label class="tab-label" for="row2">Click Me</label>
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A</td>
</tr>
<!-- 2nd accordion -->
<tr>
<td colspan="11" style="border: solid 1px white;text-align: center;padding: 0;">
<input id="row2" type="checkbox">
<div class="tab-content">
<table role="presentation" style="border-collapse: collapse;margin: 10px auto;background-color: aqua;">
<thead>
<tr>
<th rowspan="2" colspan="1" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="1" colspan="4" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<!-- <th rowspan="1" colspan="4"
style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th> -->
<th rowspan="1" colspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
<tr>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
<th rowspan="2" style="background: #333;color: white;font-weight: bold;padding: 6px;border: 1px solid #ccc;text-align: center;">
Header</th>
</tr>
</thead>
<tr>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
<td style="padding: 6px;border: 1px solid #ccc;text-align: center;">N/A
</td>
</tr>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</div>
Update:
I also tried to take ispiration from here : Change checked html table row with CSS to move the "checkbox" right outside the row of first 'td' to achieve a similar functionality - that didnt work out as well as it messed up the table.
~in CSS to apply CSS to a sibling element to that checkbox. If you move the checkbox somewhere else, there is no way to select the element containing the data. The element containing the data would essentially need to be a child or sibling of the checkbox.<div>containing the table and the data, so a sibling selector~with descendant selectors to traverse the table structure will work. Per your question, putting it beside the label does not allow the checkbox and the data to be siblings in any way as they do not share a parent or descendants.