How to style a checkbox using CSS
UPDATE:
The below answer references the state of things before widespread availability of CSS 3. In modern browsers (including Internet Explorer 9 and later) it is more straightforward to create checkbox replacements with your preferred styling, without using JavaScript.
Here are some useful links:
- Creating Custom Form Checkboxes with Just CSS
- Easy CSS Checkbox Generator
- Stuff You Can Do With The Checkbox Hack
- Implementing Custom Checkboxes and Radio Buttons with CSS3
- How to Style a Checkbox With CSS
It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported :checked
selector, you can make your replacement correctly reflect the checked status of the box.
OLDER ANSWER
Here's a useful article about styling checkboxes. Basically, that writer found that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.
It's not hard to imagine a workaround where you would use JavaScript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without JavaScript would see the default checkbox.
Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.
CSS checkbox input styling
With CSS 2 you can do this:
input[type='checkbox'] { ... }
This should be pretty widely supported by now. See support for browsers
Is it possible to style a checkbox like this?
The best way to do this is to remove the default appearance of the checkbox using appearance: none;
. From there, you can change anything about its style you want to. Here is an example that gets close to what you are wanting.
/* The checkbox holder */
.checkbox {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
}
/* The checkbox */
.checkbox > input {
/*
Remove the default appearance.
*/
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance: none;
appearance: none;
/*
Set the size of the checkbox.
*/
width: 20px;
height: 20px;
box-shadow: 0 0 0 2px black; /* Outer border */
border: 3px solid white; /* Inner border */
}
/* The checkbox - when checked */
.checkbox > input:checked {
background-color: black; /* The "check" */
}
<h1>Item List</h1>
<div class="checkbox">
<input id="item1" type="checkbox">
<label for="item1">Item 1</label>
</div>
<div class="checkbox">
<input id="item2" type="checkbox">
<label for="item2">Item 2</label>
</div>
<div class="checkbox">
<input id="item3" type="checkbox">
<label for="item3">Item 3</label>
</div>
Checkbox styling only css
The first thing you need to do with your code is add a label
, e.g.:
<form>
<input class="newsletter" type="checkbox" value="text" id="newsletter" name="newsletter">
<label for="newsletter">Ich möchte den Newsletter bekommen</label>
</form>
I've also added an id
and name
attribute to the checkbox. The id
needs to be the same as the for
attribute on the label
for this trick to work.
Next, we need to visibly hide the checkbox. I tend to use a class of sr-only
in the CSS to visibly hide things based on Yahoo's example:
.sr-only{
height: 1px;
width: 1px;
overflow: hidden
clip: rect(1px,1px,1px,1px);
position: absolute;
}
Next we create a pseudo element before
the label. For the style rules I'm using the adjacent sibling selector (+
) to apply the rules when the label is an adjacent sibling of the checkbox. I'm keeping your class name so it'll only target labels that're siblings of class newsletter
:
.newsletter:not(checked) + label:before{
content:" ";
display: inline-block;
width:13px;
height: 13px;
border: 2px solid black;
border-radius:3px;
}
The :not(checked)
means apply these rules when newsletter
is not checked.
To change the styles when newsletter
is checked
we change the background colour like this:
.newsletter:checked + label:before{
background-color:black;
}
This way clicking our pseudo checkbox or the text in the label will check the box (clicking the label at all sends focus to the field, so with a checkbox will check it).
How to add CSS styles to checkbox on selection?
You don't need any function for this, you can do this with good understanding of css selectors
You can try:
input[type="checkbox"]:checked+span {
text-decoration: line-through;
color:red;
}
and define your text box like
<input type="checkbox" ><span>Some text label for checkbox</span>
Here is an example
Why cannot change checkbox color whatever I do?
Checkboxes are not able to be styled. You would need a third party js plugin there are many available.
If you want to do this yourself it basically involves hiding the checkbox creating an element and styling that as you want then binding its click event to two functions one to change its look and another to activate the click event of the checkbox.
The same problem will arise when trying to style that little down arrow on a drop-down select element.
Related Topics
How to Reset/Remove Chrome'S Input Highlighting/Focus Border
Changing :Hover to Touch/Click For Mobile Devices
Is !Important Bad For Performance
Add Line Break to ::After or ::Before Pseudo-Element Content
Search Input With an Icon Bootstrap
Webkit: Blurry Text With CSS Scale + Translate3D
Force Refresh of Cached CSS Data
How to Center an Inline-Block Element and If So, How
How to Make Round Corners to Both Inside of a Box and Its Border
What's the Difference Between Inline Styles VS Classes
Using Variables in Property Names in Less (Dynamic Properties/Property Name Interpolation)
Sass Invalid CSS Error: "Expected Expression"
Css @Font-Face Absolute Url from External Domain: Fonts Not Loading in Firefox
How to Make Generated Content Selectable
How to Style Default Confirm Box With Only Css