Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CSS change a class display by checking some checkbox

I’m trying changing a CSS class property by checking a checkbox, but it doesn’t work… Can you help me?

My code:

<html lang="pt-br">
<body>
  <style>
    #test:checked~#title {
      display: none;
    }
  </style>
  <h1 id="title">Just a text</h1>

  <input type="checkbox" id="test" />
</body>
</html>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to use input tag first then you can use another html tag to make checked pseudo class working and you can use flexbox to order the html tags.

.form_group{
  display: flex;
  flex-direction: column;
} 
#test{
  order: 2;
}
#title{
  order: 1;
}
#test:checked~#title {
      display: none;
}
<div class="form_group">   
  <input type="checkbox" id="test" />
<h1 id="title">Just a text</h1> 
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading