views:

551

answers:

4

I have a short intro text inside a box, if the user wants to read futrher he can toggle a hidden div element that shows the rest of the content. Under this intro box I have some other element, which I always want to stay in that position. I want the toggled text to 'show above' the fixed content, and I don't want it to push downwards when the toggled div is opened. I've experimented with various z-index values, absolute and relative positioning, to no avail. Is there a clean CSS based solution to this? Please help! Here's a demo of what I'm trying to do:

> <!DOCTYPE html PUBLIC "-//W3C//DTD
> XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
> <html
> xmlns="http://www.w3.org/1999/xhtml"
> xml:lang="en"> <head>     <style
> type="text/css">   .container{width:
> 400px;
>       height: 500px;
>       border: 1px dashed #999; 
>       }   div.container{padding:0; margin:0}      #morecontent{
>      z-index: 100 
>      }   #morecontent,.introcontent{background: #DFFAFF;}     div#fixedcontent{background:
> #FFDFDF;
>        z-index: -1;
>        position: absolute; 
>        width: 400px
>        }    </style> <title>Toggle overlap - test</title> </head> <body>
>   <div class="container">   <div id="">
>    <script type="text/javascript">
>          function toggle(obj){
>          var el=document.getElementById('morecontent');
>           if (el.style.display !='none'){
>             el.style.display='none';
>            }
>            else {el.style.display='';
>              }
>          }
>           </script>
>      <p class="introcontent">Lorem ipsum dolor sit amet, consectetur
> adipiscing elit. Aenean in pede congue
> ipsum sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.<br /> 
>      There's more if you press toggle...
>      
>             </p>
>        <a href="JavaScript: toggle(this)">Toggle</a>     
>       <div id="morecontent" style="display:none;">
>        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in
> pede congue ipsum sollicitudin
> pellentesque. Nunc t  tortor dolor,
> sagittis nec, placerat vel, commodo
> sed, nunc. Vivamus bibendum molestie
> orci. Duis nec leo at libero fermentum
> molestie. Nam eu risus.
>             </p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Aenean in pede congue ipsum
> sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.
>             </p>
>        </div>
>        
>        <div id="fixedcontent">
>        <p>This should stay 'under' the toggled content!</p>
>         <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in
> pede congue ipsum sollicitudin
> pellentesque. Nunc t  tortor dolor,
> sagittis nec, placerat vel, commodo
> sed, nunc. Vivamus bibendum molestie
> orci. Duis nec leo at libero fermentum
> molestie. Nam eu risus.
>             </p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Aenean in pede congue ipsum
> sollicitudin pellentesque. Nunc
> t tortor dolor, sagittis nec, placerat
> vel, commodo sed, nunc. Vivamus
> bibendum molestie orci. Duis nec leo
> at libero fermentum molestie. Nam eu
> risus.
>             </p>
>         </div>     </div>    </div>   </body> </html>
+1  A: 

Something I have done in the past:

Separate your content into two spans. One span is displayed and the other hidden. When the user want to see the rest of the content, show the other span with the rest of the text.

I use the JQuery toggle() command to make it easier to do.

Chris Brandsma
A: 

I guess you are testing with IE only. Firefox does not have this problem.

This is a known bug in IE, the z-index bug

http://stackoverflow.com/questions/687849/is-z-index-the-only-way-to-force-an-element-to-be-positioned-over-top-of-another/687861#687861

There is a "general" solution using javascript http://mahzeh.org/?p=23

But the best way is to rearrange elements so your expandable element gets on a position over the box below. Try to insert it after the box you want to cover in your HTML and then position above it visually using absolute positioning.

Miquel
No on the contrary - I've only tested with FireFox! But the second part of your answer solved the problem. I've just had to put the fixed content before the toggling content in the markup, position it absolute, and it workd! Thank you very much. :)PS.: I'm new to the site so maybe my question wasn't coined best.
Tamas Kalman
You should mark my answer as a solution then ;)
Miquel
A: 

Try:

#container { position: relative; }
#more { position: absolute; display: none; background-color: White;}

<div id="container">
    Lorum <a href="#">click for more</a>
    <div id="more">
       Lorum
    </div>
    <div id="under">
        Underneath text
    </div>
</div>

On event 'click for more' set #more to display: block;

The z-index is implied in this case as #more is before #under in the document tree, and hence has a higher z-index

EoghanM
+1  A: 

I'm not seeing how moving the z-index around is better/more efficient than hiding and showing the content by altering the display attribute on the divs. Is there some reason you want all the content on the page at all times?

b. e. hollenbeck