Reputation: 12307
I am trying to style a checkbox using the following:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
But the style is not applied. The checkbox still displays its default style. How do I give it the specified style?
Upvotes: 1088
Views: 2452212
Reputation: 21100
The CSS Basic User Interface Module Level 4 adds support for this (finally) via a new solution called accent-color
, and it's actually quite simple, unlike pretty much every other answer here:
input {
accent-color: rebeccapurple;
}
<input type="checkbox" />
Set whatever CSS color (e.g. named value, hex code, etc.) you want as the value of accent-color
, and it will be applied.
This currently works in Chrome (v93+), Edge (v93+), Firefox (v92+), Opera (v79+), and Safari (v15.4+).
Note: Edge, Chrome, and Opera (and possibly Safari; I can't test that) currently don't support alpha channel values via rgba()
either (the RGB values of rgba()
will still "work"; the alpha channel will simply be ignored by the browser). See MDN Browser Support for more information. See also Chromium Bug Tracking for the status of support in Chromium browsers.
Upvotes: 54
Reputation: 71140
The original question and answer are now ~5 years old. As such, this is a little bit of an update.
Firstly, there are a number of approaches when it comes to styling checkboxes. The basic tenet is:
You will need to hide the default checkbox control which is styled by your browser, and cannot be overridden in any meaningful way using CSS.
With the control hidden, you will still need to be able to detect and toggle its checked state.
The checked state of the checkbox will need to be reflected by styling a new element.
The above can be accomplished by a number of means — and you will often hear that using CSS3 pseudo-elements is the right way. Actually, there is no real right or wrong way, it depends on the approach most suitable for the context you will be using it in. That said, I have a preferred one.
Wrap your checkbox in a label
element. This will mean that even when it is hidden, you can still toggle its checked state by clicking anywhere within the label.
Hide your checkbox.
Add a new element after the checkbox which you will style accordingly. It must appear after the checkbox so it can be selected using CSS and styled dependent on the :checked
state. CSS cannot select 'backwards'.
label input {
visibility: hidden;/* <-- Hide the default checkbox. The rest is to hide and allow tabbing, which display:none prevents */
display: block;
height: 0;
width: 0;
position: absolute;
overflow: hidden;
}
label span {/* <-- Style the artificial checkbox */
height: 10px;
width: 10px;
border: 1px solid grey;
display: inline-block;
}
[type=checkbox]:checked + span {/* <-- Style its checked state */
background: black;
}
<label>
<input type='checkbox'>
<span></span>
Checkbox label text
</label>
"But hey!" I hear you shout. What about if I want to show a nice little tick or cross in the box? And I don't want to use background images!
Well, this is where CSS3's pseudo-elements can come into play. These support the content
property which allows you to inject Unicode icons representing either state. Alternatively, you could use a third party font icon source such as font awesome (though make sure you also set the relevant font-family
, e.g. to FontAwesome
)
label input {
display: none; /* Hide the default checkbox */
}
/* Style the artificial checkbox */
label span {
height: 10px;
width: 10px;
border: 1px solid grey;
display: inline-block;
position: relative;
}
/* Style its checked state...with a ticked icon */
[type=checkbox]:checked + span:before {
content: '\2714';
position: absolute;
top: -5px;
left: 0;
}
<label>
<input type='checkbox'>
<span></span>
Checkbox label text
</label>
Upvotes: 172
Reputation: 33
.gf-checkbox-input{
accent-color: #5bbce2;
opacity: 1 !important;
outline: 1px solid rgba(0, 0, 0, 0.2);
}
.gf-checkbox-label {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
word-wrap: break-word;
}
<label className="gf-checkbox-label">
<input type = 'checkbox' className="gf-checkbox-input" />
Checkbox
</label>
Upvotes: 1
Reputation: 4016
Simplest way to change html checkbox color is
input[type='checkbox'] {
accent-color: red;
}
Upvotes: 0
Reputation: 312
No JavaScript or jQuery required.
Change your checkbox style simple way.
input[type="checkbox"] {
display: none;
border: none !important;
box-shadow: none !important;
}
input[type="checkbox"] + label span {
background: url("https://i.ibb.co/mG7QRYw/62a9b364a056b98f7e02705a-checkboxunselected.png");
width: 24px;
height: 24px;
display: inline-block;
vertical-align: middle;
background-size: 100%;
}
input[type="checkbox"]:checked + label span {
background: url(https://svgur.com/i/upi.svg);
width: 24px;
height: 24px;
vertical-align: middle;
background-size: 100%;
}
<input type="checkbox" id="option" />
<label for="option"> <span></span> Click me </label>
Upvotes: 4
Reputation: 10662
accent-color
Use the new accent-color
property and make certain to meet a proper contrast ratio of 3:1 to ensure accessibility. This also works for radio
buttons.
.red-input {
accent-color: #9d3039;
height: 20px; /* not needed */
width: 20px; /* not needed */
}
<input class="red-input" type="checkbox" />
<!-- Radio button example -->
<input class="red-input" type="radio" />
I have been scrolling and scrolling and tons of these answers simply throw accessibility out the door and violate WCAG in more than one way. I threw in radio buttons since most of the time when you're using custom checkboxes you want custom radio buttons too.
Fiddles:
Late to the party but somehow this is still difficult in 2019, 2020, 2021 so I have added my three solutions which are accessible and easy to drop in.
These are all JavaScript free, accessible, and external library free*...
If you want to plug-n-play with any of these just copy the style sheet from the fiddles, edit the color codes in the CSS to fit your needs, and be on your way. You can add a custom svg checkmark icon if you want for the checkboxes. I've added lots of comments for those non-CSS'y folks.
If you have long text or a small container and are encountering text wrapping underneath the checkbox or radio button input then just convert to divs like this.
Longer explanation:
I needed a solution that does not violate WCAG, doesn't rely on JavaScript or external libraries, and that does not break keyboard navigation like tabbing or spacebar to select, that allows focus events, a solution that allows for disabled checkboxes that are both checked and unchecked, and finally a solution where I can customize the look of the checkbox however I want with different background-color
's, border-radius
, svg
backgrounds, etc.
I used some combination of this answer from @Jan Turoň to come up with my own solution which seems to work quite well. I've done a radio button fiddle that uses a lot of the same code from the checkboxes in order to make this work with radio buttons too.
I am still learning accessibility so if I missed something please drop a comment and I will try to correct it.
Here is a code example of my checkboxes:
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
/* Text color for the label */
input[type="checkbox"]+span {
cursor: pointer;
font: 16px sans-serif;
color: black;
}
/* Checkbox un-checked style */
input[type="checkbox"]+span:before {
content: '';
border: 1px solid grey;
border-radius: 3px;
display: inline-block;
width: 16px;
height: 16px;
margin-right: 0.5em;
margin-top: 0.5em;
vertical-align: -2px;
}
/* Checked checkbox style (in this case the background is green #e7ffba, change this to change the color) */
input[type="checkbox"]:checked+span:before {
/* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */
background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');
background-repeat: no-repeat;
background-position: center;
/* The size of the checkmark icon, you may/may not need this */
background-size: 25px;
border-radius: 2px;
background-color: #e7ffba;
color: white;
}
/* Adding a dotted border around the active tabbed-into checkbox */
input[type="checkbox"]:focus+span:before,
input[type="checkbox"]:not(:disabled)+span:hover:before {
/* Visible in the full-color space */
box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
/* Visible in Windows high-contrast themes
box-shadow will be hidden in these modes and
transparency will not be hidden in high-contrast
thus box-shadow will not show but the outline will
providing accessibility */
outline-color: transparent; /*switch to transparent*/
outline-width: 2px;
outline-style: dotted;
}
/* Disabled checkbox styles */
input[type="checkbox"]:disabled+span {
cursor: default;
color: black;
opacity: 0.5;
}
/* Styles specific to this fiddle that you do not need */
body {
padding: 1em;
}
h1 {
font-size: 18px;
}
<h1>
NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn
</h1>
<p>You can easily change the background color, checkbox symbol, border-radius, etc.</p>
<label>
<input type="checkbox">
<span>Try using tab and space</span>
</label>
<br>
<label>
<input type="checkbox" checked disabled>
<span>Disabled Checked Checkbox</span>
</label>
<br>
<label>
<input type="checkbox" disabled>
<span>Disabled Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Normal Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Another Normal Checkbox</span>
</label>
Upvotes: 133
Reputation: 5251
It's now possible to change all of a checkbox's colors by using only CSS:
accent-color
CSS property is useful for changing a checkbox color when it's checked.filter
property.For simple changes, you can use brightness
to turn the white background color darker into grays or black, and using hue-rotate
you can give colors to the borders (at least in Firefox). However, if you want to fully change the background to any colors, you can also do that with filters
, but you'll have to do some trickery where you combine multiple filters based on the approach in this question: How to transform black into any given color using only CSS filters
Fortunately, this complex approach seems to works in all browsers and there are generators you can use to calculate the filters
needed for whichever colors you want: https://codepen.io/jumarjuaton/full/mdJYWYq
(this link specifically transforms from white, unlike the question linked above)
See examples below:
/* Disable 'filters' while checked since they impact 'accent-color'. */
input[type='checkbox']:checked {
filter: none;
}
input[type='radio']:checked {
filter: none;
}
/**
* We use 'accent-color' for colors in the checked stated and we use
* 'filter' to change the background colors in the unchecked state.
*/
.red-bg-input {
accent-color: red;
filter: invert(85%) sepia(83%) saturate(6726%) hue-rotate(360deg) brightness(111%) contrast(111%);
}
.green-bg-input {
accent-color: green;
filter: invert(49%) sepia(65%) saturate(7149%) hue-rotate(92deg) brightness(89%) contrast(103%);
}
.blue-bg-input {
accent-color: blue;
filter: invert(30%) sepia(99%) saturate(6101%) hue-rotate(202deg) brightness(52%) contrast(136%);
}
.teal-bg-input {
accent-color: teal;
filter: invert(58%) sepia(77%) saturate(3139%) hue-rotate(157deg) brightness(89%) contrast(101%)
}
.dark-input {
accent-color: black;
filter: brightness(0%) saturate(100) hue-rotate(25deg);
}
.gray-input {
accent-color: purple;
filter: brightness(60%) saturate(100) hue-rotate(25deg);
}
.orange-bg-input {
accent-color: orange;
filter: invert(63%) sepia(28%) saturate(7249%) hue-rotate(0deg) brightness(103%) contrast(105%);
}
<input type="checkbox" />
<input type="checkbox" checked/>
<input type="radio" />
<input type="radio" checked />
Defaults
<br>
<input class="red-bg-input" type="checkbox" />
<input class="red-bg-input" type="checkbox" checked/>
<input class="red-bg-input" type="radio" />
<input class="red-bg-input" type="radio" checked/>
Red
<br>
<input class="green-bg-input" type="checkbox" />
<input class="green-bg-input" type="checkbox" checked/>
<input class="green-bg-input" type="radio" />
<input class="green-bg-input" type="radio" checked/>
Green
<br>
<input class="blue-bg-input" type="checkbox" />
<input class="blue-bg-input" type="checkbox" checked/>
<input class="blue-bg-input" type="radio" />
<input class="blue-bg-input" type="radio" checked/>
Blue
<br>
<input class="teal-bg-input" type="checkbox" />
<input class="teal-bg-input" type="checkbox" checked/>
<input class="teal-bg-input" type="radio" />
<input class="teal-bg-input" type="radio" checked/>
Teal
<br>
<input class="orange-bg-input" type="checkbox" />
<input class="orange-bg-input" type="checkbox" checked/>
<input class="orange-bg-input" type="radio" />
<input class="orange-bg-input" type="radio" checked/>
Orange
<br>
<input class="dark-input" type="checkbox" />
<input class="dark-input" type="checkbox" checked/>
<input class="dark-input" type="radio" />
<input class="dark-input" type="radio" checked/>
Black/dark
<br>
<input class="gray-input" type="checkbox" />
<input class="gray-input" type="checkbox" checked/>
<input class="gray-input" type="radio" />
<input class="gray-input" type="radio" checked/>
Purple + Gray background
Upvotes: 2
Reputation: 14012
You can avoid adding extra markup. This works everywhere except IE via setting CSS appearance
:
input[type="checkbox"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/* Styling checkbox */
width: 16px;
height: 16px;
background-color: red;
}
input[type="checkbox"]:checked {
background-color: green;
}
<input type="checkbox" />
Upvotes: 29
Reputation: 2241
Modify the checkbox style with plain CSS. This does not require any JavaScript or HTML manipulation:
.form input[type="checkbox"]:before {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f096";
opacity: 1 !important;
margin-top: -25px;
appearance: none;
background: #fff;
}
.form input[type="checkbox"]:checked:before {
content: "\f046";
}
.form input[type="checkbox"] {
font-size: 22px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form class="form">
<input type="checkbox" />
</form>
Upvotes: 7
Reputation: 623
After a lot of search and testing I got this solution which is simple to implement and easier to customize. In this solution:
Simple put the flowing CSS at the top of your page and all checkboxes style will change like this:
input[type=checkbox] {
transform: scale(1.5);
}
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
input[type=checkbox]:after,
input[type=checkbox]::after {
content: " ";
background-color: #fff;
display: inline-block;
margin-left: 10px;
padding-bottom: 5px;
color: #00BFF0;
width: 22px;
height: 25px;
visibility: visible;
border: 1px solid #00BFF0;
padding-left: 3px;
border-radius: 5px;
}
input[type=checkbox]:checked:after,
input[type=checkbox]:checked::after {
content: "\2714";
padding: -5px;
font-weight: bold;
}
<input type="checkbox" id="checkbox1" />
<label for="checkbox1">Checkbox</label>
Upvotes: 37
Reputation: 1217
This helped me to change style (color) for checkbox
input[type=checkbox] {
accent-color: red;
}
We can also use the same for radio buttons.
Upvotes: 3
Reputation: 7289
Hide the input and add a before to the next element that create a checkbox equivalent
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label:before {
content:'';
-webkit-appearance: none;
background-color: transparent;
border: 2px solid #0079bf;
box-shadow: 0 1px 2px rgba(0, 0, 0, .05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
padding: 10px;
display: inline-block;
position: relative;
vertical-align: middle;
cursor: pointer;
margin-right: 5px;
background-color:red;
}
input[type="checkbox"]:checked + label:before {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%23fff' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3E%3C/svg%3E");
}
Upvotes: 2
Reputation: 350
<!DOCTYPE html>
<html>
<style>
/* 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: white;
border: 2px solid red;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a white background */
.container input:checked ~ .checkmark {
background-color: white;
}
/* 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 green;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
</style>
<body>
<h1>Custom Checkboxes</h1>
<label class="container">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="checkbox">
<span class="checkmark"></span>
</label>
</body>
</html>
Upvotes: 0
Reputation: 761
Recently I found a quite interesting solution to the problem.
You could use appearance: none;
to turn off the checkbox's default style and then write your own over it like described here (Example 4).
input[type=checkbox] {
width: 23px;
height: 23px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
margin-right: 10px;
background-color: #878787;
outline: 0;
border: 0;
display: inline-block;
-webkit-box-shadow: none !important;
-moz-box-shadow: none !important;
box-shadow: none !important;
}
input[type=checkbox]:focus {
outline: none;
border: none !important;
-webkit-box-shadow: none !important;
-moz-box-shadow: none !important;
box-shadow: none !important;
}
input[type=checkbox]:checked {
background-color: green;
text-align: center;
line-height: 15px;
}
<input type="checkbox">
Unfortunately browser support is quite bad for the appearance
option. From my personal testing I only got Opera and Chrome working correctly. But this would be the way to go to keep it simple when better support comes or you only want to use Chrome/Opera.
Upvotes: 25
Reputation: 1623
By using Materialize with a custom stylesheet, you can achieve something like this:
CSS code
.custom_checkbox[type="checkbox"]:checked + span:not(.lever)::before {
border: 2px solid transparent;
border-bottom: 2px solid #ffd600;
border-right: 2px solid #ffd600;
background: transparent;
}
HTML code
<label>
<input type="checkbox" class="custom_checkbox" />
<span>Text</span>
</label>
Demo
Upvotes: 3
Reputation: 134
Custom checkbox with CSS (WebKit browser solution only Chrome, Safari, Mobile browsers)
<input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
<label for="cardAccptance" class="bold"> Save Card for Future Use</label>
CSS:
/* The checkbox-cu */
.checkbox-cu {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 0;
cursor: pointer;
font-size: 16px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox-cu */
.checkbox-cu input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox-cu */
.checkmark {
position: absolute;
top: 4px;
left: 0;
height: 20px;
width: 20px;
background-color: #eee;
border: 1px solid #999;
border-radius: 0;
box-shadow: none;
}
/* On mouse-over, add a grey background color */
.checkbox-cu:hover input~.checkmark {
background-color: #ccc;
}
/* When the checkbox-cu is checked, add a blue background */
.checkbox-cu input:checked~.checkmark {
background-color: transparent;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.checkbox-cu input:checked~.checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.checkbox-cu .checkmark::after {
left: 7px;
top: 3px;
width: 6px;
height: 9px;
border: solid #28a745;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
z-index: 100;
}
Upvotes: 3
Reputation: 32912
I'd follow the advice of SW4's answer. Not anymore: Volomike's answer is far superior to all the answers here (note my suggested improvement in the comment to the answer). Proceed reading this answer if you are curious about alternative approaches, which this answer comments.
First of all, hide the checkbox and to cover it with a custom span, suggesting this HTML:
<label>
<input type="checkbox">
<span>send newsletter</span>
</label>
The wrap in label neatly allows clicking the text without the need of "for-id" attribute linking. However,
visibility: hidden
or display: none
It works by clicking or tapping, but that is a lame way to use checkboxes. Some people still use much more effective Tab to move focus, Space to activate, and hiding with that method disables it. If the form is long, one will save someone's wrists to use tabindex
or accesskey
attributes. And if you observe the system checkbox behavior, there is a decent shadow on hover. The well styled checkbox should follow this behavior.
cobberboy's answer recommends Font Awesome which is usually better than bitmap since fonts are scalable vectors. Working with the HTML above, I'd suggest these CSS rules:
Hide checkboxes
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
I use just negative z-index
since my example uses big enough checkbox skin to cover it fully. I don't recommend left: -999px
since it is not reusable in every layout. Bushan wagh's answer provides a bulletproof way to hide it and convince the browser to use tabindex, so it is a good alternative. Anyway, both is just a hack. The proper way today is appearance: none
, see Joost's answer:
input[type="checkbox"] {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
Style checkbox label
input[type="checkbox"] + span {
font: 16pt sans-serif;
color: #000;
}
Add checkbox skin
input[type="checkbox"] + span:before {
font: 16pt FontAwesome;
content: '\00f096';
display: inline-block;
width: 16pt;
padding: 2px 0 0 3px;
margin-right: 0.5em;
}
\00f096
is Font Awesome's square-o
, padding is adjusted to provide even dotted outline on focus (see below).
Add checkbox checked skin
input[type="checkbox"]:checked + span:before {
content: '\00f046';
}
\00f046
is Font Awesome's check-square-o
, which is not the same width as square-o
, which is the reason for the width style above.
Add focus outline
input[type="checkbox"]:focus + span:before {
outline: 1px dotted #aaa;
}
Safari doesn't provide this feature (see @Jason Sankey's comment), see this answer for Safari-only CSS
Set gray color for disabled checkbox
input[type="checkbox"]:disabled + span {
color: #999;
}
Set hover shadow on non-disabled checkbox
input[type="checkbox"]:not(:disabled) + span:hover:before {
text-shadow: 0 1px 2px #77F;
}
Try to hover the mouse over the checkboxes and use Tab and Shift+Tab to move and Space to toggle.
Upvotes: 61
Reputation: 525
This will make the dotted border, aswell as the background your seeking, also, if you want to set a specific background color for when checked, simply set it where apropriate in below example for your issue :) This solution also sets in a custom checkmark icon, etc.
for an html element like:
<input type="checkbox" />
input[type='checkbox']:checked {
background-color: rgb(60,69,77,0.9);
}
input[type='checkbox']:checked:after {
content: '\2713';
color:white;
}
input[type='checkbox']{
background:#ff0000;
text-align: center;
display: table-cell;
vertical-align: middle;
width: 20px !important;
height: 20px !important;
appearance:none;
border-radius:10%;
border: 1px dotted rgb(60,69,77,0.9);
box-shadow: none;
font-size: 1em;
}
Upvotes: -3
Reputation: 34503
Here's a modern version with a little animation, and simple styling you can customize:
.checkbox {
position: relative;
width: 20px;
height: 20px;
-webkit-appearance: none;
-moz-appearance: none;
-o-appearance: none;
appearance: none;
background: transparent;
border: 2px solid #7C7A7D;
border-radius: 5px;
margin: 0;
outline: none;
transition: 0.5s ease;
opacity: 0.8;
cursor: pointer;
}
.checkbox:checked {
border-color: #7C7A7D;
background-color: #7C7A7D;
}
.checkbox:checked:before {
position: absolute;
left: 2px;
top: -4px;
display: block;
content: '\2713';
text-align: center;
color: #FFF;
font-family: Arial;
font-size: 14px;
font-weight: 800;
}
.checkbox:hover {
opacity: 1.0;
transform: scale(1.05);
}
Upvotes: 3
Reputation: 24886
With pure CSS, nothing fancy with :before
and :after
, no transforms, you can turn off the default appearance and then style it with an inline background image like the following example. This works in Chrome, Firefox, Safari, and now Edge (Chromium Edge).
INPUT[type=checkbox]:focus
{
outline: 1px solid rgba(0, 0, 0, 0.2);
}
INPUT[type=checkbox]
{
background-color: #DDD;
border-radius: 2px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: 17px;
height: 17px;
cursor: pointer;
position: relative;
top: 5px;
}
INPUT[type=checkbox]:checked
{
background-color: #409fd6;
background: #409fd6 url("data:image/gif;base64,R0lGODlhCwAKAIABAP////3cnSH5BAEKAAEALAAAAAALAAoAAAIUjH+AC73WHIsw0UCjglraO20PNhYAOw==") 3px 3px no-repeat;
}
<form>
<label><input type="checkbox"> I Agree To Terms & Conditions</label>
</form>
Upvotes: 52
Reputation: 48590
Here is an example, with theme support. It is a modern approach with CSS transitions. There is absolutely no JavaScript required.
I derived the following code linked in this comment; on a related question.
Edit: I added radio buttons, as maxshuty suggests.
const selector = '.grid-container > .grid-row > .grid-col[data-theme="retro"]';
const main = () => {
new CheckboxStyler().run(selector);
new RadioStyler().run(selector);
};
/*
* This is only used to add the wrapper class and checkmark span to an existing DOM,
* to make this CSS work.
*/
class AbstractInputStyler {
constructor(options) {
this.opts = options;
}
run(parentSelector) {
let wrapperClass = this.opts.wrapperClass;
let parent = document.querySelector(parentSelector) || document.getElementsByTagName('body')[0];
let labels = parent.querySelectorAll('label');
if (labels.length) labels.forEach(label => {
if (label.querySelector(`input[type="${this.opts._inputType}"]`)) {
if (!label.classList.contains(wrapperClass)) {
label.classList.add(wrapperClass);
label.appendChild(this.__createDefaultNode());
}
}
});
return this;
}
/* @protected */
__createDefaultNode() {
let checkmark = document.createElement('span');
checkmark.className = this.opts._activeClass;
return checkmark;
}
}
class CheckboxStyler extends AbstractInputStyler {
constructor(options) {
super(Object.assign({
_inputType: 'checkbox',
_activeClass: 'checkmark'
}, CheckboxStyler.defaultOptions, options));
}
}
CheckboxStyler.defaultOptions = {
wrapperClass: 'checkbox-wrapper'
};
class RadioStyler extends AbstractInputStyler {
constructor(options) {
super(Object.assign({
_inputType: 'radio',
_activeClass: 'pip'
}, RadioStyler.defaultOptions, options));
}
}
RadioStyler.defaultOptions = {
wrapperClass: 'radio-wrapper'
};
main();
/* Theming */
:root {
--background-color: #FFF;
--font-color: #000;
--checkbox-default-background: #EEE;
--checkbox-hover-background: #CCC;
--checkbox-disabled-background: #AAA;
--checkbox-selected-background: #1A74BA;
--checkbox-selected-disabled-background: #6694B7;
--checkbox-checkmark-color: #FFF;
--checkbox-checkmark-disabled-color: #DDD;
}
[data-theme="dark"] {
--background-color: #111;
--font-color: #EEE;
--checkbox-default-background: #222;
--checkbox-hover-background: #444;
--checkbox-disabled-background: #555;
--checkbox-selected-background: #2196F3;
--checkbox-selected-disabled-background: #125487;
--checkbox-checkmark-color: #EEE;
--checkbox-checkmark-disabled-color: #999;
}
[data-theme="retro"] {
--background-color: #FFA;
--font-color: #000;
--checkbox-default-background: #EEE;
--checkbox-hover-background: #FFF;
--checkbox-disabled-background: #BBB;
--checkbox-selected-background: #EEE;
--checkbox-selected-disabled-background: #BBB;
--checkbox-checkmark-color: #F44;
--checkbox-checkmark-disabled-color: #D00;
}
/* General styles */
html {
width: 100%;
height: 100%;
}
body {
/*background: var(--background-color); -- For demo, moved to column. */
/*color: var(--font-color); -- For demo, moved to column. */
background: #777;
width: calc(100% - 0.5em);
height: calc(100% - 0.5em);
padding: 0.25em;
}
h1 {
font-size: 1.33em !important;
}
h2 {
font-size: 1.15em !important;
margin-top: 1em;
}
/* Grid style - using flex */
.grid-container {
display: flex;
height: 100%;
flex-direction: column;
flex: 1;
}
.grid-row {
display: flex;
flex-direction: row;
flex: 1;
margin: 0.25em 0;
}
.grid-col {
display: flex;
flex-direction: column;
height: 100%;
padding: 0 1em;
flex: 1;
margin: 0 0.25em;
/* If not demo, remove and uncomment the body color rules */
background: var(--background-color);
color: var(--font-color);
}
.grid-cell {
width: 100%;
height: 100%;
}
/* The checkbox wrapper */
.checkbox-wrapper,
.radio-wrapper {
display: block;
position: relative;
padding-left: 1.5em;
margin-bottom: 0.5em;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox and radio buttons */
.checkbox-wrapper input[type="checkbox"] {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
.radio-wrapper input[type="radio"] {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkbox-wrapper .checkmark {
position: absolute;
top: 0;
left: 0;
height: 1em;
width: 1em;
background-color: var(--checkbox-default-background);
transition: all 0.2s ease-in;
}
.radio-wrapper .pip {
position: absolute;
top: 0;
left: 0;
height: 1em;
width: 1em;
border-radius: 50%;
background-color: var(--checkbox-default-background);
transition: all 0.2s ease-in;
}
/* Disabled style */
.checkbox-wrapper input[type="checkbox"]:disabled~.checkmark,
.radio-wrapper input[type="radio"]:disabled~.pip {
cursor: not-allowed;
background-color: var(--checkbox-disabled-background);
color: var(--checkbox-checkmark-disabled-color);
}
.checkbox-wrapper input[type="checkbox"]:disabled~.checkmark:after,
.radio-wrapper input[type="radio"]:disabled~.pip:after {
color: var(--checkbox-checkmark-disabled-color);
}
.checkbox-wrapper input[type="checkbox"]:disabled:checked~.checkmark,
.radio-wrapper input[type="radio"]:disabled:checked~.pip {
background-color: var(--checkbox-selected-disabled-background);
}
/* On mouse-over, add a grey background color */
.checkbox-wrapper:hover input[type="checkbox"]:not(:disabled):not(:checked)~.checkmark,
.radio-wrapper:hover input[type="radio"]:not(:disabled):not(:checked)~.pip {
background-color: var(--checkbox-hover-background);
}
/* When the checkbox is checked, add a blue background */
.checkbox-wrapper input[type="checkbox"]:checked~.checkmark,
.radio-wrapper input[type="radio"]:checked~.pip {
background-color: var(--checkbox-selected-background);
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkbox-wrapper .checkmark:after {
display: none;
width: 100%;
position: absolute;
text-align: center;
content: "\2713";
color: var(--checkbox-checkmark-color);
line-height: 1.1em;
}
.radio-wrapper .pip:after {
display: none;
width: 100%;
position: absolute;
text-align: center;
content: "\2022";
color: var(--checkbox-checkmark-color);
font-size: 1.5em;
top: -0.2em;
}
/* Show the checkmark when checked */
.checkbox-wrapper input[type="checkbox"]:checked~.checkmark:after {
display: block;
line-height: 1.1em;
}
.radio-wrapper input[type="radio"]:checked~.pip:after {
display: block;
line-height: 1.1em;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet" />
<div class="grid-container">
<div class="grid-row">
<div class="grid-col">
<div class="grid-cell">
<h1>Pure CSS</h1>
<h2>Checkbox</h2>
<label class="checkbox-wrapper">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Three
<input type="checkbox" checked disabled>
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Four
<input type="checkbox" disabled>
<span class="checkmark"></span>
</label>
<h2>Radio</h2>
<label class="radio-wrapper">One
<input type="radio" name="group-x">
<span class="pip"></span>
</label>
<label class="radio-wrapper">Two
<input type="radio" name="group-x">
<span class="pip"></span>
</label>
<label class="radio-wrapper">Three
<input type="radio" name="group-x" checked disabled>
<span class="pip"></span>
</label>
<label class="radio-wrapper">Four
<input type="radio" name="group-x" disabled>
<span class="pip"></span>
</label>
</div>
</div>
<div class="grid-col" data-theme="dark">
<div class="grid-cell">
<h1>Pure CSS</h1>
<h2>Checkbox</h2>
<label class="checkbox-wrapper">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Three
<input type="checkbox" checked disabled>
<span class="checkmark"></span>
</label>
<label class="checkbox-wrapper">Four
<input type="checkbox" disabled>
<span class="checkmark"></span>
</label>
<h2>Radio</h2>
<label class="radio-wrapper">One
<input type="radio" name="group-y">
<span class="pip"></span>
</label>
<label class="radio-wrapper">Two
<input type="radio" name="group-y">
<span class="pip"></span>
</label>
<label class="radio-wrapper">Three
<input type="radio" name="group-y" checked disabled>
<span class="pip"></span>
</label>
<label class="radio-wrapper">Four
<input type="radio" name="group-y" disabled>
<span class="pip"></span>
</label>
</div>
</div>
<div class="grid-col" data-theme="retro">
<div class="grid-cell">
<h1>JS + CSS</h1>
<h2>Checkbox</h2>
<label>One <input type="checkbox" checked="checked"></label>
<label>Two <input type="checkbox"></label>
<label>Three <input type="checkbox" checked disabled></label>
<label>Four <input type="checkbox" disabled></label>
<h2>Radio</h2>
<label>One <input type="radio" name="group-z" checked="checked"></label>
<label>Two <input type="radio" name="group-z"></label>
<label>Three <input type="radio" name="group-z" checked disabled></label>
<label>Four <input type="radio" name="group-z" disabled></label>
</div>
</div>
</div>
</div>
Upvotes: 2
Reputation: 9406
I always use pseudo elements :before
and :after
for changing the appearance of checkboxes and radio buttons. it's works like a charm.
Refer this link for more info
Steps
visibility:hidden
or opacity:0
or position:absolute;left:-9999px
etc.:before
element and pass either an empty or a non-breaking space '\00a0'
;:checked
state, pass the unicode content: "\2713"
, which is a checkmark;:focus
style to make the checkbox accessible.Here is how I did it.
.box {
background: #666666;
color: #ffffff;
width: 250px;
padding: 10px;
margin: 1em auto;
}
p {
margin: 1.5em 0;
padding: 0;
}
input[type="checkbox"] {
visibility: hidden;
}
label {
cursor: pointer;
}
input[type="checkbox"] + label:before {
border: 1px solid #333;
content: "\00a0";
display: inline-block;
font: 16px/1em sans-serif;
height: 16px;
margin: 0 .25em 0 0;
padding: 0;
vertical-align: top;
width: 16px;
}
input[type="checkbox"]:checked + label:before {
background: #fff;
color: #333;
content: "\2713";
text-align: center;
}
input[type="checkbox"]:checked + label:after {
font-weight: bold;
}
input[type="checkbox"]:focus + label::before {
outline: rgb(59, 153, 252) auto 5px;
}
<div class="content">
<div class="box">
<p>
<input type="checkbox" id="c1" name="cb">
<label for="c1">Option 01</label>
</p>
<p>
<input type="checkbox" id="c2" name="cb">
<label for="c2">Option 02</label>
</p>
<p>
<input type="checkbox" id="c3" name="cb">
<label for="c3">Option 03</label>
</p>
</div>
</div>
Much more stylish using :before
and :after
body{
font-family: sans-serif;
}
.container {
margin-top: 50px;
margin-left: 20px;
margin-right: 20px;
}
.checkbox {
width: 100%;
margin: 15px auto;
position: relative;
display: block;
}
.checkbox input[type="checkbox"] {
width: auto;
opacity: 0.00000001;
position: absolute;
left: 0;
margin-left: -20px;
}
.checkbox label {
position: relative;
}
.checkbox label:before {
content: '';
position: absolute;
left: 0;
top: 0;
margin: 4px;
width: 22px;
height: 22px;
transition: transform 0.28s ease;
border-radius: 3px;
border: 2px solid #7bbe72;
}
.checkbox label:after {
content: '';
display: block;
width: 10px;
height: 5px;
border-bottom: 2px solid #7bbe72;
border-left: 2px solid #7bbe72;
-webkit-transform: rotate(-45deg) scale(0);
transform: rotate(-45deg) scale(0);
transition: transform ease 0.25s;
will-change: transform;
position: absolute;
top: 12px;
left: 10px;
}
.checkbox input[type="checkbox"]:checked ~ label::before {
color: #7bbe72;
}
.checkbox input[type="checkbox"]:checked ~ label::after {
-webkit-transform: rotate(-45deg) scale(1);
transform: rotate(-45deg) scale(1);
}
.checkbox label {
min-height: 34px;
display: block;
padding-left: 40px;
margin-bottom: 0;
font-weight: normal;
cursor: pointer;
vertical-align: sub;
}
.checkbox label span {
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
.checkbox input[type="checkbox"]:focus + label::before {
outline: 0;
}
<div class="container">
<div class="checkbox">
<input type="checkbox" id="checkbox" name="" value="">
<label for="checkbox"><span>Checkbox</span></label>
</div>
<div class="checkbox">
<input type="checkbox" id="checkbox2" name="" value="">
<label for="checkbox2"><span>Checkbox</span></label>
</div>
</div>
Upvotes: 108
Reputation: 575
For those using SCSS (or easily converted to SASS), the following will be helpful. Effectively, make an element next to the checkbox, which is the one that you will style. When the checkbox is clicked, the CSS restyles the sister element (to your new, checked style). Code is below:
label.checkbox {
input[type="checkbox"] {
visibility: hidden;
display: block;
height: 0;
width: 0;
position: absolute;
overflow: hidden;
&:checked + span {
background: $accent;
}
}
span {
cursor: pointer;
height: 15px;
width: 15px;
border: 1px solid $accent;
border-radius: 2px;
display: inline-block;
transition: all 0.2s $interpol;
}
}
<label class="checkbox">
<input type="checkbox" />
<span></span>
Label text
</label>
Upvotes: 5
Reputation: 35379
You can simply use appearance: none
on modern browsers, so that there is no default styling and all your styles are applied properly:
input[type=checkbox] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
display: inline-block;
width: 2em;
height: 2em;
border: 1px solid gray;
outline: none;
vertical-align: middle;
}
input[type=checkbox]:checked {
background-color: blue;
}
Upvotes: 14
Reputation: 8998
Yikes! All these workarounds have led me to the conclusion that the HTML checkbox kind of sucks if you want to style it.
As a forewarning, this isn't a CSS implementation. I just thought I'd share the workaround I came up with in case anyone else might find it useful.
I used the HTML5 canvas
element.
The upside to this is that you don't have to use external images and can probably save some bandwidth.
The downside is that if a browser for some reason can't render it correctly, then there's no fallback. Though whether this remains an issue in 2017 is debatable.
I found the old code quite ugly, so I decided to give it a rewrite.
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offset){
this.ctx.fillStyle = color;
this.ctx.fillRect(offset, offset,
this.width - offset * 2, this.height - offset * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0);
this.draw_rect("#FFFFFF", 1);
if(this.is_checked())
this.draw_rect("#000000", 2);
},
is_checked: function(){
return !!this.state;
}
});
Here's a working demo.
The new version uses prototypes and differential inheritance to create an efficient system for creating checkboxes. To create a checkbox:
var my_checkbox = Checkbox.create();
This will immediately add the checkbox to the DOM and hook up the events. To query whether a checkbox is checked:
my_checkbox.is_checked(); // True if checked, else false
Also important to note is that I got rid of the loop.
Something I neglected to mention in the last update is that using the canvas has more advantages than just making a checkbox that looks however you want it to look. You could also create multi-state checkboxes, if you wanted to.
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
Object.prototype.extend = function(newobj){
var oldobj = Object.create(this);
for(prop in newobj)
oldobj[prop] = newobj[prop];
return Object.seal(oldobj);
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offsetx, offsety){
this.ctx.fillStyle = color;
this.ctx.fillRect(offsetx, offsety,
this.width - offsetx * 2, this.height - offsety * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0, 0);
this.draw_rect("#FFFFFF", 1, 1);
this.draw_state();
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
},
is_checked: function(){
return this.state == 1;
}
});
var Checkbox3 = Checkbox.extend({
ev_click: function(self){
return function(unused){
self.state = (self.state + 1) % 3;
self.draw();
}
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
if(this.is_partial())
this.draw_rect("#000000", 2, (this.height - 2) / 2);
},
is_partial: function(){
return this.state == 2;
}
});
I modified slightly the Checkbox
used in the last snippet so that it is more generic, making it possible to "extend" it with a checkbox that has 3 states. Here's a demo. As you can see, it already has more functionality than the built-in checkbox.
Something to consider when you're choosing between JavaScript and CSS.
First, set up a canvas
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
checked = 0; // The state of the checkbox
canvas.width = canvas.height = 15; // Set the width and height of the canvas
document.body.appendChild(canvas);
document.body.appendChild(document.createTextNode(' Togglable Option'));
Next, devise a way to have the canvas update itself.
(function loop(){
// Draws a border
ctx.fillStyle = '#ccc';
ctx.fillRect(0,0,15,15);
ctx.fillStyle = '#fff';
ctx.fillRect(1, 1, 13, 13);
// Fills in canvas if checked
if(checked){
ctx.fillStyle = '#000';
ctx.fillRect(2, 2, 11, 11);
}
setTimeout(loop, 1000/10); // Refresh 10 times per second
})();
The last part is to make it interactive. Luckily, it's pretty simple:
canvas.onclick = function(){
checked = !checked;
}
This is where you might have problems in IE, due to their weird event handling model in JavaScript.
I hope this helps someone; it definitely suited my needs.
Upvotes: 6
Reputation: 1545
From my googling, this is the easiest way for checkbox styling. Just add :after
and :checked:after
CSS based on your design.
body{
background: #DDD;
}
span{
margin-left: 30px;
}
input[type=checkbox] {
cursor: pointer;
font-size: 17px;
visibility: hidden;
position: absolute;
top: 0;
left: 0;
transform: scale(1.5);
}
input[type=checkbox]:after {
content: " ";
background-color: #fff;
display: inline-block;
color: #00BFF0;
width: 14px;
height: 19px;
visibility: visible;
border: 1px solid #FFF;
padding: 0 3px;
margin: 2px 0;
border-radius: 8px;
box-shadow: 0 0 15px 0 rgba(0,0,0,0.08), 0 0 2px 0 rgba(0,0,0,0.16);
}
input[type=checkbox]:checked:after {
content: "\2714";
display: unset;
font-weight: bold;
}
<input type="checkbox"> <span>Select Text</span>
Upvotes: 8
Reputation: 429
It seems you can change the colour of the checkbox in grayscale by using CSS only.
The following converts the checkboxes from black to gray (which was about what I wanted):
input[type="checkbox"] {
opacity: .5;
}
Upvotes: 0
Reputation: 91
Since browsers like Edge and Firefox do not support :before :after on checkbox input tags, here is an alternative purely with HTML and CSS. Of course you should edit CSS according to your requirements.
Make the HTML for checkbox like this:
<div class='custom-checkbox'>
<input type='checkbox' />
<label>
<span></span>
Checkbox label
</label>
</div>
Apply this style for the checkbox to change the color label
<style>
.custom-checkbox {
position: relative;
}
.custom-checkbox input{
position: absolute;
left: 0;
top: 0;
height:15px;
width: 50px; /* Expand the checkbox so that it covers */
z-index : 1; /* the label and span, increase z-index to bring it over */
opacity: 0; /* the label and set opacity to 0 to hide it. */
}
.custom-checkbox input+label {
position: relative;
left: 0;
top: 0;
padding-left: 25px;
color: black;
}
.custom-checkbox input+label span {
position: absolute; /* a small box to display as checkbox */
left: 0;
top: 0;
height: 15px;
width: 15px;
border-radius: 2px;
border: 1px solid black;
background-color: white;
}
.custom-checkbox input:checked+label { /* change label color when checked */
color: orange;
}
.custom-checkbox input:checked+label span{ /* change span box color when checked */
background-color: orange;
border: 1px solid orange;
}
</style>
Upvotes: 2
Reputation: 628
You can use iCheck. It is customized checkboxes and radio buttons for jQuery & Zepto, and maybe it will help you.
Make sure jQuery v1.7+ is loaded before the icheck.js
Insert before in your HTML (replace your-path and color-scheme):
<link href="your-path/minimal/color-scheme.css" rel="stylesheet">
<script src="your-path/icheck.js"></script>
Example for a Red color scheme:
<link href="your-path/minimal/red.css" rel="stylesheet">
<script src="your-path/icheck.js"></script>
Add some checkboxes and radio buttons to your HTML:
<input type="checkbox">
<input type="checkbox" checked>
<input type="radio" name="iCheck">
<input type="radio" name="iCheck" checked>
Add JavaScript to your HTML to launch iCheck plugin:
<script>
$(document).ready(function(){
$('input').iCheck({
checkboxClass: 'icheckbox_minimal',
radioClass: 'iradio_minimal',
increaseArea: '20%' // Optional
});
});
</script>
For different from black color schemes use this code (example for Red):
<script>
$(document).ready(function(){
$('input').iCheck({
checkboxClass: 'icheckbox_minimal-red',
radioClass: 'iradio_minimal-red',
increaseArea: '20%' // Optional
});
});
</script>
Done
Upvotes: -2