Skip to content

Instantly share code, notes, and snippets.

@adamliptrot-oc
Last active August 21, 2020 21:38
Show Gist options
  • Save adamliptrot-oc/d44f2cff20e364277da68166ed869c11 to your computer and use it in GitHub Desktop.
Save adamliptrot-oc/d44f2cff20e364277da68166ed869c11 to your computer and use it in GitHub Desktop.
This allows a table to change from a standard table on larger screens to a stacked set of cells on smaller screens

To modify a standard table to use this:

  • add _table.scss to your service css
  • add role="table" to table element
  • add role="rowgroup" to thead element
  • add role="row" to each row element (including the one containing the headings)
  • add role="columnheader" to the th elements
  • add role="cell" to the td elements
  • add <span class="hmrc-responsive-table__heading" aria-hidden="true">[heading]</span> to each cell, with the [heading] being the column it sits under.

Example markup:

<table class="govuk-table hmrc-responsive-table" role="table">
  <thead class="govuk-table__head" role="rowgroup">
     <tr class="govuk-table__row" role="row">
         <th scope="col" class="govuk-table__header" role="columnheader">Title</th>
         <th scope="col" class="govuk-table__header" role="columnheader">Last updated</th>
         <th scope="col" class="govuk-table__header" role="columnheader">Status</th>
     </tr>
  </thead>

  <tbody class="govuk-table__body">
      <tr class="govuk-table__row" role="row">
         <td class="govuk-table__cell" role="cell">
             <span class="hmrc-responsive-table__heading" aria-hidden="true">Title</span>
             This is a title
         </td>
         <td class="govuk-table__cell" role="cell">
             <span class="hmrc-responsive-table__heading" aria-hidden="true">Last updated</span>
             04 August 2020
         </td>
         <td class="govuk-table__cell" role="cell">
             <span class="hmrc-responsive-table__heading" aria-hidden="true">Status</span>
             Completed
         </td>
      </tr>
  </tbody>
</table>
$mobile: 768px; //change this to suit your content
$color_hmrc-grey-4: #b1b4b6;
.hmrc-responsive-table {
margin-bottom: 0;
width: 100%;
thead {
border: 0;
clip: rect(0 0 0 0);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: 0;
overflow: hidden;
padding: 0;
position: absolute;
white-space: nowrap;
width: 1px;
@media (min-width: $mobile) {
clip: auto;
-webkit-clip-path: none;
clip-path: none;
display: table-header-group;
height: auto;
overflow: auto;
position: relative;
width: auto;
}
}
tbody {
tr {
display: block;
margin-bottom: 1.5em;
&:last-child {
margin-bottom: 0;
}
@media (max-width: $mobile) {
padding: 0 0.5em;
}
@media (min-width: $mobile) {
display: table-row;
}
th {
text-align: right;
@media (min-width: $mobile) {
text-align: left;
}
}
td {
display: block; // For browsers that don't support flexbox
display: flex;
justify-content: space-between;
min-width: 1px;
text-align: right;
@media (min-width: $mobile) {
display: table-cell;
text-align: left;
}
@media (max-width: $mobile) {
padding-right: 0;
&:last-child {
border-bottom: 3px solid $color_hmrc-grey-4;
}
}
}
}
}
}
.hmrc-responsive-table__heading {
font-weight: 700;
padding-right: 1em;
text-align: left;
word-break: initial;
@media (min-width: $mobile) {
display: none;
}
}
.hmrc-responsive-table__data--wrap-mobile {
@media (max-width: $mobile) {
word-break: break-all;
}
}
.hmrc-responsive-table__data--wrap-desktop {
@media (min-width: $mobile) {
word-break: break-all;
}
}
.hmrc-reponsive-table__numeric {
@media (min-width: $mobile) {
text-align: right;
}
}
.table-foot {
border-bottom: 1px solid #b1b4b6;
display: flex;
font-size: 19px;
font-weight: 700;
justify-content: space-between;
padding: 0.75em 0 0.75em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment