HTML Form How to - Hide/show element with checkbox in CSS








Question

We would like to know how to hide/show element with checkbox in CSS.

Answer


<!--from  ww  w . j  a va2  s .  c o m-->
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.spoiler {
  display: none;
}

.trigger:checked+.spoiler {
  display: block;
}
</style>
</head>
<body>
  <input type="checkbox" class="trigger" />Spoiler
  <div class="spoiler">Lorem</div>
</body>
</html>

The code above is rendered as follows: