On click hide this (link button) pure css

my function is to hide and show the div with pure css , but when I click the "Open" button, the button still does not disappear.

<a href="#show" id="open" class="btn btn-default btn-sm">Open</a>
<div id="show">
  some text...
  <a href="#hide" id="close" class="btn btn-default btn-sm">Close</a>
</div>

and css look like this:

<style>
    #show {display: none; }
    #show:target { display: inline-block; }
    #hide:target ~ #show { display: none; }
<style>

when i add this:

#show:target ~ #open { display: none; }

the button #openis still not hiding anyone can help me.

thanks before :)

+1
source share
3 answers

You can solve this problem by placing your Open link inside a #showdiv

jsFiddle

HTML

<div id="show">
    <a href="#show" id="open" class="btn btn-default btn-sm">Open</a>
    <div id="content">
        some text...
        <a href="#hide" id="close" class="btn btn-default btn-sm">Close</a>
    </div>
</div>

CSS

#content {
    display: none;
}
#show:target #content {
    display: inline-block;
}
#show:target #open {
    display: none;
}
+6
source

The click function can be implemented using Checkboxpure css. I modified your HTML as follows:

HTML

<input id="checkbox" type="checkbox" class="checkbox" />
<label id="open" for="checkbox" class="btn btn-default btn-sm"> <span class="show-text"></span>

</label>
<div id="show">some text...
    <label for="checkbox" class="second-label btn btn-default btn-sm">Close</label>
</div>

CSS

:checked ~ .btn-default, #show, .checkbox {
    display: none;
}
:checked ~ #show {
    display: block;
}
.show-text:after {
    content:"Open";
}
:checked + .show-text:after {
    content:"";
}
.second-label, .show-text {
    text-decoration: underline;
    cursor: pointer;
}

Working script

+3
source

Mr_Green Thank you for this code. I changed it for a flexible expanding menu on mobile devices.

HTML

<input id="menu-toggle" type="checkbox" class="checkbox-toggle" />
<label id="open" for="menu-toggle" class="btn btn-default">Menu</label>
<div id="show">
   Some Content
</div>

CSS

@media (max-width: 650px) {

input.checkbox-toggle + label {
    display: block;
    padding:.7em 0;
    width:100%;
    background:#bbbbbb;
    cursor: pointer;
    text-align:center;
    color:white;
    Text-transform:uppercase;
    font-family:helvetica, san serif;
    }

input.checkbox-toggle:checked + label {
    background:#6a6a6a;
    }

#show {
    display: none;
    }

input.checkbox-toggle:checked ~ #show {
    display: block;
    }

}

input.checkbox-toggle {
    display: none;
    }
+1
source

Source: https://habr.com/ru/post/1689321/


All Articles