Cet article est également disponible en : Français
In the past, I worked on several projects looking for a way to deal with custom styled proposed by some good designers to change aspect of form controls like checkbox, radio, file input, etc. As you may know, these elements are historycally not stylable for an accessibility reason. (as I heard). Still. I have a way to style checkboxes and radio buttons… Ready?
Codes and styles I will show you are using CSS level 3 selectors. Old browsers are not concerned by the changes I will propose to you. An automatic fallback to the classical old styles will benefit to these old engines.
Why are we trying to style these form controls?
To harmonize styles between browsers, for sure, but also to make some of them more effective in precise contexts.
My goal is not to explain how the context works, my goal is just to give you technical solution. Be creative then 🙂
Slightly modify the checkboxes appearance
With that example, we will write a solid base for our CSS. It will give me the possibility to explain how selectors are working.
What I want to get.
Let’s start with the HTML code. A simply composed code with one
label and one
input in each
You have now in the order 4 checkboxes: 1 empty, 1 checked and 1 disabled and 1 disabled checked.
Let’s go with CSS.
input:checkbox element is not stylable. We will play with the styles of the
label element and its two pseudo-elements.
It’s precisely in these selector you will find the magic: I will show custom checkbox with my recent browser, and classical ones with my old browser.
Targetting only recent browsers
All our custom styles will use this type of selector:
[type="checkbox"]:not(:checked) in there declaration. Which means if the web browser doesn’t recognize the selector, these styles will not be applied. It’s a kind of feature detection thanks to the selector.
The first declaration allows you to place the checkbox below the upcoming custom boxes to hide it visually without using
display:none; and to preserve readability by screen readers.
The second one makes the label element more attractive. The space is done to fit the futur checkbox size.
relative position is useful here to precisely place the checkbox (the
label becoming the reference).
Creating our checkboxes
Now, we will visually create the checkbox thanks to
:after (the check symbol) and
:before (to make the box) pseudo-elements.
The first two declarations are for the box and the check symbol aspects (size, color, etc). The two next define the states: not-checked or checked. An animation is played because of the
Styling differents states
Basics done, now we will set the other status: disabled, disabled checked, focused). Find below some styling ideas, but feel free to edit them.
That’s all for these custom styles.
Let’s go further – Advanced styles
I will not explain everything about this demonstration, the idea is exactly the same.
You can now compose a more complexe HTML code using the same logic, pseudo-class and pseudo-elements.
Here another idea of what you can do with this code.
These two demonstrations are using respectively
em and pixel unit (
px) to make examples more understandable for this last one. You should try with
rem to create a more flexible and maintainable code, depending on your experience and web project.
Looking for ideas?
Now you can do what you want with checkboxes (or radio), find below so inspiration to go further.
And yes, really, you can do the same with radio buttons. Did you try?
Your turn now!