← Back to CSS
56

The input hack in CSS: toggles and menus without JavaScript

Learn how checkbox and radio states can drive simple UI interactions in pure CSS, while understanding the accessibility and maintainability limits that tell you when JavaScript is the better tool.

📘 Theory

The base pattern

Hidden checkbox, connected label and a `:checked` selector.

1

The `` activates its associated checkbox. CSS can then react through `:checked` and change the appearance of nearby elements.

2

The usual mechanism is a sibling combinator such as `+` or `~` to reach the panel you want to toggle.

Limits and good practice

Not every interactive state should be solved through a hack.

Avoid fragile selector chains that depend too heavily on the exact HTML order.

If the interaction needs Escape handling, focus management, outside-click closing or several synchronized states, JavaScript is usually the right layer.

  • Keep the markup semantic and labels clear.
  • Do not turn hidden inputs into a replacement for real state logic.
  • For critical components, prioritize complete accessibility over cleverness.

🧪 Learn by doing

Example Demo: FAQ without JavaScript A simple accordion driven by a checkbox and `:checked`.

🏁 Challenges

Challenge Challenge: mobile menu with CSS only Show `.menu` when `#nav-toggle` is checked.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com