How I can change element order and positions within a div using media queries? The following image shows the desired behaviour(use left image when browser window is smaller than 1000px wide, the second when bigger.):
My first attempt using 'normal' placement on first case and use float on the second:
.box2 {
float: right;
}
but then the element 2 (green) aligns on extreme right of container. Not close to the first element.
fiddle: http://jsfiddle.net/vmkRM/1/
Although it is not really well enough supported by FF and IE, the flex-box model is the right way to do it (conceptually at least if not for practical purposes). Check out this with chrome:
http://jsfiddle.net/38cNE/4/
The key parts are:
try this
http://jsfiddle.net/vmkRM/3/
Assuming you have standard HTML that looks like this:
And CSS like this:
I'd achieve the narrow version by adding this CSS:
And I'd achieve the wide version by adding this CSS instead:
Note that I'm cheating a bit by manually calculating the extra top-margin in order to vertically align the boxes.
Putting it all together with media queries to do it automatically would look like this:
A working fiddle is here: http://jsfiddle.net/UwtZW/
(Note that I've used narrower widths to make it work nicely in the fiddle - but it should be easy to adapt to the actual widths you need)
If anybody knows how to achieve the vertical alignment automatically without knowing the heights, I'd be very interested to learn. When I try, I can't get past the float / vertical-alignment conflict.
use width 100% for parent DIV, just look this fiddle