Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapse without javascript

Tags:

html

css

I know how to collapse (display / hide) a div:

$('#nav').click(function() { $('#hello').toggleClass('hidden'); });
.hidden { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="nav">NAV</div>
<div id="hello" class="hidden">Hello</div>

Is it possible to do this without Javascript / jQuery?

I've tried the main answer from this question, but it is not working, as detailed here.

like image 213
Basj Avatar asked Dec 19 '16 10:12

Basj


2 Answers

You may use :checked selector.

#hidden {
  display: none;
  height: 100px;
  background: red;
}
:checked + #hidden {
  display: block;
}
<input type="checkbox" id="my_checkbox" style="display:none;">
<div id="hidden"></div>

<label for="my_checkbox">Show/hide</label>

Example fiddle

like image 183
Denis Sheremet Avatar answered Nov 02 '22 13:11

Denis Sheremet


Nobody has mentioned the 'details' element, which seems perfect for this job.

<details>
    <summary>Click to toggle</summary>
    <span>Oh, hello</span>
</details>
like image 30
Matthew Spencer Avatar answered Nov 02 '22 12:11

Matthew Spencer