HTML + CSS only Accordion Element (Radio Button)

HTML + CSS only Accordion Element (Radio Button)

13 Aug, '16css2 mins.

In this tutorial, we create a HTML and CSS only accordion, using the radio button method.

 

 

View Demo

Background

Accordions are graphical control elements, which comprise a vertically (or horizonally) stacked list of items, such as labels, headings or thumbnails. Each item can be “expanded” or “stretched” to reveal the content associated with that item.

They are common today on websites and, not only add a cool factor, but also allows us to get a lot of content into small spaces.

This was previously only achieved using Javascript or jQuery, but with advancements (CSS3), it is now possible using only CSS.

 

Let’s Do this

We will be using a hidden radio button group, the :checked pseudo-class and appropriate HTML and CSS to create the accordion.

The logic: When the user selects an accordion tab (heading), they basically check the radio button that belongs to that tab. When they click on the next tab in the accordion, they select the next radio button, etc. With CSS rules we then control opening and closing of the tab to show and hide its content area.

Using the radio button method ensures only one section is opened at a time.

 

The HTML

The HTML is simply an unordered list. Each list item represents a single tab and contains a radio input, a pair of empty <i> tags as placeholder to add visual indicators, a H2 title tag as the heading and the tab content.

 

The CSS

Standard SCSS is used here and a few variables as well as responsive styles are defined. The radio button is hidden using opacity: 0; rule, the H2 holds the title of each tab, and the <i> tag is absolutely positioned and styled to add the visual (up/down arrow) indicators to each tab.

Combining the radio input’s :checked pseudo selector with the element ~ element general sibling selector, the tab content’s z-index, height and other properties are manipulated to open and close each accordion tab, hence displaying and hiding content.

 

The Result

A responsive HTML + CSS only Accordion Element, using the Radio Button method.

 

Basically…

Accordions are useful when you want to toggle between hiding and showing large amount of content within a small space. The method described above allow us to achieve this using HTML + CSS only with the radio button method.

Hopefully this will come in handy for your next project.

 

© 2023Prince Bazawule