CSS checkbox input styling
Asked Answered
L

13

240

Any style for input affects every input element. Is there a way to specify styling to apply for only checkboxes without applying a class to each check box element?

Logic answered 13/7, 2009 at 17:22 Comment(0)
C
411

With CSS 2 you can do this:

input[type='checkbox'] { ... }

This should be pretty widely supported by now. See support for browsers

Costanza answered 13/7, 2009 at 17:23 Comment(3)
I believe IE 7 and greater support attribute selectors, which are actually CSS 2.1. quirksmode.org/css/contents.htmlGumma
@realtebo: Note many styles (border, background, etc) cannot be applied directly to HTML checkboxes.Fenderson
@RoyTinker You would use outline, not border, for checkboxes.Padron
P
63

I create my own solution without label

input[type=checkbox] {
         position: relative;
	       cursor: pointer;
    }
    input[type=checkbox]:before {
         content: "";
         display: block;
         position: absolute;
         width: 16px;
         height: 16px;
         top: 0;
         left: 0;
         border: 2px solid #555555;
         border-radius: 3px;
         background-color: white;
}
    input[type=checkbox]:checked:after {
         content: "";
         display: block;
         width: 5px;
         height: 10px;
         border: solid black;
         border-width: 0 2px 2px 0;
         -webkit-transform: rotate(45deg);
         -ms-transform: rotate(45deg);
         transform: rotate(45deg);
         position: absolute;
         top: 2px;
         left: 6px;
}
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">

input[type=checkbox] {
         position: relative;
	       cursor: pointer;
    }
    input[type=checkbox]:before {
         content: "";
         display: block;
         position: absolute;
         width: 20px;
         height: 20px;
         top: 0;
         left: 0;
         background-color:#e9e9e9;
}
input[type=checkbox]:checked:before {
         content: "";
         display: block;
         position: absolute;
         width: 20px;
         height: 20px;
         top: 0;
         left: 0;
         background-color:#1E80EF;
}
    input[type=checkbox]:checked:after {
         content: "";
         display: block;
         width: 5px;
         height: 10px;
         border: solid white;
         border-width: 0 2px 2px 0;
         -webkit-transform: rotate(45deg);
         -ms-transform: rotate(45deg);
         transform: rotate(45deg);
         position: absolute;
         top: 2px;
         left: 6px;
}
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
Pirzada answered 28/12, 2017 at 15:19 Comment(6)
How to do with the gray background as in this image (i.stack.imgur.com/Q23fy.png), if you can look at my post (pt.https://mcmap.net/q/119334/-how-can-i-guarantee-catching-a-exception_stack_overflow-structured-exception-in-c-under-visual-studio-2005). Thanks.Reyreyes
Fantastic! Can you put the answer in my post for me to mark as done?Reyreyes
After hours of searching and testing this is the only thing that worked for me. Thanks!Decapod
Awesome examplesHyonhyoscine
Great, I'd use position: relative;Wing
doesn't work in Firefox, tooChela
V
25

Something I recently discovered for styling Radio Buttons AND Checkboxes. Before, I had to use jQuery and other things. But this is stupidly simple.

input[type=radio] {
    padding-left:5px;
    padding-right:5px;
    border-radius:15px;

    -webkit-appearance:button;

    border: double 2px #00F;

    background-color:#0b0095;
    color:#FFF;
    white-space: nowrap;
    overflow:hidden;

    width:15px;
    height:15px;
}

input[type=radio]:checked {
    background-color:#000;
    border-left-color:#06F;
    border-right-color:#06F;
}

input[type=radio]:hover {
    box-shadow:0px 0px 10px #1300ff;
}

You can do the same for a checkbox, obviously change the input[type=radio] to input[type=checkbox] and change border-radius:15px; to border-radius:4px;.

Hope this is somewhat useful to you.

Vestryman answered 24/2, 2012 at 15:38 Comment(2)
Really good, too bad that don't work so much on Opera nor IE =(Value
If you want to apply your own styling to a checkbox/input, checkout my blogpost about custom input elements via CSS. Then you can style it however you want with CSS only, including an IE8 fallback.Abject
K
9

Thanks to everyone who contributed to this post. Take a look at just what I was able to accomplish using pure CSS.

input[type=checkbox] {
  position: absolute;
  cursor: pointer;
  width: 0px;
  height: 0px;
}

input[type=checkbox]:checked:before {
      content: "";
  display: block;
  position: absolute;
  width: 34px;
  height: 34px;
  border: 4px solid #FFCB9A;
  border-radius: 20px;
  background-color: #445768;  
  transition: all 0.2s linear;
}


input[type=checkbox]:before {
  content: "";
  display: block;
  position: absolute;
  width: 34px;
  height: 34px;
  border: 4px solid #FFCB9A;
  border-radius: 3px;
  background-color: #445768;
}


input[type=checkbox]:after {
  content: "";
  display: block;
  width: 0px;
  height: 0px;
  border: solid #FFCB9A;
  border-width: 0 0px 0px 0;
  -webkit-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
  position: absolute;
  top: 0px;
  left: 50px;
  transition: all 0.2s linear;
}

input[type=checkbox]:checked:after {
  content: "";
  display: block;
  width: 12px;
  height: 21px;
  border: solid #FFCB9A;
  border-width: 0 5px 5px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 14px;
}
<input type="checkbox">

SUMMARY

So the following controls the actual checkbox which we've hidden by adding h:0 & w:0.

input[type=checkbox] {...}

And the rest controls the virtual checkbox.

This controls the virtual checkbox's background when CHECKED.

input[type=checkbox]:checked:before {...}

This controls the virtual checkbox's background when UNCHECKED.

input[type=checkbox]:before {...}

This controls the virtual checkbox's foreground when UNCHECKED.

input[type=checkbox]:after {...}

This controls the virtual checkbox's foreground when CHECKED.

input[type=checkbox]:checked:after {...}

I really hope it helps you as much as it helped me :)

Killoran answered 7/5, 2021 at 2:49 Comment(2)
This solution seems to work only in chromium based browsers. In Firefox the checkbox has still the default design. JSFiddleJezabelle
Thank you for bringing this to my attention.Killoran
L
8

Classes also work well, such as:

<style>
  form input .checkbox
  {
    /* your checkbox styling */
  }
</style>

<form>
  <input class="checkbox" type="checkbox" />
</form>
Lynwoodlynx answered 5/12, 2011 at 16:0 Comment(4)
It's probably not necessary to nest the .checkbox inside form because you're unlikely to find a checkbox outside of an form. Also input .checkbox should be input.checkboxAlterative
@DuncanWalker I haven't tested it, but wouldn't the form tags be necessary to tie the input control to a default submit control?Lynwoodlynx
@Jim Fell this does not work. If I create two different classes for two different styled checkboxes i.e. .checkbox1 [input='checkbox'] and .checkbox2[input='checkbox'], the styles always will effect both regardless..how do you ensure stylings are separate?Fullblown
@Fullblown You need to give your checkbox elements different class names.Lynwoodlynx
M
4

You can apply only to certain attribute by doing:

input[type="checkbox"] {...}

It explains it here.

Medrek answered 13/7, 2009 at 17:25 Comment(0)
L
4
input[type="checkbox"] {
 /* your style */
}

But this will only work for browsers except IE7 and below, for those you will have to use a class.

Lindo answered 13/7, 2009 at 17:26 Comment(1)
below IE7 it is, IE7 itself is fine with selectors. Tested and see here: kimblim.dk/css-tests/selectorsChenab
A
4

Trident provides the ::-ms-check pseudo-element for checkbox and radio button controls. For example:

<input type="checkbox">
<input type="radio">

::-ms-check {
    color: red;
    background: black;
    padding: 1em;
}

This displays as follows in IE10 on Windows 8:

enter image description here

Aleida answered 29/1, 2014 at 19:20 Comment(0)
M
3

Although CSS does provide a way for you to do the style specific to the checkbox type or another type, there are going to be problems with browsers that do not support this.

I think your only option in this case is going to be to apply classes to your checkboxes.

just add the class="checkbox" to your checkboxes.

Then create that style in your css code.

One thing you could do is this:

main.css

input[type="checkbox"] { /* css code here */ }

ie.css

.checkbox{ /* css code here for ie */ }

Then use the IE specific css include:

<!--[if lt IE 7]>
   <link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

You will still need to add the class for it to work in IE, and it will not work in other non-IE browsers that do not support IE. But it will make your website forward-thinking with css code and as IE gets support, you will be able to remove the ie specific css code and also the css classes from the checkboxes.

Marney answered 13/7, 2009 at 17:31 Comment(1)
thanks for the tip about the 2 style sheet. although im trying to avoid that kind of thing i think i will just create a style for the check box and thats it...Logic
R
2

As IE6 doesn't understand attribute selectors, you can combine a script only seen by IE6 (with conditional comments) and jQuery or IE7.js by Dean Edwards.

IE7(.js) is a JavaScript library to make Microsoft Internet Explorer behave like a standards-compliant browser. It fixes many HTML and CSS issues and makes transparent PNG work correctly under IE5 and IE6.

The choice of using classes or jQuery or IE7.js depends on your likes and dislikes and your other needs (maybe PNG-24 transparency throughout your site without having to rely on PNG-8 with complete transparency that fallbacks to 1-bit transparency on IE6 - only created by Fireworks and pngnq, etc)

Richelle answered 13/7, 2009 at 19:12 Comment(0)
P
2

I tried a few things to modify the default colour offered by the input checkboxes, which I felt were too bulky.But I wanted something simple, so I began exploring various platforms, including gpt and Google, until I came across this answer on YouTube. I hope this helps if anyone else has a similar problem, and it works with a wide range of browsers.

You can also check the link below MDN docs on accent color

.checkbox-zero{
    accent-color: #eda30f;
}
.checkbox-one{
    accent-color: #eb1607;
}
.checkbox-two{
    accent-color:#1aeb07;
}
.checkbox-three{
    accent-color:#eb07d4;
}
<input type="checkbox" class="checkbox-zero">
<input type="checkbox" class="checkbox-one">
<input type="checkbox" class="checkbox-two">
<input type="checkbox" class="checkbox-three">
Putrescine answered 26/9, 2023 at 13:32 Comment(0)
R
1

You can apply your style to checkbox input by using this selector:

input[type='checkbox']{}

but unfortunately, it does not support custom style so you can use "after" and "before" in order to cover the original input: See the: Demo

HTML

 <input
    type="checkbox"
    style="background-color: #d81b60; color: #fff"
    name="checkboxGreen"
  />

CSS

input[type='checkbox'] {
      position: relative;
      cursor: pointer;
      margin: 20px;
 }
 input[type='checkbox']:before {
      content: '';
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      top: 50%;
      left: 50%;
      background-color: inherit;
      outline: 1px rgba(255, 255, 255, 0.2) solid;
      transform: translate(-50%, -50%);
 }
 input[type='checkbox']:checked:before {     
      content: '';
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      top: 50%;
      left: 50%;
      background-color: inherit;
      outline: 1px rgba(255, 255, 255, 0.2) solid;
      transform: translate(-50%, -50%);
      box-shadow: 0px 0px 4px 1px #fff;
 }
 input[type='checkbox']:checked:after {
      content: '';
      display: block;
      width: 6px;
      height: 10px;
      border: solid;
      border-width: 0 2px 2px 0;
      border-color: inherit;
      position: absolute;
      top: 35%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(45deg);
 }

The result:

enter image description here

Refulgent answered 12/6, 2022 at 17:14 Comment(1)
doesn't work in FirefoxChela
P
0

input[type=checkbox] {
         position: relative;
           cursor: pointer;
    }
    input[type=checkbox]:before {
         content: "";
         display: block;
         position: absolute;
         width: 16px;
         height: 16px;
         top: 0;
         left: 0;
         border: 2px solid #555555;
         border-radius: 3px;
         background-color: white;
}
    input[type=checkbox]:checked:after {
         content: "";
         display: block;
         width: 5px;
         height: 10px;
         border: solid black;
         border-width: 0 2px 2px 0;
         -webkit-transform: rotate(45deg);
         -ms-transform: rotate(45deg);
         transform: rotate(45deg);
         position: absolute;
         top: 2px;
         left: 6px;
}
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
Putrescine answered 26/9, 2023 at 12:19 Comment(1)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now.Pascual

© 2022 - 2024 — McMap. All rights reserved.