Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrapper">
  <div class="cards-container">
    <div class='header'>
      <button 
        class="randomize-button btn btn-secondary"
      >Reset</button>
      <button 
        class="add-card-button btn btn-secondary"
      >Add Card</button>
      <button 
        class="align-button btn btn-primary"
        data-bs-toggle="button"
      >Align</button>
      <button
        class="spacer-vis-button btn btn-primary"
        data-bs-toggle="button"
      >Toggle Spacers Visible</button>
    </div>

    <div class='cards'></div>
  </div>
</div>

              
            
!

CSS

              
                html, body {
  width: 100%;
  height: 100%;
}
.wrapper {
  display: flex;
}
.cards-container {
  display: inline-block;
  width: auto;
  min-width: 620px;
  margin: 25px auto;
}
.header {
  text-align: center;
  margin: 0 0 20px 0;
}
.cards {
  height: 100%;
  display: flex;
  justify-content: start;
  align-items: start;
  flex-wrap: wrap;
}
.card {
  flex: 0 0 180px;
  display: inline-block;
  margin: 10px 10px;
  background-color: grey;
}
.card-section {
  width: 100%;
}
.height-sync {
  transition: height .75s, background-color .75s linear;
  transition-timing-function: cubic-bezier(.5, .25, 0, 1);
}
.flatten {
  height: 0 !important;
}
.spacer-vis {
  background-color: white;
  opacity: .6;
}
              
            
!

JS

              
                
/*
 * Height alignment utility using ResizeObserver
 */
class HeightSync {
  constructor() {
    this.reset();
  }
  recalcHeights(groupKey) {
      const els = this.registry[groupKey]?.els;
      if (!els || !els.length) return;

      // Calculate max height and update spacers
      let maxHeight = 0;
      for (const { el, spacer } of els) {
          const curr = el.clientHeight - (spacer.clientHeight || 0);
          if (curr > maxHeight) {
              maxHeight = curr;
          }
      }
      for (const { el, spacer } of els) {
          const curr = el.clientHeight - (spacer.clientHeight || 0);
          const newSpacer = maxHeight - curr;
          spacer.style.height = newSpacer + 'px';
      }
  }
  registerHeightSync(el, groupKey = '_') {
      if (!el) return;

      let spacer = el.querySelector(`div.height-sync`);
      if (!spacer) {
          spacer = document.createElement('div');
          spacer.classList.add('height-sync');
          el.append(spacer);
      }

      this.registry[groupKey] = this.registry[groupKey] || {
          els: [],
          recalc: () => this.recalcHeights(groupKey),
      };
      this.registry[groupKey].els.push({
          el,
          spacer,
      });
      this.registry._lookup.set(el, groupKey);
      this.resizeObserver.observe(el);
  }
  reset() {
    this.registry = {
      _lookup: new Map(),
    };
    this.resizeObserver = new ResizeObserver((entries) => {
        const targetKeys = new Set(
            entries.map((entry) => this.registry._lookup.get(entry.target))
        );
        targetKeys.forEach((groupKey) => this.registry[groupKey]?.recalc());
    });
  }
}

/*
 * Utility usage demo
 */
const config = {
  cardCt: 3,
  sectionBaseHeights: [40, 40, 35],
  sectionRandHeight: 50,
  colors: [
    '#4285F4',
    '#DB4437',
    '#F4B400',
    '#0F9D58',
  ],
  alignToggled: false,
  visToggled: false,
}

const alignBtn = document.querySelector('.align-button');
const visBtn = document.querySelector('.spacer-vis-button');
const randomizeBtn = document.querySelector('.randomize-button');
const addBtn = document.querySelector('.add-card-button');
const cardsEl = document.querySelector('.cards');
const heightSync = new HeightSync();
init();

function init() {
  bindEvents();
  renderCards();
  updateSpacerBtnDisabled();
}

function bindEvents() {
  alignBtn.addEventListener("click", handleAlignClick);
  visBtn.addEventListener("click", handleSpacerVisClick);
  randomizeBtn.addEventListener("click", handleRandomizeClick);
  addBtn.addEventListener("click", handleAddCardClick);
}

function renderCards() {
  cardsEl.innerHTML = '';
  shuffle(config.colors);
  
  for (let i = 0; i < config.cardCt; i++) {
    renderCard(cardsEl);
  }
}

function renderCard(hostEl) {
    const card = document.createElement("div");
    card.classList.add("card");
  
    hostEl.append(card);

    for (let i = 0; i < config.sectionBaseHeights.length; i++) {
      const section = document.createElement("div");
      const sectionContent = document.createElement("div");
      sectionContent.style.height =
        (config.sectionBaseHeights[i] + randomInt(0, config.sectionRandHeight)) + 'px';
      section.append(sectionContent);
      section.style.backgroundColor = config.colors[i];
      section.classList.add("card-section");
      card.append(section);
      heightSync.registerHeightSync(section, 'section-' + i);
    }
  
    !config.alignToggled && toggleSpacersFlat(card);
    config.visToggled && toggleSpacerVisibility(card);
}

function handleAddCardClick(e) {
  renderCard(cardsEl);
}

function handleAlignClick(e) {
  config.alignToggled = !config.alignToggled;
  updateSpacerBtnDisabled();
  toggleSpacersFlat();
}

function updateSpacerBtnDisabled() {
  if (!config.alignToggled) {
    visBtn.classList.add('disabled');
  } else {
    visBtn.classList.remove('disabled');
  }
}

function toggleSpacersFlat(scope) {
  const spacers = (scope || document).querySelectorAll('.height-sync');
  spacers.forEach(sp => sp.classList.toggle('flatten'));
}

function handleSpacerVisClick(e) {
  config.visToggled = !config.visToggled;
  toggleSpacerVisibility();
}

function toggleSpacerVisibility(scope) {
  const spacers = (scope || document).querySelectorAll('.height-sync');
  spacers.forEach(sp => sp.classList.toggle('spacer-vis'));
} 

function handleRandomizeClick(e) {
  renderCards();
}

function shuffle(array) {
  for (var i = array.length - 1; i > 0; i--) {
      var j = Math.floor(Math.random() * (i + 1));
      var temp = array[i];
      array[i] = array[j];
      array[j] = temp;
  }
}

function randomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


              
            
!
999px

Console