Forum Moderators: not2easy

Message Too Old, No Replies

I want one div to slide under another as window width reduces.

         

itKiwi

1:27 pm on Apr 21, 2007 (gmt 0)

10+ Year Member



I have a two column layout. Left hand column is the main content; right hand column is narrower, and consists of links, similar to a menu. This uses “float: right” div. When I make the window narrower, I want the right hand column to slide under the left hand. The left hand column should stay at it's original width.
Thanks for any ideas.

Xapti

5:29 am on Apr 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As far as I know... such a thing would not be doable using CSS...

How narrow does the window need to be sized? And I guess the only reason you want it jumping is to save space at lower sizes?
Do you want the list of links (or whatever that content is) to switch to horizontal spread instead of vertical after the jump? (I assume that'd be ideal)

A solution can be found using Javascript, though. on window size = whatever, remove float:right (assuming you put that div under the left div's code), and change it's width. Assuming the links in the list were also floated (and stacked), they would then spread out horizontally.

[edited by: Xapti at 5:38 am (utc) on April 22, 2007]

itKiwi

9:06 pm on Apr 22, 2007 (gmt 0)

10+ Year Member



Thanks Xapti. Perhaps I need to explain better.
The end result of this "exercise" will be:-
The left hand (main content) div will be a form. Normally, the right hand div will (I hope) be hidden under (z-index?) the left hand div. A "help" button will (using javascript) increase the width of the window, and so make the right hand list of links visible. The right hand list of links will lead to various help subjects.
Playing with my test window manually, all I have managed so far is that the left hand div either changes it's width, or it pops vertically under the right hand div, as the window width is reduced.
Thanks again.

SuzyUK

10:37 am on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it should be possible, though you would likely have to position the right hand div with absolute positioning

the wrapper (which would be the one you would then need to widen?) would be set to position: relative in order to become the containing block for right div, but then also the left div should be set to position: relative and given a z-index - this should ensure it always stays "on top" of the right div as the window narrows

something like:


CSS:
#wrap {
position: relative;
width: 80%;
margin: 0 auto;
border: 1px solid #f00;
}

#right {
position: absolute;
width: 200px;
background: #ff0;
right: 10px;
top: 15px;
}

#left {
background: #eee;
width: 550px;
position: relative;
z-index: 1;
}

HTML:
<div id="wrap">

<div id="left">
<p>left content.....</p>
<p>left content.....</p>
<p>left content.....</p>
</div>

<div id="right">
<p>right content to slide under left</p>
<p>right content to slide under left</p>
</div>

</div>

not sure if that's going to fit with what you want to do, let us know

Suzy

itKiwi

8:42 pm on Apr 23, 2007 (gmt 0)

10+ Year Member



Thanks Suzy.
The only changes I had to make to your suggestions were to give the right div a z-index of 1, and change the left z-index to 2.
This gives me the principles of operation; now to integrate it into my working pages ;-)
Just to help me understand. Giving the right div an absolute position, AND the small right margin (without a left margin), glues the right div to the right hand side of the container. Correct? Why doesn't "float: right;" (which is what I'd been trying), work like this?
Thanks again; Ciao

SuzyUK

9:18 am on Apr 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why doesn't "float: right;" (which is what I'd been trying), work like this?

Well truth be told, you can do it with a mixture of floats and position relative to control the z-index too, but guess which browser doesn't play along ;) - see below for code sample

anyway simple explanation is because when you float 'A' right then it still affects 'B' - 'B' will reduce it's text line size but not its actual width to accommodate the float so as you narrow the window 'B' is just going to keep narrowing until it can't anymore (line size or specified width) then it drops below

- whereas when you Absolutely Position 'A' (glue it to the right) it's not affecting the left 'B' at all, 'B' doesn't even know it's there ;) - 'A' is removed from the document flow and would actually sit on top of the left side unless you control the stacking contexts, which is what adding a higher z-index to 'B' is doing.

z-index (stacking levels) can only be applied to elements which have position: relative or position: absolute.

btw, you don't need to use the values for top and right that I've used you can can set them to 0 or position to suit, these aren't really margins as explained above they're not affecting the distance from the left column at all - they measure from the top/right of the #wrap div (containing block)

and lastly just for info here's the CSS that might do it for the float, still using my HTML sample above, though in this case the right div must appear before the left in the source (whereas with the AP method it can be in any order)

CSS:
#wrap {
position: relative;
width: 80%;
margin: 0 auto;
border: 1px solid #f00;
min-width: 550px;
}

#right {
float: right;
width: 200px;
background: #ff0;
position: relative;
z-index: 1;

*margin-left: -550px;
*left: 350px;

}

#left {
background: #eee;
width: 550px;
position: relative;
z-index: 2;
}

p {margin: 1em 0;}

the bits in red are for IE only, it's is hack but is done like this for demo only.. it depends on your application but I'd say the AP method is more stable and more reliably positioned

Suzy