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.
This can be done with CSS only!
Please check my answer to this similar question:
https://stackoverflow.com/a/25462829/1077230
I don't want to double post my answer but the short of it is that the parent needs to become a flexbox element. Eg:
(only using the webkit vendor prefix here.)
Then, swap divs around by indicating their order with:
A solution with a bigger browser support then the flexbox (works in IE≥9):
In contrast to the
display: table;
solution this solution works when.wrapper
has any amount of children.There is absolutely no way to achieve what you want through CSS alone while supporting pre-flexbox user agents (mostly old IE) -- unless:
If the above are true then you can do what you want by absolutely positioning the elements --
Again, if you don't know the height want for at least #firstDiv, there's no way you can do what you want via CSS alone. If any of this content is dynamic, you will have to use javascript.
Little late to the party, but you can also do this:
Negative top margins can achieve this effect, but they would need to be customized for each page. For instance, this markup...
...and this CSS...
...would allow you to pull the second paragraph above the first.
Of course, you'll have to manually tweak your CSS for different description lengths so that the intro paragraph jumps up the appropriate amount, but if you have limited control over the other parts and full control over markup and CSS then this might be an option.