Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looking for show/hide example

Status
Not open for further replies.

NickMalloy

Programmer
Apr 15, 2005
68
US
I am looking for a good example of showing and hiding content within a <div> using css?
 
The only way you can do it cross browser and purely in CSS is by using anchor tags and :hover or :active pseudo classes. However, that is not the best way to do it, because it will seem confusing to people. I suggest you employ a little bit of javascript that switches display from block to none and vice versa on the respective divs. Or use visibitility hidden and visible if you want to still show the space occupied by the hidden element to be reserved.
 
Something like this (which is the same as one of Vragabonds suggestions):
Code:
<style type="text/css">
#uniqueDiv1 {
  display:none; /* use display:block; to show by default */
}
</style>
...
<body>
<a href="javascript:document.getElementById('uniqueDiv1').style.display = (document.getElementById('uniqueDiv1').style.display == 'block') ? 'none' : 'block';">Show/Hide Div</a>
<div id="uniqueDiv1">uniqueDiv1 content</div>
</body>
Mixes a little javascript in the a href.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Even tho divs are display style block inherently, you still have to explicitly define it for the javascript conditional to evaluate true the first time the button is clicked.
Code:
<script language="javascript">
function switchDisplay(obj) {
   obj.style.display = (obj.style.display == "block") ? "none" : "block"
}
</script>
<input type="button" value="show/hide" onclick="switchDisplay(document.getElementById('mydiv'))">
<div id="mydiv" style="border:1px solid #000000; display:block">This is the div you want to hide</div>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
as usual

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top