I have two quite narrow (width:400 px) divs that should be stacked vertically if there is sufficient vertical space in the browser window. If the browser height is too small and there is space enough to the right, the “bottom div” should float to the right of the top div.
In a sense I’m asking for the opposite to “float:left”. float:left align divs horizontally if there is sufficient horizontal space in the browser window and only float divs below the others if there only is available space below them.
Thanks for any suggestions!
Depending on the level of browser support you require, a simple media query may solve your problem:
Here’s the effect I see with that:


The CSS3 media queries let you set any rules css you like based on the viewport height. The w3c has lots of information about media queries.
A relevant snippet regarding viewport height:
Sometimes I find the Mozilla Developer Network a bit more accessible, but in this case they provide basically the same information.
Do you need to support old browsers who can’t render media queries? It should be fairly simple to write a javascript polyfill can have the same effect, especially using a library like jQuery.
Edit
I revised my code example to more closely match your problem. You commented:
And I also set the widths to 400px as in your example.
Now it only floats left if the screen is too short for them to be vertical and there is sufficient room on the right to fit both. Otherwise it’s always vertical.