Skip to content

Instantly share code, notes, and snippets.

@adbutterfield
Last active September 9, 2023 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adbutterfield/990f05a17eb47419094515af59c28b09 to your computer and use it in GitHub Desktop.
Save adbutterfield/990f05a17eb47419094515af59c28b09 to your computer and use it in GitHub Desktop.
Custom Styling Checkboxes: The Modern Way (The Butterfield Way)
:root {
--checkbox-border-color: #8b8c89;
--checkbox-checked-color: #274c77;
--checkbox-hover-color: #a3cef1;
--checkbox-disabled-bg-color: #d9d9d9;
}
input[type="checkbox"] {
box-sizing: border-box;
width: 20px;
height: 20px;
margin: 6px;
padding: 0;
border: 2px solid var(--checkbox-border-color);
appearance: none;
background-color: transparent;
outline: none;
transition: outline 0.1s;
}
input[type="checkbox"]:checked {
background-size: cover;
padding: 2px;
}
input[type="checkbox"]:not(:disabled):checked {
border-color: var(--checkbox-checked-color);
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 32 32" xml:space="preserve"><path style="fill: %23274c77" d="M11.941,28.877l-11.941-11.942l5.695-5.696l6.246,6.246l14.364-14.364L32,8.818"/></svg>');
}
input[type="checkbox"]:disabled {
background-color: var(--checkbox-disabled-bg-color);
}
input[type="checkbox"]:disabled:checked {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 32 32" xml:space="preserve"><path style="fill: %238b8c89" d="M11.941,28.877l-11.941-11.942l5.695-5.696l6.246,6.246l14.364-14.364L32,8.818"/></svg>');
}
@media (hover: hover) {
input[type="checkbox"]:not(:disabled):hover {
background-color: var(--checkbox-hover-color);
outline: 6px solid var(--checkbox-hover-color);
transform: scale(1.05);
}
}
input[type="checkbox"]:focus-visible {
outline: 6px solid var(--checkbox-hover-color);
transform: scale(1.05);
}
@media (prefers-reduced-motion: reduce) {
input[type="checkbox"] {
transition: none;
}
}
$checkbox-border-color: #8b8c89;
$checkbox-checked-color: #274c77;
$checkbox-hover-color: #a3cef1;
$checkbox-disabled-bg-color: #d9d9d9;
input[type="checkbox"] {
box-sizing: border-box;
width: 20px;
height: 20px;
margin: 6px;
padding: 0;
border: 2px solid $checkbox-border-color;
appearance: none;
background-color: transparent;
outline: none;
transition: outline 0.1s;
&:checked {
background-size: cover;
padding: 2px;
border-color: $checkbox-checked-color;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 32 32" xml:space="preserve"><path style="fill: #{$checkbox-checked-color}" d="M11.941,28.877l-11.941-11.942l5.695-5.696l6.246,6.246l14.364-14.364L32,8.818"/></svg>');
}
&:disabled {
background-color: $checkbox-disabled-bg-color;
&:checked {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 32 32" xml:space="preserve"><path style="fill: #{$checkbox-border-color}" d="M11.941,28.877l-11.941-11.942l5.695-5.696l6.246,6.246l14.364-14.364L32,8.818"/></svg>');
}
}
@media (hover: hover) {
&:not(:disabled):hover {
background-color: $checkbox-hover-color;
outline: 6px solid $checkbox-hover-color;
transform: scale(1.05);
}
}
&:focus-visible {
outline: 6px solid $checkbox-hover-color;
transform: scale(1.05);
}
@media (prefers-reduced-motion: reduce) {
transition: none;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment