Created
May 30, 2025 16:27
-
-
Save rafaelcamargo/d27a556892081532e21cd9d11f677e3a to your computer and use it in GitHub Desktop.
Customizing checkboxes and radio buttons without hacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Customizing checkboxes and radio buttons without hacks</title> | |
<style> | |
html, | |
body { | |
margin: 0; | |
font-family: Arial, sans-serif; | |
background-color: #F1F1F1; | |
} | |
form { | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
justify-content: center; | |
min-height: 100vh; | |
} | |
form > div + div { | |
margin-top: 40px; | |
} | |
input[type=checkbox], | |
input[type=radio] { | |
margin: 5px 6px 5px 0; | |
background-color: #FFF; | |
border-width: 1px; | |
border-style: solid; | |
border-color: #C6C6C6; | |
box-sizing: border-box; | |
box-shadow: 0 0 0 #CDE6FE; | |
appearance: none; | |
outline: 0; | |
transition-property: box-shadow, border-color; | |
transition-duration: 200ms; | |
transition-timing-function: ease-in-out; | |
} | |
input[type=checkbox]:focus, | |
input[type=checkbox]:active, | |
input[type=radio]:focus, | |
input[type=radio]:active { | |
box-shadow: 0 0 0 4px #CDE6FE; | |
border-color: #3A80EC; | |
} | |
input[type=checkbox]:checked, | |
input[type=radio]:checked { | |
background-color: #6699FF; | |
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDEyIDEyIj48cGF0aCBmaWxsPSIjZmZmIiBkPSJNMTEuNiwyLjNsLTYuOCw4LjhjMCwwLS4xLjItLjMuMnMtLjIsMC0uMy0uMVMuNCw3LjQuNCw3LjRoMGMwLS4xLDAtLjIsMC0uMnMwLS4xLDAtLjJjMCwwLDAsMCwwLDAsLjQtLjQsMS4xLTEuMiwxLjItMS4zLDAsMCwuMS0uMS4yLS4xcy4yLjEuMy4yYzAsMCwyLjIsMi4xLDIuMiwyLjFMOS44LjhzLjEsMCwuMiwwLC4xLDAsLjIsMGwxLjUsMS4yczAsLjEsMCwuMmMwLDAsMCwuMSwwLC4yWiIvPjwvc3ZnPgo='); | |
background-size: 12px 12px; | |
background-position: center center; | |
background-repeat: no-repeat; | |
} | |
input[type=checkbox]:checked:not(:disabled), | |
input[type=radio]:checked:not(:disabled) { | |
border-color: #6699FF; | |
} | |
input[type=checkbox]:disabled, | |
input[type=radio]:disabled { | |
background-color: #C6C6C6; | |
} | |
input[type=checkbox] { | |
width: 22px; | |
height: 22px; | |
border-radius: 6px; | |
} | |
input[type=radio] { | |
width: 24px; | |
height: 24px; | |
border-radius: 12px; | |
} | |
label:has(> input) { | |
display: inline-flex; | |
align-items: center; | |
color: #555; | |
font-size: 12px; | |
font-weight: bold; | |
text-transform: uppercase; | |
} | |
label:not(:first-child) { | |
margin-left: 20px; | |
} | |
</style> | |
</head> | |
<body> | |
<form> | |
<div> | |
<label> | |
<input type="checkbox" name="genres"> Rock | |
</label> | |
<label> | |
<input type="checkbox" name="genres"> Jazz | |
</label> | |
<label> | |
<input type="checkbox" name="genres"> Blues | |
</label> | |
</div> | |
<div> | |
<label> | |
<input type="radio" name="beverage" value="water"> Water | |
</label> | |
<label> | |
<input type="radio" name="beverage" value="coffee"> Coffee | |
</label> | |
<label> | |
<input type="radio" name="beverage" value="tea"> Tea | |
</label> | |
</div> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment