# How to Build a Pricing Table With a Monthly/Yearly CSS Toggle Switch

Toggle button switches are a classic UX trend often met in different parts of a website or an app. In a previous tutorial, we discussed how to build a CSS-only switch component for a simple to-do checklist. Today, we’ll create another switch: a monthly/yearly switch that will let visitors go through different pricing plans.

## Our Switch Component

Here’s our component demo:

## Showcase of Pricing Toggle Switches

Before we start building our switch, let’s first get inspiration from a few pricing pages that use toggle switches.

## 1. Begin With the HTML Markup

We’ll define a container that will include two nested wrappers: the switch wrapper and the table wrapper.

Inside the switch wrapper, we’ll place two radio buttons followed by their associated labels and an empty span element with the class of highlighter. By default, the first radio button will be checked.

It’s worth mentioning that there are different approaches we can follow to build the component. An alternative implementation might be to get rid of the empty span and use pseudo-elements.

Inside the table wrapper, we’ll place the table.

Here’s the starting markup:

 1 
 2 
 3   4   5   6   7   8 
 9 
 10  ...  11 
 12 


Most of the table contents and styling will come from a previous tutorial where we built a pricing table with a sticky header. From this tutorial, I’ve cleared out all the unnecessary styles. I’ve also made a few style adjustments to match the target of this demonstration. Anyhow, the table styles are of secondary importance.

For this exercise, we’ll only update the table headings, so the .info element will include two different amounts depending on the plan. By default, only the monthly amount will be visible.

Here’s what the markup of a table heading will look like:

 1 ...  2 
 3 
Starter
 4 
 5 
 6 
$10 month  7   8   9 $7 month
 10 
billed annually
 11 
 12   13 
 14 
 15 ... 

## 2. Define the Switch Styles

Similar to our previous switch component, we’ll also take advantage of the “CSS checkbox hack technique” to toggle the switch.

There are already lots of implementations of this method in the Tuts+ library, so I won’t go into more detail. I would recommend going through all these examples to become familiar with how to simulate JavaScript’s click event on CSS elements though.

Here are (briefly) the steps we’ll follow:

• Visually hide the radio buttons.

• Position absolutely the .highligher element inside the container.
• Each time we click on the switch, the .highlighter’s position will change thanks to the transform CSS property. At that point, the color of the active label will also change from black to white.

Here are all the styles attached to our component:

 1 /*CUSTOM VARIABLES HERE*/  2 3 .switch-wrapper {  4  position: relative;  5  display: inline-flex;  6  padding: 4px;  7  border: 1px solid lightgrey;  8  margin-bottom: 40px;  9  border-radius: 30px;  10  background: var(--white);  11 }  12 13 .switch-wrapper [type="radio"] {  14  position: absolute;  15  left: -9999px;  16 }  17 18 .switch-wrapper [type="radio"]:checked#monthly ~ label[for="monthly"],  19 .switch-wrapper [type="radio"]:checked#yearly ~ label[for="yearly"] {  20  color: var(--white);  21 }  22 23 .switch-wrapper [type="radio"]:checked#monthly ~ label[for="monthly"]:hover,  24 .switch-wrapper [type="radio"]:checked#yearly ~ label[for="yearly"]:hover {  25  background: transparent;  26 }  27 28 .switch-wrapper  29  [type="radio"]:checked#monthly  30  + label[for="yearly"]  31  ~ .highlighter {  32  transform: none;  33 }  34 35 .switch-wrapper  36  [type="radio"]:checked#yearly  37  + label[for="monthly"]  38  ~ .highlighter {  39  transform: translateX(100%);  40 }  41 42 .switch-wrapper label {  43  font-size: 16px;  44  z-index: 1;  45  min-width: 100px;  46  line-height: 32px;  47  cursor: pointer;  48  border-radius: 30px;  49  transition: color 0.25s ease-in-out;  50 }  51 52 .switch-wrapper label:hover {  53  background: var(--lightgray);  54 }  55 56 .switch-wrapper .highlighter {  57  position: absolute;  58  top: 4px;  59  left: 4px;  60  width: calc(50% - 4px);  61  height: calc(100% - 8px);  62  border-radius: 30px;  63  background: var(--darkgreen);  64  transition: transform 0.25s ease-in-out;  65 } 

## 3. Toggle Table Info With JavaScript

Up to this point, we’ve successfully created the switch. But, there’s one extra thing that we have to do; show the relevant table prices depending on the active selection.

To do this, we’ll use a bit of JavaScript. Specifically, each time a user toggles the switch, we’ll do the following things:

• Grab the ID of the selected radio button.
• Hide all the table elements with the class of .price.
• Show only the .price elements whose class matches this ID.

Here’s the required JavaScript code:

 1 const tableWrapper = document.querySelector(".table-wrapper");  2 const switchInputs = document.querySelectorAll(".switch-wrapper input");  3 const prices = tableWrapper.querySelectorAll(".price");  4 const toggleClass = "hide";  5 6 for (const switchInput of switchInputs) {  7  switchInput.addEventListener("input", function () {  8  for (const price of prices) {  9  price.classList.add(toggleClass);  10  }  11  const activePrices = tableWrapper.querySelectorAll(  12  .price.\${switchInput.id}  13  );  14  for (const activePrice of activePrices) {  15  activePrice.classList.remove(toggleClass);  16  }  17  });  18 } 

And the associated CSS class:

 1 table .hide {  2  display: none;  3 } 

## Conclusion

Thanks for following across another tutorial, folks! I hope this exercise has given you enough knowledge to build a CSS-only toggle switch for filtering parts of your website or app. Here we covered how to use such a switch as a part of a pricing page—another use case might be to have it switch layout views (grid and list views).

Of course, if you need to create switches with complex functionality, JavaScript will probably be a better approach.

Here’s a reminder of what we built:

As always, thanks a lot for reading!