I have a HTML table, in a mobile browser I need to hide some of its columns.
I want to hide 2 or 3 of these cols using @media
queries if possible.
the below code didn't work for me:
<table>
<tr>
<td></td>
<td class='collapsable'></td>
<td class='collapsable'></td>
<td></td>
</tr>
</table>
//hide cols here
td.collapsable {display:block; }
@media only screen and (min-width: 450px) {
//show cols here
td.collapsable {display:none; }
}
Usually, to hide an element from view, you use the 'display' property and set it to 'none'. But CSS also has a property called 'visibility', which hides elements in a different way. In particular, we use 'visibility: collapse' here, which is designed especially for hiding table columns and rows.
The solution involves making table cells display: block on mobile devices, and adding a data-* attribute to each cell, matching the column name. This data attribute is injected in the cell's ::before pseudo-element with content: attr() . You'll need to add some extra float to make it pretty.
Something like this? (try resizing the Result window/pane)
The HTML:
<tr>
<td class="one">corpse</td>
<td>corpse</td>
<td>corpse</td>
</tr>
The CSS:
table{
width: 50%;
}
td, th{
border: 1px solid orange;
}
@media only screen and (max-width: 900px) {
.one{
display: none;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With