0

I tried to follow this which is fine for background image replacement and fixed dimensions. But how about having an inline image as a image replacement where the image has a dynamic height?

<input type='checkbox' name='thing' value='valuable' id="thing"/><label for="thing"></label> 

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

input[type=checkbox] + label {
  background: #999;
  height: 16px;
  width: 16px;
  display:inline-block;
  padding: 0 0 0 0px;
}
input[type=checkbox]:checked + label {
  background: #0080FF;
  height: 16px;
  width: 16px;
  display:inline-block;
  padding: 0 0 0 0px;
}

Looking for a replacement for

<label for="thing">
 <input type='checkbox' name='thing' value='valuable' id="thing"/>
 <img src="img.jpg" height="auto">
</label> 
Community
  • 1
  • 1
rob.m
  • 9,843
  • 19
  • 73
  • 162

3 Answers3

2

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

input[type=checkbox] + label {
  border: 10px solid transparent;
  display: inline-block;
  font-size: 0;
  transition-duration: 0.4s;
}

input[type=checkbox]:checked + label {
  border-color: green;
}
<input type='checkbox' name='thing' value='valuable' id="thing" />
<label for="thing"><img src="//lorempixel.com/200/100" alt="checkbox" /></label>
connexo
  • 53,704
  • 14
  • 91
  • 128
0

If I have understood your question correctly, I think you can achieve this straightforwardly:

$(document).ready(function(){
    $('input').each(function(){
        $(this).click(function(){
          alert('You clicked Checkbox ' + $(this).attr('name'));
        });
    });
});
input[type=checkbox] {
  display:none;
}

input[type=checkbox] + label {
  background: #999;
  height: 16px;
  width: 16px;
  display:inline-block;
  padding: 0;
}

input[type=checkbox]:checked + label {
  background: #0080FF;
  height: 16px;
  width: 16px;
  display:inline-block;
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label for="thing1">
<input type="checkbox" name="thing1" id="thing1" value="valuable" />
<img src="http://placehold.it/16x16" />
</label>

<label for="thing2">
<input type="checkbox" name="thing2" id="thing2" value="valuable" />
<img src="http://placehold.it/24x24" />
</label> 

<label for="thing3">
<input type="checkbox" name="thing3" id="thing3" value="valuable" />
<img src="http://placehold.it/32x32" />
</label>
Rounin
  • 27,134
  • 9
  • 83
  • 108
0

This trick depends on:

  1. The for attribute so that a <label> can be an extension of the <[=checkbox]> so that any event happening to the label, happens to the <[=checkbox]> as well.
  2. The adjacent sibling selector + which selects the sibling node immediately next. In this case: <[=checkbox]> + <label>
  3. The :checked pseudo-class which enables the <[=checkbox]> to be in and/or influence 2 distinct states of styles.

Note in the Snippet, the <img> can be of any size.

SNIPPET

.chx {
  display: none;
}
label {
  border: .5px inset #fefefe;
  border-radius: 100%;
  background: rgba(0, 0, 0, .1);
}
.chx:checked + label img {
  opacity: 1;
  transition: opacity 1.2s ease-in;
}
.chx + label img {
  opacity: 0;
  transition: opacity .9 ease-out;
}
<input id='chx1' class='chx' type='checkbox' checked>
<label for='chx1'>
  <img src='http://www.seaicons.com/wp-content/uploads/2016/11/Button-Blank-Green-icon.png' width='50'>
</label>
<input id='chx2' class='chx' type='checkbox' checked>
<label for='chx2'>
  <img src='http://www.freeiconspng.com/uploads/delete-button-png-24.png' width='128'>
</label>
<input id='chx3' class='chx' type='checkbox' checked>
<label for='chx3'>
  <img src='http://images.all-free-download.com/images/graphiclarge/round_yellow_play_button_4032.jpg' width='256'>
</label>
Graham
  • 7,431
  • 18
  • 59
  • 84
zer00ne
  • 41,936
  • 6
  • 41
  • 68