If Javascript is not an option for you, your choices become quite limited. You will need to come up with a way to toggle the state through a click and represent those states using CSS.
Here are some possible options:
- Utilizing (hidden?) radio buttons or checkboxes and leveraging their
:checked
pseudo-class in CSS
- Using anchors and their pseudo-classes (similar to what you have already tried)
However, one issue with these approaches is that all your dynamic content (the elements you show/hide on click) must be placed inside or alongside these toggling elements, which can be inconvenient.
Personally, I prefer using the :target
pseudo-class. When you visit a URL like "https://something.com#foobar", the element with the id "foobar" becomes the current target (if it exists). The drawback here is that only one target is supported per document. You can manipulate the target by clicking on anchors like this:
.dynamic-content {
display: none;
}
#first:target, #second:target {
display: block;
}
<div>
<div id="first" class="dynamic-content">
First dynamic content
<a href="#">Hide</a>
</div>
<div id="second" class="dynamic-content">
Second dynamic content
<a href="#">Hide</a>
</div>
</div>
<a href="#first">Show first</a>
<a href="#second">Show second</a>