Given a template where the HTML cannot be modified because of other requirements, how is it possible to display (rearrange) a div
above another div
when they are not in that order in the HTML? Both div
s contain data that varies in height and width.
<div id="wrapper">
<div id="firstDiv">
Content to be below in this situation
</div>
<div id="secondDiv">
Content to be above in this situation
</div>
</div>
Other elements
Hopefully it is obvious that the desired result is:
Content to be above in this situation
Content to be below in this situation
Other elements
When the dimensions are fixed it easy to position them where needed, but I need some ideas for when the content is variable. For the sake of this scenario, please just consider the width to be 100% on both.
I am specifically looking for a CSS only solution (and it will probably have to be met with other solutions if that doesn't pan out).
There are other elements following this. A good suggestion was mentioned given the limited scenario I demonstrated -- given that it might be the best answer, but I am looking to also make sure elements following this aren't impacted.
I was looking for a way to change the orders of the divs only for mobile version so then I can style it nicely. Thanks to nickf reply I got to make this piece of code which worked well for what I wanted, so i though of sharing it with you guys:
Just use flex for the parent div by specifying
display: flex
andflex-direction : column
. Then use order to determine which of the child div comes firstI have a much better code, made by me, it is so big, just to show both things... create a 4x4 table and vertical align more than just one cell.
It does not use any IE hack, no vertical-align:middle; at all...
It does not use for vertical centering display-table, display:table-rom; display:table-cell;
It uses the trick of a container that has two divs, one hidden (position is not the correct but makes parent have the correct variable size), one visible just after the hidden but with top:-50%; so it is mover to correct position.
See div classes that make the trick: BloqueTipoContenedor BloqueTipoContenedor_VerticalmenteCentrado BloqueTipoContenido_VerticalmenteCentrado_Oculto BloqueTipoContenido_VerticalmenteCentrado_Visible
Please sorry for using Spanish on classes names (it is because i speak spanish and this is so tricky that if i use English i get lost).
The full code:
As others have said, this isn't something you'd want to be doing in CSS. You can fudge it with absolute positioning and strange margins, but it's just not a robust solution. The best option in your case would be to turn to javascript. In jQuery, this is a very simple task:
or more generically:
For CSS Only solution 1. Either height of wrapper should be fixed or 2. height of second div should be fixed
Here's a solution:
But I suspect you have some content that follows the wrapper div...