css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>CSS Flex: Smooth Wrapping</h1>
<div class="title">(no dependency libraries)</div>
<div class="title-link"><a href="https://codepen.io/KeithDC/post/css-flex-smooth-wrapping-success-no-dep-libs">CodePen Post (the backstory)</a></div>

<div class="wrapper">
  <div class="options">
    <button class="button">Change Width</button>
    <p>Although the 'Change Width' button works, the transitons are cleaner when resizing the window/frame.</p>
  </div>

  <div class="container">
    <div class="item item-node">1</div>
    <div class="item item-node">2</div>
    <div class="item item-node">3</div>
    <div class="item item-node">4</div>
    <div class="item item-node">5</div>
    <div class="item item-node">6</div>
  </div>
</div>
            
          
!
            
              body {
  margin: 0 auto;
}
.wrapper {
  margin: 1rem auto;
  padding: 0 0 1rem;
  position: relative;
  text-align: center;
  width: 95%;
}
.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-around;
  margin: 0 auto;
  padding: 0 0.5rem 1rem;
  position: relative;
  transition: all 2s;
}
.container > div.item {
  align-items: center;
  background-color: hsl(133, 100%, 80%);
  display: flex;
  flex: 0 1 auto;
  justify-content: center;
  outline: 3px solid green;
  margin: 0.5rem;
  padding: 0.5rem;
  position: relative;
    min-height: 100px;
    width: 150px;
  transition: top 0.5s, left 0.5s;
}
.container > div.item-node {
  visibility: hidden;
}
.container > div.item-dupe {
  position: absolute;
  visibility: visible;
}

/*  Superfluous  */

h1,
h2,
h3 {
  color: green;
  margin: 1rem auto 0;
  padding: 0;
  text-align: center;
}
.title,
.title-link {
  color: green;
  margin: 0.75rem auto 0;
  text-align: center;
}
.title-link {
  margin: 0.5rem auto 0;
}
.title-link a {
  color: orange;
  text-decoration: none;
  transition: all 0.25s;
}
.title-link a:hover {
  color: maroon;
  text-decoration: none;
  transition: all 0.25s;
}
.container.switch {
  transition: all 2s;
  width: 50%;
}
div.options {
  margin: 0 auto;
  padding: 1rem 0 0;
  text-align: center;
  width: 400px;
}
  button {
    cursor: pointer;
  }
  div.options p {
    opacity: 0.5;
  }
.no-bold {
  font-weight: normal;
}
            
          
!
            
              const nodes = document.querySelectorAll(".item-node"),
      totalNodes = nodes.length,
      boxes = [];

let nodeCnt = 0,      // Node count: Loop through nodes[]
    resizeWaitID = 0, // setTimeout ID for window.resize()
    node = {},
    dupe = {};

for (nodeCnt = 0; nodeCnt < totalNodes; nodeCnt++) {
  node = nodes[nodeCnt];

  dupe = node.cloneNode(true);        // We'll clone each node so it can "follow" its sibling `node` element around the UI.
  dupe.classList.remove("item-node"); // .item-node | visibility: hidden - Will move natively with Flex wrapping (snappy!).
  dupe.classList.add("item-dupe");    // .item-dupe | visibility: visible - Will smoothly follow sibling .item-node around.

  node.parentNode.appendChild(dupe);  // Position: `absolute` - Each clone stays relative to their shared parent container.

  dupe.style.top = node.offsetTop + 'px';   // Establish each dupe's `absolute` position.
  dupe.style.left = node.offsetLeft + 'px'; // <--^

  boxes[nodeCnt] = { node, dupe };
}

function moveNodes() {
  clearTimeout(resizeWaitID);
  resizeWaitID = setTimeout(() => {
    for (nodeCnt = 0; nodeCnt < totalNodes; nodeCnt++) {
      boxes[nodeCnt].dupe.style.left = boxes[nodeCnt].node.offsetLeft + 'px';
      boxes[nodeCnt].dupe.style.top = boxes[nodeCnt].node.offsetTop + 'px';
    }
  }, 101);
}

window.addEventListener("resize", moveNodes);

const divSwitch = document.querySelector('.container');

document.querySelector('.button').addEventListener('click', e => {
  moveNodes();
  divSwitch.classList.toggle('switch');
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console