2

I have my checkboxes and I am trying to style it with the following CSS.

input[type="checkbox"]{
    display: none;
    border: none !important;
    box-shadow: none !important;
}

input[type="checkbox"] + label span {
    display: inline-block;
    vertical-align: middle;
    width: 30px;
    height: 30px;
    background: url(/static/app/images/check_no.svg);
}

input[type="checkbox"]:checked + label span {
    background: url(/static/app/images/check_yes.svg);
    content: '';
    color: #fff;
    vertical-align: middle;
    width: 30px;
    height: 30px;
}
<div class="check1">
  <input id="id_contract_name" name="contract_name" type="checkbox"> 
    <label for=" id_contract_name "> 
      <span class="chk_contract"></span>   Name on Contract
    </label>
</div>


<div class="check2">
  <input id="id_is_ceo" name="is_ceo" type="checkbox"> 
  <label for=" id_is_ceo "> 
    <span></span>  CEO?
  </label>
</div>

The checkboxes are inside my form.

This does not work when I click my checkbox. I am new to styling and let me know where the error is.

2 Answers 2

1

You have spaces around the name in the for attribute, remove them.

Working demo:

 input[type="checkbox"]{
display: none;
border: none !important;
box-shadow: none !important;
   }

      input[type="checkbox"] + label span {
display: inline-block;
vertical-align: middle;
width: 30px;
height: 30px;
background: url(/static/app/images/check_no.svg);

      }

     input[type="checkbox"]:checked + label span {
background: url(/static/app/images/check_yes.svg);
content: '';
color: #fff;
vertical-align: middle;
width: 30px;
height: 30px;
       /*added this to show the behavior here on SO*/
       background-color: #000
 }
     <div class="check1">
          <input id="id_contract_name" name="contract_name" type="checkbox"> 
          <label for="id_contract_name"> 
              <span class="chk_contract"></span>   Name on Contract
          </label>
      </div>


     <div class="check2">
          <input id="id_is_ceo" name="is_ceo" type="checkbox"> 
          <label for="id_is_ceo"> 
              <span></span>  CEO?
          </label>
      </div>

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

Comments

0

You have to mention id or class in CSS. And if you want to add css dynamically then you have to use javascript or jquery.

 #id_contract_name{
display: none;
border: none !important;
box-shadow: none !important;
   }

      #id_is_ceo {
display: inline-block;
vertical-align: middle;
width: 30px;
height: 30px;
background: url(/static/app/images/check_no.svg);

      }

     input[type="checkbox"]:checked + label span {
background: url(/static/app/images/check_yes.svg);
content: '';
color: #fff;
vertical-align: middle;
width: 30px;
height: 30px;
 }
<html>
<body>
     <div class="check1">
          <input id="id_contract_name" name="contract_name" type="checkbox"> 
          <label for=" id_contract_name "> 
              <span class="chk_contract"></span>   Name on Contract
          </label>
      </div>


     <div class="check2">
          <input id="id_is_ceo" name="is_ceo" type="checkbox"> 
          <label for=" id_is_ceo "> 
              <span></span>  CEO?
          </label>
      </div>
  </body>
 </html>

1 Comment

This is not what the OP wants. The OP wants to hide the checkbox and display custom graphics instead of it, based on checked property.

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.