Toggle Table Data with CSS

Copy Link
Copied!

The Problem

You need to design a table that displays its data in two different units. You want to avoid displaying both sets at once.

I came across this little problem while working on an e-commerce website. The client wanted to display the measurements in their size guide in both centimetres and inches.

The user shouldn’t have to convert the measurements to their unit of preference. And displaying the measurements in both units at once wasn’t a very desirable option. Why should the user go through both units when they only need to see the one they prefer? It’s our job to improve their experience after all.

The Solution

Give the user the option to toggle between both units. If they want to peruse the size guide in centimetres, they can. If their American friend next to them needs to see the measurements in inches, they are just a click/tap away.

Using JavaScript is an obvious option. And given many online stores rely on JavaScript for critical functions (adding items to cart, checkout process, etc), a non-JavaScript method may not seem necessary here. It is possible nonetheless.

Update: based on Heydon Pickering's tweet, this method is not great for accessibility. Proceed with caution!

The method is really simple. We'll rely on a checkbox's state .mycheckbox:checked and use CSS's general sibling selector to target the measurements we want to hide/display.

Here is what we're after:

Width (A) Length (B) Sleeve (C)
Small 20 cm 8 inch 28 cm 11 inch 12 cm 5 inch
Large 32 cm 12.5 inch 40 cm 16 inch 22 cm 9 inch
X-Large 42 cm 16 inch 50 cm 20 inch 32 cm 12.5 inch

These guidelines have been issued as a guide only. If you have a specific sizing question or would like more information, please contact us. View it on Codepen.

The Code

A size guide is typically presented in a table. The markup for the table here is pretty standard. We'll just include two spans (one for each measurement unit) inside the table's tds.

In order to be able to target these spans with the general sibling selector and based on the checkbox's state .checkbox:checked ~ table span, we need to make the checkbox and the table siblings (i.e. have the same parent element). We also need to place the checkbox before the table. The checkbox doesn't have to be placed directly before the table though.

"The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent."

Simplified markup

<input type="checkbox" id="toggle">
<table>
  <tbody>
    <tr>
      <td>Small</td>
      <td>
        <span class="default">20 cm</span>
        <span class="alt">8 inch</span>
      </td>
      <td>
        <span class="default">28 cm</span>
        <span class="alt">11 inch</span>
      </td>
    </tr>
  </tbody>
</table>

CSS to toggle spans

table .default
{
  display: inline;
}

table .alt
{
  display: none;
}

/*checked*/
.checkbox:checked ~ table .default
{
  display: none;
}

.checkbox:checked ~ table .alt
{
  display: inline;
}
Width Length Sleeve
Small 20 cm 8 inch 28 cm 11 inch 12 cm 5 inch
Large 32 cm 12.5 inch 40 cm 16 inch 22 cm 9 inch
X-Large 42 cm 16 inch 50 cm 20 inch 32 cm 12.5 inch

Now it functions as intended. It's a table with a checkbox that toggles the table's data.

By default: we leave the checkbox unchecked and hide .alt spans with CSS. When checked, we hide the .default spans and display the .alt spans

Now all there's left to do is to customise our checkbox to make it look like a toggle switch and add clear labelling. We want these elements next to each other in this order:

  1. Main label
  2. First (default) measurement unit label
  3. Checkbox/custom toggle switch
  4. Alternative measurement unit label

There are many examples of custom switches on CodePen. That's a good starting place to get some inspiration.

When coding your own remember this:

Firstly, (as far as I know) you can't use pseudo elements on input[type=checkbox]. In my example, I'm using an empty span for my switch.

The other thing to keep in mind is while you want to hide the checkbox you need it to remain clickable. You want the user to be actually clicking the checkbox when they click on the measurement unit label they want to display. The trick is to set the opacity of the checkbox to 0 (makes it invisible, but remains clickable) and absolutely position it to overlap with the measurement units labels. You may also need to set the checkbox's z-index to a higher value to ensure it's on top (and thus remains clickable).

<label for="toggle" class="toggle__main-label">Measurements in: </label>
<label for="toggle" class="toggle__option">CM</label>
<input type="checkbox" class="toggle__checkbox" id="toggle">
<span class="toggle__switch"></span>
<label for="toggle" class="toggle__option">INCHES</label>
.toggle__main-label
{
  display: inline-block;
  vertical-align: middle;
  margin-right: 10px;
}

.toggle__checkbox
{
  opacity: 0; /*invisible but it's there (we need to keep it clickable)*/
  width: 100%;
  z-index: 99; /*make sure it's on top so it's clickable*/
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  cursor: pointer;
}

.toggle__option
{
  display: inline-block;
  vertical-align: middle;
  user-select: none;
}

.toggle__option:first-child
{
  text-align: right;
  left: 0;
}
.toggle__option:last-child
{
  right: 0;
}

.toggle__switch
{
  display: inline-block;
  vertical-align: middle;
  position: relative;
  width: 50px;
  height: 20px;
  margin: 0 10px;
}

/*focus styles for accessibility*/
.toggle__checkbox:focus ~ .toggle__switch
{
  outline: 1px solid #19ca2a;
}

/*base of toggle switch*/
.toggle__switch:before
{
  content: '';
  position: absolute;
  display: block;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 1;
  background-color: #464646;
  width: 50px;
  height: 2px;
}

/*move to either side of the base based on checkbox's state*/
.toggle__switch:after
{
  content: '';
  position: absolute;
  display: block;
  background-color: #171717;
  z-index: 1;

  width: 20px;
  height: 7px;
  left: 50%;
  top: 50%;
  transform: translate(-130%, -50%); /*position to the left side of the switch base*/
  transition: transform 0.30s ease-out;
}

.toggle__checkbox:checked ~ .toggle__switch:after
{
  transform: translate(30%, -50%); /*position to the right side of the switch base*/
}

Last Words

If you want to get a bit fancy, you can detect the user’s country (e.g. from user’s IP address with PHP) and display the unit of choice in their country by default.

Javascript is still an option; you could display the measurements in both units by default and hide the alternative with Javascript (if the script loads). This way users who don’t have Javascript enabled will still be able to view both sets of measurements, and those who have it enabled will be able to toggle the measurements as they wish. Progressive Enhancement FTW.

I haven’t done any accessibility testing. The checkbox is still accessible via the keyboard and the focus styles highlights the custom switch. If you think the checkbox isn’t fully accessible and know how to fix that, please share your thoughts.

Get in Touch