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
Well, with a bit of absolute positioning and some dodgy margin setting, I can get close, but it's not perfect or pretty:
#wrapper { position: relative; margin-top: 4em; }
#firstDiv { position: absolute; top: 0; width: 100%; }
#secondDiv { position: absolute; bottom: 0; width: 100%; }
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.
If you know, or can enforce the size for the to-be-upper element, you could use
position : absolute;
In your css and give the divs their position.
otherwise javascript seems the only way to go:
fd = document.getElementById( 'firstDiv' );
sd = document.getElementById( 'secondDiv' );
fd.parentNode.removeChild( fd );
sd.parentNode.insertAfter( fd, sd );
or something similar.
edit: I just found this which might be useful: w3 document css3 move-to
A CSS-only solution (works for IE10+) – use Flexbox's order
property:
Demo: http://jsfiddle.net/hqya7q6o/596/
#flex { display: flex; flex-direction: column; }
#a { order: 2; }
#b { order: 1; }
#c { order: 3; }
<div id="flex">
<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
</div>
More info: https://developer.mozilla.org/en-US/docs/Web/CSS/order
If you just use css, you can use flex.
.price {
display: flex;
align-items: center;
justify-content: center;
flex-direction: row-reverse; //revert horizontally
//flex-direction: column-reverse; revert vertically
}
<div class="price">
<div>first block</div>
<div>second block</div>
</div>
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.
#firstDiv { position: relative; top: YYYpx; height: XXXpx; }
#secondDiv { position: relative; top: -XXXpx; height: YYYpx; }
Where XXX and YYY are the heights of firstDiv and secondDiv respectively. This will work with trailing elements, unlike the top answer.
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:
$('#secondDiv').insertBefore('#firstDiv');
or more generically:
$('.swapMe').each(function(i, el) {
$(el).insertBefore($(el).prev());
});