2

I have this kind of checkbox on my site atleast 4 checkbox

<div id="product">
   <div class="checkbox">
      <label>
        <input type="checkbox">
        <img src="http://test.com/dev/image/cache/catalog/chains/chain_1-50x50.jpg" alt="Chain 1 +₱10.00" class="img-thumbnail"> 
        Chain 1 (+₱10.00)
      </label>
   </div>
</div>

How can I add css that when I select the checkbox, the image will add some border and the text will be bigger

I have this code but does not work

 #product :checked + label {
  font-weight: bold;
} 
 #product :checked + img{
  border:1px solid red
} 
4
  • you can move the checkbox outside <label> and use the for attribute to map the label Commented Feb 13, 2018 at 3:38
  • is there a way on css because those HTML are generated by opencart theme Commented Feb 13, 2018 at 3:40
  • 2
    The font-weight rule doesn't work, because the label is wrapping the checkbox, it's not a sibling Commented Feb 13, 2018 at 3:40
  • Oh I see Thanks! Commented Feb 13, 2018 at 3:42

2 Answers 2

3

The CSS rule:

#product :checked + label {
  font-weight: bold;
}

will match a <label> that is an immediate sibling of a :checked item. In your case, the label is the parent of the checkbox. To make the text bold, you could wrap it in a <span> and then use the ~ combinator to select any matching siblings:

HTML

<div id="product">
   <div class="checkbox">
      <label>
        <input type="checkbox">
        <img src="http://test.com/dev/image/cache/catalog/chains/chain_1-50x50.jpg" alt="Chain 1 +₱10.00" class="img-thumbnail"> 
        <span>Chain 1 (+₱10.00)</span>
      </label>
   </div>
</div>

CSS

#product :checked ~ span {
  font-weight: bold;
}

The following should work if the HTML is as you have it in the question:

 #product :checked + img{
    border:1px solid red
 } 

If not, you likely have another element between the checkbox and the image. In which case, you can use #product :checked ~ img as above.

Sign up to request clarification or add additional context in comments.

Comments

2

Your code will work for the image border, but the text bolding cannot be done in pure CSS without modifications to your HTML. If you cannot modify the HTML, you will need to use JavaScript like this:

document.querySelector("#product input").addEventListener("click", function() {
  var img = document.querySelector("#product img");
  var label = document.querySelector("#product label");

  if (this.checked) {
    img.style.border = "1px solid red";
    label.style.fontWeight = "bold";
  } else {
    img.style.border = "none";
    label.style.fontWeight = "normal";
  }
});
<div id="product">
  <div class="checkbox">
    <label>
        <input type="checkbox">
        <img src="http://via.placeholder.com/50x50" alt="Chain 1 +₱10.00" class="img-thumbnail"> 
        Chain 1 (+₱10.00)
        </label>
  </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.