I would like to do this without JavaScript if possible:
I will have 2 or more elements on a page that I want to change the order of when the page is a certain size. I am using media-queries and will serve different css based on screen size. Div "a" should be at the top normally, but if the screen size is under 480px then b should be directly over a.
<div id="a" class="myDivs">Top when on desktop</div>
<div id="b" class="myDivs">Becomes top when under 480px wide screen</div>
The CSS that I must keep in my mobile.css:
.myDivs {
width: 100%;
}
So the height is not specified; therefore, I can't simply position absolute and set margin-top or top.
With FlexBox - tested on iPad iOS7 this will reverse the two columns.
I'm using Modernizr so I can easily test for non-flexbox support by removing the 'flexbox' class from my HTML tag's classes.
Note: I am very new to Flexbox and these may not be all the correct prefixes needed now or into the future, but I've tested on my iPad and that's the main target for me for this and it works beautifully. See also CSS Flexible Box Layout on iPad
This one may do the trick but will maybe have drawbacks...
CSS:
HTML:
DEMO: http://jsfiddle.net/pavloschris/pYkfK/embedded/result/
Use table layout in CSS to rearrange the divs.
The elements will display in this order: Child 3, Child2, Child1
Fiddle is located here: http://jsfiddle.net/lockedown/gyef3vj7/1/
You're talking about source-order independence in terms of presentation. The bad news is that as of today, you're kind of out of luck without getting super kludgy (read: moving the DOM around with JS or something similar). The good news is that help is on the way in the form of CSS Flexbox - check out "ordering" on that page.
Other resources:
https://developer.mozilla.org/en-US/docs/CSS/Tutorials/Using_CSS_flexible_boxes
http://css-tricks.com/old-flexbox-and-new-flexbox/