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.
Well, with a bit of absolute positioning and some dodgy margin setting, I can get close, but it's not perfect or pretty:
The "margin-top: 4em" is the particularly nasty bit: this margin needs to be adjusted according to the amount of content in the firstDiv. Depending on your exact requirements, this might be possible, but I'm hoping anyway that someone might be able to build on this for a solid solution.
Eric's comment about javascript should probably be pursued.
you just need this! in css float first div by left or right. float second div by left or right same as first. clear left or right same as above two div for second div. for example:
Have fun my friend.
It is easy with css, just use
display:block
andz-index
propertyHere is an example:
HTML:
CSS:
Edit: It is good to set some
background-color
to thediv-s
to see things properly.I have a simple way to do this.
This can be done using Flexbox.
Create a container that applies both display:flex and flex-flow:column-reverse.
Sources:
CSS really shouldn't be used to restructure the HTML backend. However, it is possible if you know the height of both elements involved and are feeling hackish. Also, text selection will be messed up when going between the divs, but that's because the HTML and CSS order are opposite.
Where XXX and YYY are the heights of firstDiv and secondDiv respectively. This will work with trailing elements, unlike the top answer.