Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best hide a table column for mobile view

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; }
      }
like image 236
IEnumerable Avatar asked Jul 24 '13 15:07

IEnumerable


People also ask

How do you hide a table column in CSS?

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.

How do you fold columns into rows in mobile devices?

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.


1 Answers

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;
        }
    }
like image 191
Zubzob Avatar answered Oct 26 '22 13:10

Zubzob