Search code examples
htmlcsscheckboxchecked

I want to make "onclick" effect on mobile with checkbox:checked (without using JS)


I want my dropup content to appear when I check my checkbox, but it isn't working. Dropup-content by default has visibility: collapse, so I want it to have visibility: visible when I check the checkbox. how it should look

I tried to put .dropup-content under input:checked + span but nothing change

index.html:

<label for="sports">  
  <input type="checkbox" id="sports" class="checkbox">  
  <span class="span-label">Sports</span>  
</label>  
<div class="dropup-content">  
  <a href="">More...</a>  
  <a href="">Esports</a>  
  <a href="">Handball</a>  
  <a href="">Basketball</a>  
  <a href="">Football</a>  
</div>

index.scss:

input:checked + span {
    color: orange;  //changes color of text Sports
   .dropup-content {
       visibility: visible;
       max-height: 250px; 
       transition: max-height 0.2s; 
  } 
}

Solution

  • I ended up using inputs rather than checkbox. But I wanted to post a solution. So, it had to do with subtree, but also in css you have to use ~ to refer to sibling element:

    This solved my problem:

    input:checked + label {  
      color: orange;
    } 
    input:checked ~ .dropup-content {
      visibility: visible;
      max-height: 250px;
      transition: max-height 0.2s;
    }