Paul Bradley • Solutions Architect & Software Developer


Published:

Responsive HTML Tables using Flex with minimal CSS

How I'm making a table heavy web application mobile responsive using Flex and minimal CSS

Introduction

I’m making a concerted effort to make the health application I work on mobile responsive. The application is table heavy as it lists all sorts of results in tables. Patient lists, Covid results, published documents and more. I wanted to make all the tables mobile responsive so that they looked good on mobile phones and tablets. The driver for this work is we’re opening up the application to non NHS staff who may want to access the data on the go in the patients home using a mobile device.

Here is sample of a patient listing. This is how it would look on a desktop computer. The table styling uses the Bootstrap CSS framework.

...

With the addition of minimal CSS and using the flex properties we can reformat the layout on smaller screens. This is how the table appears on mobile devices. The CSS hides the table headings and the column names appear within each cell as a small label.

...

The HTML

Below is an example table. The classes on the table tag are Bootstrap stylings. Within each td tag that we want a label to appear on the small screen version, we add the label naming within a span tag. The cell value is then wrapped within a normal paragraph tag. This allows us to style each of the components.

<table class="table table-condensed table-hover">
    <tr>
        <th width="70">Discharge</th>
        <th width="160">NHS Number</th>
        <th width="70">DOB</th>
        <th >Patient</th>
        <th width="170">Access</th>
    </tr>
    <tr>
        <td><button class="btn btn-primary" type="submit">Remove</button></td>
        <td><span>NHS#</span> <p>999 055 8108</p></td>
        <td><span>DOB#</span> <p>12.07.1995</p></td>
        <td><span>Patient</span> <p>Bradley, Paul</p></td>
        <td><button class="btn btn-primary" type="submit">Open Patient</button></td>
    </tr>
</table>

The CSS

Below is the commented minimal CSS added to the applications style sheet which makes all tables mobile responsive. The key bits are switching the tables row display properties to flex and adding the flex-wrap statement.

/*
   default styling for the span within
   the table td's on screens wider than
   800px these are hidden and only made
   visible when the media query hits 800px
*/
table td span {
    background: #eee;
    color: dimgrey;
    display: none;
    font-size: 11px;
    font-weight: bold;
    padding: 5px;
    position: absolute;
    text-transform: uppercase;
    top: 0;
    left: 0;
}

@media(max-width: 800px) {
    table tr th {
        /* hide the table headers when on smaller screens */
        left: -9999px;
        position: absolute;
        visibility: hidden;
    }
    table tr {
        /* reformat the table rows */
        border-bottom: 0;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        margin-bottom: 40px;
    }
    table td {
        /* reformat the table columns */
        border: 1px solid dimgrey;
        margin: 0 -1px -1px 0;
        position: relative;
        width: 50%;
    }
    table td p {
        /* reformat the table cell values */
        font-size: 15px !important;
        margin: 0;
        padding: 25px 0 0 0 !important;
    }
    table td .btn {
        /* reformat any buttons within the table */
        font-size: 15px !important;
    }
    table td span {
        display: block;
    }
}