- How TO — Custom Checkbox
- How To Create a Custom Checkbox
- Example
- Example
- How To Create a Custom Radio Button
- Example
- Checkbox CSS Styling a checkbox with only CSS
- The goals
- The results
- The strategy
- How to get there
- The CSS selectors used
- Important CSS styles used
- Starting out: the HTML
- First step: hide the unstyleable checkbox
- Second step: make our own checkbox
- Last step: make our checkbox change when checked
- Bonus points: extend this to radio buttons
- Wrapping up and side notes
- :checked
- Syntax
- Examples
- Basic example
- HTML
- CSS
- Result
- Toggling elements with a hidden checkbox
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
How TO — Custom Checkbox
Learn how to create custom checkboxes and radio buttons with CSS.
Custom checkbox:
Custom radio button:
How To Create a Custom Checkbox
Step 1) Add HTML:
Example
Step 2) Add CSS:
Example
/* Customize the label (the 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;
>
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark background-color: #ccc;
>
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark background-color: #2196F3;
>
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after content: «»;
position: absolute;
display: none;
>
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after display: block;
>
/* Style the checkmark/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
/* Customize the label (the 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 radio button */
.container input position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
>
/* Create a custom radio button */
.checkmark position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 50%;
>
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark background-color: #ccc;
>
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark background-color: #2196F3;
>
/* Create the indicator (the dot/circle — hidden when not checked) */
.checkmark:after content: «»;
position: absolute;
display: none;
>
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after display: block;
>
/* Style the indicator (dot/circle) */
.container .checkmark:after top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
>
Checkbox CSS Styling a checkbox with only CSS
This seems like an obvious statement; styling should be done with CSS. But unfortunately in the case of checkboxes, there’s basically no support for directly styling the checkbox. Because of that, I’ve seen several JavaScript implementations to change between displaying an unchecked box and a checked box based on state in order to fake a styled checkbox. But let’s not go so quickly to JS to solve a fundamentally styling problem! It can be done with just CSS, we just need to shift our thinking a little.
The goals
- Style the checkbox
- Style the label
- Require no additional DOM nodes or configuration (including icons/images)
- Require 0 JavaScript
The results
The strategy
Since we cannot style the checkbox input directly with something like checkbox-style: awesome; , we’ll need to take a different approach:
- Hide the actual checkbox input
- Show a styled element that looks like an empty checkbox when the input is unchecked
- Show a styled element that looks like a checked checkbox when the input is checked
How to get there
The CSS selectors used
- Type selector type — selects all elements of the given type (e.g. input will select all nodes)
- Attribute selector [attribute=»value»] — selects an element with attribute where its value is exactly value
- Psuedo-class :checked — selects checkbox/radio input types or option s in a select that are selected/checked/on/active
- Psuedo-element ::before — styleable element that doesn’t actually exist in the DOM; considered the first child of the selected element
- Universal selector * — selects anything/everything
- Child combinator > — combines two selectors; narrowing the selection on the right-hand side to only those elements that are direct descendants of elements selected by the left-hand side.
- Adjacent sibling combinator + — combines two selectors; narrowing the selection on the right-hand side to only those elements that are the siblings immediately after the elements on the left-hand side
Important CSS styles used
- content — used in the ::before psuedo-element to set its content
- display — specifically none to hide elements and inline-block to make our otherwise inline checkbox able to have a consistent width and height
- width / height — does what you think: sets width and height of the element
- color — sets the color of the text
- text-align / vertical-align — used for adjusting the position of our check/checkbox to its label
- border styles — How we’ll form and color the checkbox
- background — sets the background color (used to fill in the checkbox when it is checked)
Starting out: the HTML
Let’s set up our checkbox as a child of its label element with a sibling of a span of the label text:
type="checkbox" name="key" value="value" /> I am a checkbox
This structure allows clicking on the label text ( I am a checkbox ) to toggle the checkbox without needing for or unique id attributes. Placing the text in a span directly after the input will allow us to select it in CSS.
First step: hide the unstyleable checkbox
Going back to our strategy: since we can’t do anything with the native checkbox, we’ll have to hide it and do our own thing.
label > input[type="checkbox"] display: none; >
We’ll select the checkbox ( input[type=»checkbox»] ) and make sure it’s labelled the way we need it to be ( label > ). Then just display: none to get it off our screens.
Second step: make our own checkbox
Making an empty square is easy with CSS, just put a border around an element with no content and set its width and height. Let’s also put it on the ::before psuedo-element to avoid adding unnecessary HTML.
label > input[type="checkbox"] + *::before content: ""; display: inline-block; vertical-align: bottom; width: 1rem; height: 1rem; border-radius: 10%; border-style: solid; border-width: 0.1rem; border-color: gray; >
Building from the selection in the previous section; we add + * to select any element as long as it is the direct subsequent sibling to the checkbox of interest and then ::before to select the said psuedo-element for making the box.
Last step: make our checkbox change when checked
So far, even though the checkbox has been working, it doesn’t look like it’s working. Let’s change that by making the checkbox checked when it is :checked . Also, for the proof-of-concept, let’s change styles on the label text itself (while I’ll only change the text color, you can imagine changing any style to build any sort of check/uncheck select/unselect interface).
label > input[type="checkbox"]:checked + *::before content: "✓"; color: white; text-align: center; background: teal; border-color: teal; > label > input[type="checkbox"]:checked + * color: teal; >
The important part here being :checked is placed after the input[type=»checkbox»] (since that is the element that is checked or not). And fortunately for us, there’s a checkmark character that we can set as the content of our checkbox: ✓ .
Bonus points: extend this to radio buttons
As a challenge to you, reader, this same strategy applies to making a styled radio button. Using this as a guide, are you able to transform this for the type=»radio» input? (or just follow me as that will be the next installment in this series)
Wrapping up and side notes
Thanks for reading! I like sharing and finding things like this where common web design patterns can be done without a massive JavaScript library or framework bogging the page down. Give me some suggestions below on patterns you’d like to see me break-down in CSS/HTML-only for this series.
While this example had «no unnecessary DOM»; it is also perfectly valid to include an additional span (or two) to hold svg/font-awesome icons for more precise/exotic checkbox designs. Then :checked should be used to alternatively display: none and display: inline-block the icons and + will need to become ~ to select all the siblings.
:checked
The user can engage this state by checking/selecting an element, or disengage it by unchecking/deselecting the element.
Note: Because browsers often treat s as replaced elements, the extent to which they can be styled with the :checked pseudo-class varies from browser to browser.
Syntax
Examples
Basic example
HTML
div> input type="radio" name="my-input" id="yes" value="yes" /> label for="yes">Yeslabel> input type="radio" name="my-input" id="no" value="no" /> label for="no">Nolabel> div> div> input type="checkbox" name="my-checkbox" id="opt-in" /> label for="opt-in">Check me!label> div> select name="my-select" id="fruit"> option value="opt1">Applesoption> option value="opt2">Grapesoption> option value="opt3">Pearsoption> select>
CSS
div, select margin: 8px; > /* Labels for checked inputs */ input:checked + label color: red; > /* Radio element, when checked */ input[type="radio"]:checked box-shadow: 0 0 0 3px orange; > /* Checkbox element, when checked */ input[type="checkbox"]:checked box-shadow: 0 0 0 3px hotpink; > /* Option elements, when selected */ option:checked box-shadow: 0 0 0 3px lime; color: red; >
Result
Toggling elements with a hidden checkbox
This example utilizes the :checked pseudo-class to let the user toggle content based on the state of a checkbox, all without using JavaScript.
HTML
input type="checkbox" id="expand-toggle" /> table> thead> tr> th>Column #1th> th>Column #2th> th>Column #3th> tr> thead> tbody> tr class="expandable"> td>[more text]td> td>[more text]td> td>[more text]td> tr> tr> td>[cell text]td> td>[cell text]td> td>[cell text]td> tr> tr> td>[cell text]td> td>[cell text]td> td>[cell text]td> tr> tr class="expandable"> td>[more text]td> td>[more text]td> td>[more text]td> tr> tr class="expandable"> td>[more text]td> td>[more text]td> td>[more text]td> tr> tbody> table> label for="expand-toggle" id="expand-btn">Toggle hidden rowslabel>
CSS
/* Hide the toggle checkbox */ #expand-toggle display: none; > /* Hide expandable content by default */ .expandable visibility: collapse; background: #ddd; > /* Style the button */ #expand-btn display: inline-block; margin-top: 12px; padding: 5px 11px; background-color: #ff7; border: 1px solid; border-radius: 3px; > /* Show hidden content when the checkbox is checked */ #expand-toggle:checked ~ * .expandable visibility: visible; > /* Style the button when the checkbox is checked */ #expand-toggle:checked ~ #expand-btn background-color: #ccc; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser