Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Hide div with css only

I'm trying to do a show / hide using css only, is that possible or does some type of jscript is needed? this is what i'm trying to do, when anyone one of the 4 div's are clicked the div for it below is shown.

    <div class="span3">
        <img src="an1.jpg" class="img-rounded" />
        <h3>AN1<br />1234</h3>
    </div>

    <div class="span3">
        <img src="an2.jpg" class="img-rounded" />
        <h3>AN2<br />1234</h3>
    </div>

    <div class="span3">
        <img src="an3.jpg" class="img-rounded" />
        <h3>AN3<br />1234</h3>
    </div>

    <div class="span3">
        <img src="an4.jpg" class="img-rounded" />
        <h3>AN4<br />1234</h3>
    </div>

Show div when div is click:

<div style="display: none;"> this is AN1 </div>
<div style="display: none;"> this is AN2 </div>
<div style="display: none;"> this is AN3 </div>
<div style="display: none;"> this is AN4 </div>
like image 917
acctman Avatar asked Jun 11 '13 10:06

acctman


People also ask

How do I completely hide a div in CSS?

You can hide an element in CSS using the CSS properties display: none or visibility: hidden . display: none removes the entire element from the page and mat affect the layout of the page. visibility: hidden hides the element while keeping the space the same.

How do you hide visibility in a div?

visibility: hidden is used for the first div in the test block (right). As a result, the content is hidden but the space for it is still available. This is because visibility: hidden hides the element but still leaves it on the page flow. Next, we apply display: none to <div 3> .

How do I hide a div but keep the space?

you can wrap another div around the outside of it, and probably tell it a specific height to occupy. that way your inner div can show and hide and fadeOut, etc, and the outer div will hold down the real-estate on the page.


1 Answers

You can use a hidden input that can be toggled that corresponds to the label in the click area.

<div class="span3">
<label for="an1">
    <img src="an1.jpg" class="img-rounded" />
</label>
<h3><label for="an1">AN1<br />1234</label></h3>
</div>
...
<input id="an1" type=checkbox><div style="display: none;"> this is AN1 </div>

Then in your CSS:

[type=checkbox] {
    display: none;
}
:checked + div {
    display: block !important;
}

I would also stear clear of style and just use the stylesheet.

http://jsfiddle.net/ExplosionPIlls/ZyAXA/1/

like image 114
Explosion Pills Avatar answered Oct 01 '22 23:10

Explosion Pills