Hide/show a div

I have

						<div class="row">
							<div class="col">
							  <a id="hide" href="#hide" class="float-end"><span class="icon-chevron-down"></span> Show more</a>
							  <a id="show" href="#/" class="float-end"><span class="icon-chevron-up"></span> Show less</a>
							</div>								
						</div>
					    <div id="answer" class="row">

...
</div>

and heres the CSS

#answer,
#show,
#hide:target {
    display: none; 
}

#hide:target + #show,
#hide:target ~ #answer {
    display: inherit; 
}

what am I missing?

Why not try <details>? Minimal CSS.

You can do a lot using <details>

5 Likes

#hide has no sibling called #answer. (but yes, details is easier.)

2 Likes

In your example the answer would need to be a sibling of the target with html like this.

<div class="row">
  <div class="col">
    <a id="hide" href="#hide" class="float-end"><span class="icon-chevron-down"></span> Show more</a>
    <a id="show" href="#show" class="float-end"><span class="icon-chevron-up"></span> Show less</a>
    <div id="answer" class="row">The answer</div>
  </div>
</div>

In your structure you could use :has but support is not good at the moment.

#answer,
#show,
#hide:target {
  display: none;
}

#hide:target + #show,
#hide:target ~ #answer {
  display: inherit;
}

.row:has(#hide:target) + #answer {
  display: inherit;
}
<div class="row">
  <div class="col">
    <a id="hide" href="#hide" class="float-end"><span class="icon-chevron-down"></span> Show more</a>
    <a id="show" href="#show" class="float-end"><span class="icon-chevron-up"></span> Show less</a>
  </div>
</div>
<div id="answer" class="row">The answer</div>
1 Like

thank, I went with using details…

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.