Last active
February 29, 2024 19:44
-
-
Save adbutterfield/260249153051827d2dda78c39fba3259 to your computer and use it in GitHub Desktop.
Custom Styling Radio Buttons: The Modern Way (The Butterfield Way)
This file contains 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
:root { | |
--radio-border-color: #8b8c89; | |
--radio-checked-color: #274c77; | |
--radio-hover-color: #a3cef1; | |
--radio-disabled-bg-color: #d9d9d9; | |
} | |
input[type="radio"] { | |
box-sizing: border-box; | |
width: 20px; | |
height: 20px; | |
margin: 6px; | |
padding: 0; | |
border: 2px solid var(--radio-border-color); | |
border-radius: 50%; | |
appearance: none; | |
background-color: transparent; | |
outline: none; | |
transition: outline 0.1s; | |
} | |
input[type="radio"]:not(:disabled):checked { | |
border-color: var(--radio-checked-color); | |
background-color: var(--radio-checked-color); | |
background-clip: content-box; | |
padding: 2px; | |
background-image: radial-gradient( | |
circle, | |
var(--radio-checked-color) 0%, | |
var(--radio-checked-color) 50%, | |
transparent 60%, | |
transparent 100% | |
); | |
} | |
input[type="radio"]:disabled { | |
background-color: var(--radio-disabled-bg-color); | |
} | |
input[type="radio"]:disabled:checked { | |
background-image: radial-gradient( | |
circle, | |
var(--radio-border-color) 0%, | |
var(--radio-border-color) 50%, | |
transparent 50%, | |
transparent 100% | |
); | |
} | |
@media (hover: hover) { | |
input[type="radio"]:not(:disabled):hover { | |
background-color: var(--radio-hover-color); | |
outline: 6px solid var(--radio-hover-color); | |
transform: scale(1.05); | |
} | |
} | |
input[type="radio"]:focus-visible { | |
background-color: var(--radio-hover-color); | |
outline: 6px solid var(--radio-hover-color); | |
transform: scale(1.05); | |
} | |
@media (prefers-reduced-motion: reduce) { | |
input[type="radio"] { | |
transition: none; | |
} | |
input[type="radio"]:focus-visible { | |
transform: scale(1); | |
} | |
} | |
@media (prefers-reduced-motion: reduce) and (hover: hover) { | |
input[type="radio"]:not(:disabled):hover { | |
transform: scale(1); | |
} | |
} |
This file contains 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
$radio-border-color: #8b8c89; | |
$radio-checked-color: #274c77; | |
$radio-hover-color: #a3cef1; | |
$radio-disabled-bg-color: #d9d9d9; | |
input[type="radio"] { | |
box-sizing: border-box; | |
width: 20px; | |
height: 20px; | |
margin: 6px; | |
padding: 0; | |
border: 2px solid $radio-border-color; | |
border-radius: 50%; | |
appearance: none; | |
background-color: transparent; | |
outline: none; | |
transition: outline 0.1s; | |
&:not(:disabled):checked { | |
border-color: $radio-checked-color; | |
background-color: $radio-checked-color; | |
background-clip: content-box; | |
padding: 2px; | |
background-image: radial-gradient( | |
circle, | |
$radio-checked-color 0%, | |
$radio-checked-color 50%, | |
transparent 60%, | |
transparent 100% | |
); | |
} | |
&:disabled { | |
background-color: $radio-disabled-bg-color; | |
} | |
&:disabled:checked { | |
background-image: radial-gradient( | |
circle, | |
$radio-border-color 0%, | |
$radio-border-color 50%, | |
transparent 50%, | |
transparent 100% | |
); | |
} | |
&:focus-visible { | |
background-color: $radio-hover-color; | |
outline: 6px solid $radio-hover-color; | |
transform: scale(1.05); | |
} | |
@media (hover: hover) { | |
&:not(:disabled):hover { | |
background-color: $radio-hover-color; | |
outline: 6px solid $radio-hover-color; | |
transform: scale(1.05); | |
} | |
} | |
@media (prefers-reduced-motion: reduce) { | |
transition: none; | |
&:focus-visible { | |
transform: scale(1); | |
} | |
} | |
@media (prefers-reduced-motion: reduce) and (hover: hover) { | |
&:not(:disabled):hover { | |
transform: scale(1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment