DEV Community

Hammed Oyedele
Hammed Oyedele

Posted on • Originally published at on

Awesome Checkbox and Radio-Button CSS Hacks

Hello, today we will explore some CSS hacks that can be used without the need for JavaScript. I will go over 3 awesome hacks and I know you will be amazed by the end of this article. LET’S KEEP THE BALL ROLLIN’

  • CSS Menu

First, we will hide the checkbox and the menu container.

.menu--body {
 display: none;
Enter fullscreen mode Exit fullscreen mode

Now, the main trick of this Menu is the ~ CSS General Sibling selector. below CSS is used to change .menu--body to display: block when the checkbox is in:checked state.

According to MDN Docs, ~ is:

The general sibling combinator (~) separates two selectors and matches the second element only if it follows the first element (though not necessarily immediately), and both are children of the same parent element.

Below is the trick we will apply using this life-saver:

.menu--toggle:checked ~ .menu--body {
 display: block;
Enter fullscreen mode Exit fullscreen mode

Check below pen for the full markup of this hack

  • CSS Modal

This hack is same with CSS Menu so i am not going to explain this because the only difference is how will style the Modal container, but below is the pen:

The last hack we will go over is:

  • CSS Accordion

Yes, Accordion you all know can be created with pure CSS and radio buttons 🙄🙄 . Note: a single radio button can only be selected in a named group.

We will take advantage of this to create pure CSS accordion and it also utilize :checked state like CSS Menu, below is the pen for you to play around with.

Thanks for taking your time to read this, 😍😍😍 and can you do this:


See you next time!!! </>

Top comments (0)