Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change checkbox check image to custom image

Tags:

html

css

xhtml

I am trying to change the default 'box image' of the checkbox with CSS, but it is not working. Is there any way around this?

.class_checkbox{     background: url("../images/button_bullet_normal.png") no-repeat scroll 0 0 transparent; } 
<input type="checkbox" class="class_checkbox"> 
like image 563
themhz Avatar asked Apr 22 '12 18:04

themhz


People also ask

How do I put an image in a checkbox in HTML?

The solution is to use the :checked css selector. What we want to achieve is when you click the label (which will be an image or an icon) the input field will change to "checked" and the label image will change to something different.

How do I add a tick to a custom checkbox?

Also, you can use content: '✔'; in your css to provide a tick on click. Show activity on this post. You will need to use :checked pseudo class for it. In the following example it's using a span tag to create the custom checkbox style, and using a pseudo element :before with a special character ✔ inside for the tick.


2 Answers

You can use pure css, just add a label to the checkbox like this:

.check_box {     display:none; }  .check_box + label{     background:url('images/check-box.png') no-repeat;     height: 16px;     width: 16px;     display:inline-block;     padding: 0 0 0 0px; }  .check_box:checked + label{     background:url('images/check-box-checked.png') no-repeat;     height: 16px;     width: 16px;     display:inline-block;     padding: 0 0 0 0px; } 

Example HTML:

    .check_box {      	display:none;      }        .check_box + label{      	background:url('images/check-box.png') no-repeat;      	height: 16px;      	width: 16px;      	display:inline-block;          padding: 0 0 0 0px;      }        .check_box:checked + label{          background:url('images/check-box-checked.png') no-repeat;      	height: 16px;      	width: 16px;      	display:inline-block;          padding: 0 0 0 0px;      }
<input type="checkbox" class="check_box" id="checkbox1">  <label for="checkbox1">
like image 163
ali mokrani Avatar answered Oct 14 '22 22:10

ali mokrani


Try:

<input type="checkbox" class="input_class_checkbox"> 

jQuery

$('.input_class_checkbox').each(function(){     $(this).hide().after('<div class="class_checkbox" />');  });  $('.class_checkbox').on('click',function(){     $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked')) }); 

Fiddle: http://jsfiddle.net/cn6kn/

$('.input_class_checkbox').each(function(){      $(this).hide().after('<div class="class_checkbox" />');    });    $('.class_checkbox').on('click',function(){      $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'))  });
.class_checkbox {      width: 20px;        height: 20px;      background-color: red;  }  .class_checkbox.checked {      background-color: green;  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  <input type="checkbox" class="input_class_checkbox">
like image 27
iambriansreed Avatar answered Oct 14 '22 21:10

iambriansreed