No JavaScript. Just Clean HTML & CSS.
Creating visually appealing and interactive form elements used to require JavaScript. But with modern CSS, you can now fully customize checkboxes using nothing but HTML and CSS.
In this article, we’ll show you how to build a pure CSS custom checkbox from scratch — no external libraries or JavaScript required.
1 2 3 4 5 6 7 | <label class="custom-checkbox"> <input type="checkbox"> <span class="checkmark"></span> Pure CSS Custom Checkbox </label> |
🧠 Why use a
<label>
? Clicking the text will also toggle the checkbox — improving both UX and accessibility.
🎨 Styling with CSS
Here’s where the magic happens.
We style the container, hide the default checkbox input, and design a custom box with a checkmark that appears when the checkbox is selected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | .custom-checkbox { display: inline-block; position: relative; padding-left: 25px; cursor: pointer; font-size: 16px; user-select: none; } .custom-checkbox input { position: absolute; opacity: 0; cursor: pointer; } .checkmark { position: absolute; top: 0; left: 0; height: 20px; width: 20px; background-color: #ccc; border-radius: 4px; } .custom-checkbox input:checked ~ .checkmark { background-color: #2196F3; } .checkmark:after { content: ""; position: absolute; display: none; } .custom-checkbox input:checked ~ .checkmark:after { display: block; } .custom-checkbox .checkmark:after { left: 7px; top: 3px; width: 5px; height: 10px; border: solid white; border-width: 0 2px 2px 0; transform: rotate(45deg); } |
🖼️ How It Works
- The real checkbox input is hidden using
opacity: 0
. - The
.checkmark
span acts as the visual box. - When the checkbox is checked, the
.checkmark
background changes color. - A checkmark (✓) is drawn using CSS borders and is only shown when the box is checked.
💡 Why Use Pure CSS?
- ⚡ Lightweight — No JavaScript needed.
- 🧩 Customizable — Easily change size, colors, or animations.
- 🧠 Accessible — Works seamlessly with screen readers when using proper labels.
- 🎨 Design-Friendly — You’re in full control of styling.
🧪 Demo Time
Here’s a live snippet of the complete code in action:
1 2 3 4 5 6 7 | <label class="custom-checkbox"> <input type="checkbox"> <span class="checkmark"></span> Pure CSS Custom Checkbox </label> |
💡 Try styling the checkmark with different shapes, colors, or even animations for a more dynamic UI.
Here’s the complete and final HTML + CSS code for the Pure CSS Custom Checkbox demo based on your structure, ready to run in any browser:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Pure CSS Custom Checkbox</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background-color: #f9f9f9; } .custom-checkbox { display: inline-block; position: relative; padding-left: 30px; margin-bottom: 20px; cursor: pointer; font-size: 18px; user-select: none; color: #333; } .custom-checkbox input { position: absolute; opacity: 0; cursor: pointer; } .checkmark { position: absolute; top: 0; left: 0; height: 20px; width: 20px; background-color: #ccc; border-radius: 4px; transition: background-color 0.3s ease; } .custom-checkbox input:checked ~ .checkmark { background-color: #2196F3; } .checkmark:after { content: ""; position: absolute; display: none; } .custom-checkbox input:checked ~ .checkmark:after { display: block; } .custom-checkbox .checkmark:after { left: 7px; top: 3px; width: 5px; height: 10px; border: solid white; border-width: 0 2px 2px 0; transform: rotate(45deg); } </style> </head> <body> <h2>Pure CSS Custom Checkbox</h2> <label class="custom-checkbox"> <input type="checkbox"> <span class="checkmark"></span> I agree to the terms and conditions </label> </body> </html> |
✅ This version is clean, accessible, and production-ready. You can copy and paste this directly into your project or embed it into your documentation or blog post.
[…] 👉 Continue reading: Pure CSS Custom Checkbox […]