How can I reorder my divs using only CSS?

后端 未结 24 1185
傲寒
傲寒 2020-11-22 01:53

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

24条回答
  •  失恋的感觉
    2020-11-22 01:56

    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.

提交回复
热议问题