I'm looking for a pure css way to make the checkbox hide/show the contents of the <div class="chapter">
block:
<div>
<input id="_1" type="checkbox">
<h2>
<label for="_1">Collapse 1</label>
</h2>
</div>
<div class="chapter">
Content 1
</div>
<div>
<input id="_2" type="checkbox">
<h2>
<label for="_2">Collapse 2</label>
</h2>
</div>
<div class="chapter">
Content 2
</div>
My current attempt (tl;dr: scroll to the bottom):
/* Niceties */
*:focus{
outline: none;
}
label {
cursor : pointer;
display: block;
}
/* Checkboxes are hidden, and replaced with a right/down pointing
arrow, depending on selection state. Note that I want this
arrow to be placed *before* the h2 */
div > input {
display : inline-block;
-webkit-appearance: none;
-o-appearance : none;
-moz-appearance : none;
}
div > input:before {
content: "\25b6 "; /* right-pointing arrow */
display: inline-block;
}
div > input:checked:before {
content: "\25bc "; /* down-pointing arrow */
display: inline-block;
}
/* And here's the problem: how to make the checkbox affect the div
after the checkbox' parent div? */
/* (show) */
input + h2 + div.chapter { /* this obviously does not work */
display: none;
}
/* (hide) */
(div + input:checked) + div.chapter {
/* this selector is pseudo-code of what I really need, but
how to accomplish this kind of grouping? */
display: block;
}
The accepted answer to this question changes the structure of the HTML by placing the element-to-be-affected into the <input>
's surrounding <div>
. I would like my <div>
structure to stay intact, because I need it to style the <h2>
.
Note that I explicitly didn't use the jquery or javascript tags, I'm wondering if there's a pure CSS solution here that I'm not seeing (or even aware of).
`.