2

I have created a div with custom checkbox and i want to change the checkbox image by the custom image. and i have done it but i am unable to change the size of the image.Please help me.

Any help will be appreciated.

            <div class="left">

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

And CSS which i have used.

.checkbox {
display: inline-block;
cursor: pointer;
font-size: 13px; line-height:18px;
}
input[type=checkbox] {
display:none;   
}
.checkbox:before {
content: "";
display: inline-block;
width: 25px;
height: 25px;
vertical-align: middle;
background-color: rgb(248,183,51);
color: #f3f3f3;
text-align: center;
}
input[type=checkbox]:checked + .checkbox:before {
content: url("images/Tick.png");
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
font-size: 15px;
}
Neha Purwar
  • 175
  • 1
  • 13

2 Answers2

1

You can do this without using images, just with simple characters and CSS, so you can make change on your custom checkbox just with adjusting font-size or changing color.

Please see the below example:

/* Base for label styling */
[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 .1s;
  transform-origin: center;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
  transform-origin: center;
}
[type="checkbox"]:checked + label:after {
  opacity: 1;
  transform: scale(1);
  transform-origin: center;
}
/* disabled checkbox */
[type="checkbox"]:disabled:not(:checked) + label:before,
[type="checkbox"]:disabled:checked + label:before {
  box-shadow: none;
  border-color: #bbb;
  background-color: #ddd;
}
[type="checkbox"]:disabled:checked + label:after {
  color: #999;
}
[type="checkbox"]:disabled + label {
  color: #aaa;
}
/* accessibility */
[type="checkbox"]:checked:focus + label:before,
[type="checkbox"]:not(:checked):focus + label:before {
  border: 1px dotted blue;
}

/* hover style just for information */
label:hover:before {
  border: 1px solid #4778d9!important;
}






/* Useless styles, just for demo design */

body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
}
h1, h2 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}
h2 {
  margin: 5px 0 2em;
  color: #aaa;
}
form {
  width: 80px;
  margin: 0 auto;
}
.txtcenter {
  margin-top: 4em;
  font-size: .9em;
  text-align: center;
  color: #aaa;
}
.copy {
 margin-top: 2em; 
}
.copy a {
 text-decoration: none;
 color: #4778d9;
}
<form action="#">
  <p>
    <input type="checkbox" id="test1" />
    <label for="test1">Red</label>
  </p>
  <p>
    <input type="checkbox" id="test2" checked="checked" />
    <label for="test2">Yellow</label>
  </p>
  <p>
    <input type="checkbox" id="test3" checked="checked" disabled="disabled" />
    <label for="test3">Green</label>
  </p>
    <p>
      <input type="checkbox" id="test4" disabled="disabled" />
      <label for="test4">Brown</label>
  </p>
</form>

or you can check on CodePen as well: Custom Checkbox Without images


(Edited)

Custom Checkbox background-image property

Please check the following live example for a custom checkbox using background-image:

.checkbox {
display: inline-block;
cursor: pointer;
font-size: 13px; line-height:18px;
}
input[type=checkbox] {
display:none;   
}
.checkbox:before {
content: "";
 display: inline-block;
 width: 25px;
 height: 25px;
 vertical-align: middle;
 background-color: rgb(248,183,51);
 color: #f3f3f3;
 text-align: center;
}
input[type=checkbox]:checked + .checkbox:before {
  background: url("https://cdn1.iconfinder.com/data/icons/windows8_icons_iconpharm/26/checked_checkbox.png") no-repeat center center;
 text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
 font-size: 15px;
 background-size: 10px 10px;
}
<div class="left">
  <input id="Option" type="checkbox">
  <label class="checkbox" for="Option"> </label>
</div>
  • Please make sure that your image is exist in right path.
  • Also please consider you can re-position your background image by setting the horizontal and vertical position for background property as well.
Meghdad Hadidi
  • 1,093
  • 13
  • 28
  • Please suggest me by using image. @Meghdad Hadidi – Neha Purwar May 19 '16 at 08:59
  • @NehaPurwar This is a live version using background-image. As Paran0a said, you can use background-size property to control size of background-image. Also please make sure the image you are using is located in right path. – Meghdad Hadidi May 21 '16 at 04:54
  • @NehaPurwar I have edited my answer for background image solution. – Meghdad Hadidi May 21 '16 at 05:03
  • Very nice. However I am not seeing the blue focus decoration in Chrome 54, Firefox 49, or Safari 10. Chrome focus is behaving--tab moves the cursor, spacebar checks/unchecks, but no blue focus. (Safari and Firefox don't respond properly to tab or spacebar. Maybe it's the demo frame?) – Thad Nov 04 '16 at 22:06
0

Main thing I did was set backgrounds background-size to cover.

.checkbox {
 display: inline-block;
 cursor: pointer;
 font-size: 13px; line-height:18px;
}

input[type=checkbox] {
 display:none;   
}

.checkbox:before {
 content: "";
 display: inline-block;
 width: 40px;
 height: 40px;
 vertical-align: middle;
 background-color: rgb(248,183,51);
 color: #f3f3f3;
 text-align: center;
}

input[type=checkbox]:checked + .checkbox:before {
 background: url("https://cdn1.iconfinder.com/data/icons/windows8_icons_iconpharm/26/checked_checkbox.png");
 text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
 font-size: 15px;
 background-size: contain;

}
 <div class="left">
  <input id="Option" type="checkbox">
  <label class="checkbox" for="Option"> </label>
 </div>
Paran0a
  • 3,359
  • 3
  • 23
  • 48
  • Actually background is not working for me when i add the image path inside the background it doesn't show the image. please tell me what is my error. – Neha Purwar May 19 '16 at 07:11
  • I am adding my image path in background property like this background : url('../images/Tick.png'); Is it a wrong way ? if it is right, is there any other way to adding the custom image in checkbox? Please let me know.I am struggling for this for an hour.@Paran0a – Neha Purwar May 19 '16 at 09:09
  • Yes definitely, and it's working when i am giving the path url in content tag but here i am unable to resize it. – Neha Purwar May 23 '16 at 04:01
  • background: url("../images/Tick.png"); do it like this. – Paran0a May 23 '16 at 05:49