How can I reorder my divs using only CSS?

后端 未结 24 1111
傲寒
傲寒 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 02:00

    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.

提交回复
热议问题