The “Checkbox Hack” (and things you can do with it)

Avatar of Chris Coyier
Chris Coyier on (Updated on )

UGURUS offers elite coaching and mentorship for agency owners looking to grow. Start with the free Agency Accelerator today.

The “Checkbox Hack” is where you use a connected <label> and <input type="checkbox"> and usually some other element you are trying to control, like this:

<label for="toggle">Do Something</label>
<input type="checkbox" id="toggle">
<div class="control-me">Control me</div>

Then with CSS, you hide the checkbox entirely. Probably by kicking it off the page with absolute positioning or setting its opacity to zero. But just because the checkbox is hidden, clicking the <label> still toggles its value on and off. Then you can use the adjacent sibling combinator to style the <div> differently based on the :checked state of the input.

.control-me {
  /* Default state */
}
#toggle:checked ~ .control-me {
  /* A toggled state! No JavaScript! */
}

So you can style an element completely differently depending on the state of that checkbox, which you don’t even see. Pretty neat. Let’s look at a bunch of things the “Checkbox Hack” can do.

See the Pen
The Checkbox Hack
by Chris Coyier (@chriscoyier)
on CodePen.

Some of this stuff crosses the line of what you “should” do with CSS and introduces some questionable semantics. It’s still very fun to play with and cool that it’s possible, but in general, functional behavior should be controlled by JavaScript.

Custom Designed Radio Buttons and Checkboxes

See the Pen
Custom checkboxes with CSS only
by Geoffrey Crofte (@GeoffreyCrofte)
on CodePen.

You can hide the default UI of a radio button or checkbox, and display a custom version right on top of it.

File system like “tree menu”

Demo by Ryan Seddon

Tabbed Areas

The “tabs” design pattern is just toggling on and off of areas, perfect for the checkbox hack. But instead of checkboxes, in which any checkbox can be on or off independently of one another, these tabs use radio buttons in which only one per group can be on at a time (like how only one tab can be active at a time).

Demo from Functional CSS tabs revisited:

See the Pen
Functional CSS Tabs
by Chris Coyier (@chriscoyier)
on CodePen.

Dropdown Menus

See the Pen
Radio Button Dropdowns
by Chris Coyier (@chriscoyier)
on CodePen.

Push Toggles

A toggle can take the form of ON/OFF, which can be done with a single <input type="checkbox">. Like emoji toggles!

See the Pen
CSS Checkbox Toggle Switch
by Chris Coyier (@chriscoyier)
on CodePen.

Or it could be multiple <input type="checkbox"> elements to switch between differnet distinct values.

Those are radio inputs in this MPG calculator

FAQ Answer Revealing

You’d probably just use a <details>/<summary> combo for this these days, but expandable sections can be done with the checkbox hack.

See the Pen
FAQ without Details/Summary (checkbox hack)
by Chris Coyier (@chriscoyier)
on CodePen.

Hide the Sidebar

Like the old school Octopress theme.

See the Pen
Hide the Sidebar with Checkbox Hack
by Chris Coyier (@chriscoyier)
on CodePen.