How can I reorder my divs using only CSS?

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

    This can be done with CSS only!

    Please check my answer to this similar question:

    https://stackoverflow.com/a/25462829/1077230

    I don't want to double post my answer but the short of it is that the parent needs to become a flexbox element. Eg:

    (only using the webkit vendor prefix here.)

    #main {
        display: -webkit-box;
        display: -webkit-flex;
        display: flex;
        -webkit-box-orient: vertical;
        -webkit-flex-direction: column;
        flex-direction: column;
        -webkit-box-align: start;
        -webkit-align-items: flex-start;
        align-items: flex-start;
    }
    

    Then, swap divs around by indicating their order with:

    #main > div#one{
        -webkit-box-ordinal-group: 2;
        -moz-box-ordinal-group: 2;
        -ms-flex-order: 2;
        -webkit-order: 2;
        order: 2;
        overflow:visible;
    }
    
    #main > div#two{
        -webkit-box-ordinal-group: 1;
        -moz-box-ordinal-group: 1;
        -ms-flex-order: 1;
        -webkit-order: 1;
        order: 1;
    }
    

提交回复
热议问题