DEV Community

Jude
Jude

Posted on

1

Turn a checkbox into a button without any javascript.

Sometimes checkboxes aren't the best UI/UX for your users, especially if you have a lot of options that you need to give to a user to choose from.

This technique allows you to turn a checkbox into a button, creating a larger click area and also giving better context to what the user is selecting.

NOTE: In the HTML it's important that the input comes before the label.

<style>
.checkbox-btn-layout {
display: flex;
flex-wrap: wrap;
gap: 4px;
}
.checkbox-hide {
display: none;
}
label.checkbox-btn {
text-align: center;
padding: 6px 8px;
line-height: 1;
border-radius: 6px;
border: 1px solid #ccc;
font-weight: 500;
}
.checkbox-hide:checked + label {
background-color: orange;
border: 1px solid red;
color: white;
}
</style>
<html>
<head>
</head>
<body>
<div class="checkbox-btn-layout">
<input class="checkbox-hide" type="checkbox" value="condition=excellent" name="condition[excellent]" id="condition[excellent]">
<label class="checkbox-btn" for="condition[excellent]">Excellent</label>
<input class="checkbox-hide" type="checkbox" value="condition=good" name="condition[good]" id="condition[good]">
<label class="checkbox-btn" for="condition[good]">Good</label>
<input class="checkbox-hide" type="checkbox" value="condition=poor" name="condition[poor]" id="condition[poor]">
<label class="checkbox-btn" for="condition[poor]">Poor</label>
<input class="checkbox-hide" type="checkbox" value="condition=damaged" name="condition[damaged]" id="condition[damaged]">
<label class="checkbox-btn" for="condition[damaged]">Damaged</label>
</div>
</body>
</html>

And that's it. If you have any questions or it's not clear what's going on, or even if you have a better way of achieving this, feel free to comment below.

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay