<h3>Smoothly change flex direction with GSAP's Flip plugin</h3>

<button id="changeLayout" class="button">change</button>
<div class="group">
  <div class="box">Common "FLIP" techniques employed by other tools won't work with flex elements because of the way browsers handle width/height. 
  </div>
  <div class="box">Simply set <code>absolute: true</code> to have GSAP's Flip plugin make the elements position: absolute during the flip to work around challenges with flex and grid layouts. 
  </div>
  <div class="box">When the flip animation is done, elements get reverted back to their normal positioning and everything appears seamless.</div>
  <div class="box">Happy flipping!</div>
</div>
<a class="gsap-logo" target="_blank" href="gsap.com">
  <img src="https://assets.codepen.io/16327/gsap-logo-light.svg" alt="" />
</a>
/* Typography */

@font-face {
  font-display: block;
  font-family: Mori;
  font-style: normal;
  font-weight: 400;
  src: url(https://assets.codepen.io/16327/PPMori-Regular.woff) format("woff");
}

@font-face {
  font-display: block;
  font-family: Mori;
  font-style: normal;
  font-weight: 600;
  src: url(https://assets.codepen.io/16327/PPMori-SemiBold.woff) format("woff");
}

@font-face {
  font-display: block;
  font-family: Fraktion Mono;
  font-style: normal;
  font-weight: 400;
  src: url(https://assets.codepen.io/16327/PPFraktionMono-Bold.woff)
    format("woff");
}

:root {
  --color-shockingly-green: #0ae448;
  --color-just-black: #0e100f;
  --color-surface-white: #fffce1;
  --color-pink: #fec5fb;
  --color-shockingly-pink: #f100cb;
  --color-orangey: #ff8709;
  --color-lilac: #9d95ff;
  --color-lt-green: #abff84;
  --color-blue: #00bae2;
  --color-grey: gray;
  --color-surface75: #bbbaa6;
  --color-surface50: #7c7c6f;
  --color-surface25: #42433d;
}

body {
  color: var(--color-surface-white);
  background-color: var(--color-just-black);
  font-family: Mori, sans-serif;
  font-weight: 400;
  margin: 0;
  font-size: 1rem;
  line-height: 1.45;
  text-align: center;
}

.group.reorder {
  flex-direction: row;
}

h1 {
  font-weight: 400;
}

.group {
  width: 740px;
  height: auto;
  margin: 1rem auto;
  padding: 4px;
 border: dashed 2px var(--color-surface-white);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: var(--color-just-black);
  position: relative;
  border-radius: 10px;
}

.box {
  margin: 4px;
  padding: 8px;
  line-height: 28px;
  border-radius: 10px;
}
.box:nth-child(1) {
  background-color: #bef3fe;
}
.box:nth-child(2) {
   background-color: var(--color-blue);
}
.box:nth-child(3) {
  background-color: var(--color-pink);
}
.box:nth-child(4) {
   background-color: #e1faff;
}

button {
  margin: 0 auto;
  display: inline-block;
  outline: none;
  padding: 8px 14px;
  background: var(--dark);
  border: solid 2px var(--color-surface-white);
  color: var(--light);
  text-decoration: none;
  border-radius: 99px;
  padding: 12px 25px;
  text-transform: uppercase;
  font-weight: 600;
  cursor: pointer;
  line-height: 18px;
}

.gsap-logo {
  width: 20vw;
  max-width: 90px;
  position: fixed;
  bottom: 15px;
  right: 15px;
  mix-blend-mode: difference;
  z-index: 9999;
}

img {
  max-width: 100%;
}
gsap.registerPlugin(Flip);

const group = document.querySelector(".group");

document.querySelector(".button").addEventListener("click", () => {
  // Get the initial state
  const state = Flip.getState(".group, .box");
  
  console.log(state);
  
  // toggle the flex direction
  group.classList.toggle("reorder");
  
  Flip.from(state, {
    absolute: true, // uses position: absolute during the flip to work around flexbox challenges
    duration: 0.5, 
    stagger: 0.1,
    ease: "power1.inOut"
    // you can use any other tweening properties here too, like onComplete, onUpdate, delay, etc. 
  });
});
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://assets.codepen.io/16327/Flip.min.js
  2. https://unpkg.co/gsap@3/dist/gsap.min.js