How to create: custom checkbox
- Previous Page Login Form in Navigation Bar
- Next Page Custom Dropdown Box
Learn how to use CSS to create custom checkboxes and radio buttons.
Default:
OneTwo
One
Two
Custom checkbox:
Custom radio button:
How to create a custom checkbox
First step - Add HTML:
<label class="container">One <input type="checkbox" checked="checked"> <span class="checkmark"></span> </label> <label class="container">Two <input type="checkbox"> <span class="checkmark"></span> </label> <label class="container">Three <input type="checkbox"> <span class="checkmark"></span> </label> <label class="container">Four <input type="checkbox"> <span class="checkmark"></span> </label>
Second step - Add CSS:
/* Custom label (container) */ .container { display: block; position: relative; padding-left: 35px; margin-bottom: 12px; cursor: pointer; font-size: 22px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; {} /* Hide the browser's default checkbox */ .container input { position: absolute; opacity: 0; cursor: pointer; height: 0; width: 0; {} /* Create a custom checkbox */ .checkmark { position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: #eee; {} /* Add gray background color when mouse hovers */ .container:hover input ~ .checkmark { background-color: #ccc; {} /* Add a blue background after selecting the checkbox */ .container input:checked ~ .checkmark { background-color: #2196F3; {} /* Create the checkbox marker/indicator (hidden when not selected) */ .checkmark:after { content: ""; position: absolute; display: none; {} /* Display the checkbox marker when selected */ .container input:checked ~ .checkmark:after { display: block; {} /* Set the style of the checkbox marker/indicator */ .container .checkmark:after { left: 9px; top: 5px; width: 5px; height: 10px; border: solid white; border-width: 0 3px 3px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); {}
How to create a custom radio button
Example
/* Custom label (container) */ .container { display: block; position: relative; padding-left: 35px; margin-bottom: 12px; cursor: pointer; font-size: 22px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; {} /* Hide browser's default radio button */ .container input { position: absolute; opacity: 0; cursor: pointer; height: 0; width: 0; {} /* Create custom radio button */ .checkmark { position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: #eee; border-radius: 50%; {} /* Add gray background color when mouse hovers */ .container:hover input ~ .checkmark { background-color: #ccc; {} /* Add blue background when radio button is selected */ .container input:checked ~ .checkmark { background-color: #2196F3; {} /* Create indicator (dot/circle - hidden when not selected) */ .checkmark:after { content: ""; position: absolute; display: none; {} /* Display indicator (dot/circle) when selected */ .container input:checked ~ .checkmark:after { display: block; {} /* Set indicator style (dot/circle) */ .container .checkmark:after { top: 9px; left: 9px; width: 8px; height: 8px; border-radius: 50%; background: white; {}
- Previous Page Login Form in Navigation Bar
- Next Page Custom Dropdown Box