0

I would like to customize it such that the checkbox color should change on click. Please refer the link and image and suggest how to achieve this as I am not sure how to do it.

Below is the HTML code for checkbox:

<div id="container">
    <div id="btnOuterDIV">
        <div id="btnChkbox">
            <input type="checkbox" id="btnCB" />
        </div>
    </div>
</div>

CSS for the checkbox, it should look like the one in image:

#container {
    padding:50px;
}
#btnOuterDIV {
    height:30px;
    width:80px;
    border:1px solid #ccc;
    border-radius:5px;
}
#btnOuterDIV:hover {
    border-color:#888;
}
#btnChkbox {
    height:15px;
    width:15px;
    padding-top:5px;
    padding-left:5px;
}
mike.dld
  • 2,929
  • 20
  • 21
Vivek
  • 281
  • 2
  • 7
  • 21

5 Answers5

2

You have to make a <label> and style it.

<input type="checkbox" id="cb">
<label for="cb" class="checkbox"></label>

In CSS, there is :checked, you can use it like that :

#cb:checked + .checkbox {
  /* style here */
}

Example here

Rust in Peace
  • 176
  • 2
  • 8
1

[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left:0; top: 2px;
  width: 17px; height: 17px;
  border: 1px solid #aaa;
  background: #f8f8f8;
  border-radius: 3px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
  content: '✔';
  position: absolute;
  top: 3px; left: 4px;
  font-size: 18px;
  line-height: 0.8;
  color: #09ad7e;
  transition: all .2s;
}
[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
}
body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
}
h1 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}
form {
  width: 80px;
  margin: 0 auto;
}
<h1>Custom checkboxes with CSS</h1>


<form action="#">
  <p>
    <input type="checkbox" id="test1" />
    <label for="test1">Red</label>
  </p>
  
</form>
vshah
  • 31
  • 8
  • Thank you very much Mahesh , It worked perfectly fine , but the same doesnt work as expected if i change the background color and the color of arrow , can u pls tell me how to make it work if possible . https://jsfiddle.net/vivekhegde99/hfezs8r1/2/ – Vivek Aug 27 '16 at 15:14
1
    <h1>Custom checkboxes with CSS</h1>


    <form action="#">
      <p>
        <input type="checkbox" id="test1" />
        <label for="test1">Red</label>
      </p>

    </form>



------CSS--------


[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}

[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}


/* checkbox aspect */

[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 2px;
  width: 17px;
  height: 17px;
  border: 1px solid #aaa;
  background: #f8f8f8;
  border-radius: 3px;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .3)
}


/* checked mark aspect */

[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
  content: '✔';
  position: absolute;
  top: 3px;
  left: 4px;
  font-size: 18px;
  line-height: 0.8;
  color: white;
  background-color:#FF3336;
  transition: all .2s;
}

[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
}

body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
  background-color:#4CFF33;
}

h1 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}

form {
  width: 80px;
  margin: 0 auto;
}
vshah
  • 31
  • 8
  • I think I have changed the background color and the color of arrow.Its working.Hope you are satisfied with the changes. – vshah Sep 07 '16 at 14:26
  • Thanks a lot Mahesh and sorry for late reply , It is working properly as expected !! – Vivek Sep 18 '16 at 08:41
0
<!DOCTYPE html>
<html>
<head>
<title>Custom checkbox</title>
</head>
<style>
input[type="checkbox"] {
  visibility: hidden;
}
label {
  cursor: pointer;
}
input[type="checkbox"] + label:before {
  border: 1px solid #ff0000;
  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: #00ff00;
  color: #ff0000;
  content: "\2713";
  text-align: center;
}
</style>
<body>
    <p>
      <input type="checkbox" id="checkbox1" name="CheckOption">
      <label for="checkbox1">Check1</label>
    </p>
    <p>
      <input type="checkbox" id="checkbox2" name="CheckOption">
      <label for="checkbox2">Check2</label>
    </p>
    <p>
      <input type="checkbox" id="checkbox3" name="CheckOption">
      <label for="checkbox3">Check3</label>
    </p>

</body>
</html>
Sato Takeru
  • 1,669
  • 4
  • 12
  • 27
0
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<style>
.checkbox {
    display: inline-flex;
    cursor: pointer;
    position: relative;
}

.checkbox > span {
    color: #34495E;
    padding: 0.5rem 0.25rem;
}

.checkbox > input {
    height: 25px;
    width: 25px;
    -webkit-appearance: none;
    -moz-appearance: none;
    -o-appearance: none;
    appearance: none;
    border: 1px solid #34495E;
    border-radius: 4px;
    outline: none;
    transition-duration: 0.3s;
    background-color: #41B883;
    cursor: pointer;
  }

.checkbox > input:checked {
    border: 1px solid #41B883;
    background-color: #34495E;
}

.checkbox > input:checked + span::before {
    content: '\2713';
    display: block;
    text-align: center;
    color: #41B883;
    position: absolute;
    left: 0.7rem;
    top: 0.2rem;
}

.checkbox > input:active {
    border: 2px solid #34495E;
}
</style>
<body>
  <label class="checkbox">
      <input type="checkbox" />
      <span>Check Me</span>
  </label>
</body>
</html>
Sato Takeru
  • 1,669
  • 4
  • 12
  • 27