Faytraneozter
Faytraneozter

Reputation: 875

on click hide this (button link) pure css

my function is hide and show div with pure css but when i click open, the button still 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 the css look like:

<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 #open still not hiding anyone can help me.

thanks before :)

Upvotes: 2

Views: 17870

Answers (3)

Isaac Bailey
Isaac Bailey

Reputation: 11

Mr_Green Thank you for that code. I modified it for a responsive 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;
    }

Upvotes: 1

Mr_Green
Mr_Green

Reputation: 41852

The click functionality can be implemented using Checkbox for pure 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 Fiddle

Upvotes: 3

Mathias
Mathias

Reputation: 5670

You could solve it by putting your Open link inside the #show div

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;
}

Upvotes: 8

Related Questions