<!-- BOOK TEMPLATE -->
<template id="book-template">
  <style>
    /* Layout Container */
    :host {
      container-type: inline-size;
      cursor: grab;
      display: block;
    }
    
    /* Base Styles */
    ::slotted(img) {
      display:block;
      width:100%;
      height: auto;
      border-radius: 0px 2px 2px 0px;
      background-color: var(--cover-color);
    }
    .book {
      font-size: var(--font-size, 1rem);
      font-weight: 400;
    }
    .meta {
      line-height: 1.3;
    }
    .title {
      font-size: 1.25em;
      font-family: "Merriweather", serif;
      margin-bottom:.5rem;
    }
    .author {
      font-size: .875em;
      font-family: sans-serif;
      margin: .5rem 0 1rem;
    }
    .btn {
      display: none;
      padding: 1em 1.5em;
      color: #FFF;
      background-color: #222;
      border:0;
      border-radius: 2em;
      font-size: 1rem;
    }
    
    /* Small Variant */
    @container (max-width: 200px) {
      .book {
        --font-size: .5rem;
      }
      .author {
        display: none;
      }
    }
    
    /* Medium Variant */
    @container ((min-width: 201px) and (max-width: 399px)) {
      .book {
        --font-size: 1rem;
        display: grid;
        align-items: start;
        grid-template-columns: 1fr 1fr;
        gap: 1rem;
      }
    }
    
    /* Large Variant */
    @container (min-width: 400px) {
      ::slotted(img) {
        transform: translateZ(40px);
        box-shadow: 5px 5px 20px rgba(0,0,0,0.1);
      }
      .book {
        --font-size: 1.75rem;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 2rem;
        padding: 2rem;
      }
      .image {
        perspective:1000px;
        flex: 0 1 20rem;
      }
      .front {
        position: relative;
        transition: transform .5s ease;
        transform-style: preserve-3d;
        transform: rotateY(-25deg);
        
      }
      .front::before {
        position: absolute;
        content: ' ';
        left: 100%;
        top: 1%;
        width: 80px;
        height: 98%;
        transform: translate(-55%,0) rotateY(90deg);
        background: linear-gradient(
    90deg
    , #fff 0%, #f9f9f9 5%, #fff 10%, #f9f9f9 15%, #fff 20%, #f9f9f9 25%, #fff 30%, #f9f9f9 35%, #fff 40%, #f9f9f9 45%, #fff 50%, #f9f9f9 55%, #fff 60%, #f9f9f9 65%, #fff 70%, #f9f9f9 75%, #fff 80%, #f9f9f9 85%, #fff 90%, #f9f9f9 95%, #fff 100% );
      }
      .front::after {
        position: absolute;
        top: 0;
        left: 1%;
        content: ' ';
        width: 100%;
        height: 100%;
        transform: translateZ(-40px);
        background-color: var(--cover-color, #000);
        border-radius: 0 2px 2px 0;
        box-shadow: -10px 0 50px 10px rgba(0,0,0,0.3);
      }
      .btn {
        display: inline-block;
      }
    }
  </style>
  <article class="book">
    <div class="image">
      <div class="front">
        <slot name="cover"></slot>
      </div>
    </div>
    <div class="meta">
      <h2 class="title"><slot name="title"></slot></h2>
      <p class="author"><slot name="author"></slot></p>
      <button class="btn">Buy now</button>
    </div>
  </article>
</template>
<!-- END BOOK TEMPLATE -->



<div class="layout">
  <!-- HEADER -->
  <header class="header">
    <h1 class="logo">Container Query Bookstore</h1>
    <p class="by"><a href="https://mxb.dev">Max Böck</a></p>
  </header>

  <!-- FEATURED STAGE -->
  <div class="stage js-drag-container">
    <h2 class="capitals">Featured</h2>
    
      <book-element color="#52947c">
        <img slot="cover" src="https://assets.codepen.io/1256430/oz.avif" alt="" />
        <span slot="title">The Wizard of Oz</span>
        <span slot="author">L. Frank Baum</span>
      </book-element>

  </div>

  <!-- MAIN CONTENT AREA -->
  <main class="content">
    <h2 class="capitals">Bestsellers</h2>

    <div class="support-notice">
      ⚠️ Your browser does not support Container Queries - Switch to a <a href="https://caniuse.com/css-container-queries">modern browser</a> to see this demo.
    </div>

    <div class="booklist js-drag-container">
      
      <book-element color="#BA423D">
        <img slot="cover" src="https://assets.codepen.io/1256430/1984.avif" alt="" />
        <span slot="title">1984</span>
        <span slot="author">George Orwell</span>
      </book-element>

      <book-element color="#d2d5dc">
        <img slot="cover" src="https://assets.codepen.io/1256430/little-women.avif" alt="" />
        <span slot="title">Little Women</span>
        <span slot="author">Louisa May Alcott</span>
      </book-element>

      <book-element color="#fefef2">
        <img slot="cover" src="https://assets.codepen.io/1256430/fahrenheit-451.avif" alt="" />
        <span slot="title">Fahrenheit 451</span>
        <span slot="author">Ray Bradbury</span>
      </book-element>
      
      <book-element color="#0c0d12">
        <img slot="cover" src="https://assets.codepen.io/1256430/moby-dick.avif" alt="" />
        <span slot="title">Moby Dick</span>
        <span slot="author">Herman Melville</span>
      </book-element>
      
      <book-element color="#1480a8"> 
        <img slot="cover" src="https://assets.codepen.io/1256430/pride.avif" alt="" />
        <span slot="title">Pride and Prejudice</span>
        <span slot="author">Jane Austen</span>
      </book-element>
      
      <book-element color="#cb0f2d">
        <img slot="cover" src="https://assets.codepen.io/1256430/sputnik-sweetheart.avif" alt="" />
        <span slot="title">Sputnik Sweetheart</span>
        <span slot="author">Haruki Murakami</span>
      </book-element>

    </div>
  </main>


  <!-- SHOPPING CART -->
  <div class="cart js-drag-container">
    <h2 class="capitals">Cart</h2>
    
    <book-element color="#067682">
      <img slot="cover" src="https://assets.codepen.io/1256430/don-quixote.avif" alt="" />
      <span slot="title">Don Quixote</span>
      <span slot="author">Miguel de Cervantes</span>
    </book-element>

  </div>
</div>

<!-- INCLUDE DRAGULA.JS -->
<script src="https://unpkg.com/dragula@3.7.2/dist/dragula.min.js"></script>
:root {
  --spacing: 1rem;
  --highlight-color: #52947c;
}

body {
  font-family: sans-serif;
}

/* Basic Layout Sections */

.layout {
  min-height: 100vh;
}

.header {
  display:flex;
  align-items: center;
  justify-content: space-between;
  height: 3.5rem;
  padding: 0 var(--spacing);
  box-shadow:0 2px 16px rgba(0,0,0,0.1);
  position:relative;
  z-index: 1;
}

.stage,
.content,
.cart {
  padding: var(--spacing);
  overflow-y: auto;
}

.stage {
  position:relative;
}

.stage::after {
  content: "";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background-color: var(--bg-color, var(--highlight-color));
  filter: brightness(1.5);
  transition: background-color .5s linear;
  opacity:0.25;
}

.booklist {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(201px, 1fr));
  gap: var(--spacing);
}

.cart {
  background-color: #f5f3f3;
}
.cart > * + * {
  margin-top: 1rem;
}

/* Desktop Layout */

@media (min-width: 1260px) {
  .layout {
    --spacing: 2rem;
    
    display:grid;
    min-height: 100vh;
    grid-template-rows: 3.5rem auto;
    grid-template-columns: 480px 1fr 130px;
    overflow-y: hidden;
  }
  .header {
    grid-column: 1 / -1;
  }
}

/* No-CQ Support Notice */

.support-notice {
  padding: 1rem;
  border-radius: .25rem;
  margin-bottom: var(--spacing);
  border: 2px solid salmon;
  background-color: rgba(salmon, .1);
  line-height: 1.4;
}

@supports (contain: layout style inline-size) {
  .support-notice {
    display: none;
  }
}

/* Misc Helper Classes */

.gu-mirror {
  width: 180px !important;
}

.capitals {
  font-size: .75rem;
  font-weight: bold;
  letter-spacing:.25em;
  text-transform: uppercase;
  margin-bottom: var(--spacing);
}

.logo {
  font-family: "Merriweather", serif;
}

.by a {
  color: inherit;
  text-decoration:underline;
}

p {
  margin:0;
}
// Init Dragula
if (dragula) {
  const drg = dragula(Array.from(document.querySelectorAll('.js-drag-container')))
  drg.on('drop', (el, target, source, sibling) => {
    if (target.classList.contains('stage')) {
      const color = el.getAttribute('color')
      target.style.setProperty('--bg-color', color)
      
      if (sibling && sibling.tagName.toLowerCase() === 'book-element') {
        source.appendChild(sibling)
      }
    }
  })
}

// Custom Element Definition
class BookElement extends HTMLElement {
  constructor() {
    super()
    this.template = document.getElementById('book-template')
    
    if (this.template) {
      this.attachShadow({ mode: "open" }).appendChild(this.template.content.cloneNode(true))
    }
  }
  
  static get observedAttributes() {
    return ['color'];
  }
  
  attributeChangedCallback(attrName, oldValue, newValue) {
    if (newValue !== oldValue) {
      this[attrName] = this.getAttribute(attrName);
      this.update();
    }
  }
  
  connectedCallback() {
    this.update();
  }
  
  update() {
    if (this.color) {
      this.style.setProperty('--cover-color', this.color)
    }
  }
}

if ('customElements' in window) {
    customElements.define('book-element', BookElement)
}

External CSS

  1. https://fonts.googleapis.com/css2?family=Merriweather&amp;display=swap
  2. https://unpkg.com/dragula@3.7.2/dist/dragula.min.css

External JavaScript

This Pen doesn't use any external JavaScript resources.