How can I reorder my divs using only CSS?

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

    This can be done using Flexbox.

    Create a container that applies both display:flex and flex-flow:column-reverse.

    /* -- Where the Magic Happens -- */
    
    .container {
      
      /* Setup Flexbox */
      display: -webkit-box;
      display: -moz-box;
      display: -ms-flexbox;
      display: -webkit-flex;
      display: flex;
    
      /* Reverse Column Order */
      -webkit-flex-flow: column-reverse;
      flex-flow: column-reverse;
    
    }
    
    
    /* -- Styling Only -- */
    
    .container > div {
      background: red;
      color: white;
      padding: 10px;
    }
    
    .container > div:last-of-type {
      background: blue;
    }
    first
    second

    Sources:

    • https://css-tricks.com/using-flexbox/
    • https://developer.mozilla.org/en-US/docs/Web/CSS/flex-flow#Browser_compatibility

提交回复
热议问题