How can I reorder my divs using only CSS?

后端 未结 24 1142
傲寒
傲寒 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:12

    There is absolutely no way to achieve what you want through CSS alone while supporting pre-flexbox user agents (mostly old IE) -- unless:

    1. You know the exact rendered height of each element (if so, you can absolutely position the content). If you're dealing with dynamically generated content, you're out of luck.
    2. You know the exact number of these elements there will be. Again, if you need to do this for several chunks of content that are generated dynamically, you're out of luck, especially if there are more than three or so.

    If the above are true then you can do what you want by absolutely positioning the elements --

    #wrapper { position: relative; }
    #firstDiv { position: absolute; height: 100px; top: 110px; }
    #secondDiv { position: absolute; height: 100px; top: 0; }
    

    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.

提交回复
热议问题