Back to CSS Grid Layouts Container-Query Card Pure CSS
Share
HTML
<div class="gl-cq">
  <section class="gl-cq-card gl-cq-wide">
    <article class="gl-cq-inner">
      <span class="gl-cq-name">Container ≥ 320px</span>
      <header class="gl-cq-img">
        <strong>Image</strong>
        <span class="gl-cq-coord">img · col 1</span>
      </header>
      <div class="gl-cq-body">
        <h4>Card with container queries</h4>
        <p>Resize the card and the layout switches — no media query, no JS.</p>
        <span class="gl-cq-coord gl-cq-coord-light">body · col 2 · @container ≥ 320px</span>
      </div>
    </article>
  </section>
  <section class="gl-cq-card gl-cq-narrow">
    <article class="gl-cq-inner">
      <span class="gl-cq-name">Container &lt; 320px</span>
      <header class="gl-cq-img">
        <strong>Image</strong>
        <span class="gl-cq-coord">img · row 1</span>
      </header>
      <div class="gl-cq-body">
        <h4>Stacks below 320px</h4>
        <p>Same component. Different container width.</p>
        <span class="gl-cq-coord gl-cq-coord-light">body · row 2 · @container &lt; 320px</span>
      </div>
    </article>
  </section>
</div>
CSS
.gl-cq {
  display: grid;
  grid-template-columns: 1fr 260px;
  gap: 18px;
  width: 100%;
  min-height: 480px;
  padding: 22px;
  background:
    radial-gradient(60% 80% at 50% 0%, rgba(20,184,166,0.14), transparent 60%),
    #042022;
  font-family: 'Inter', system-ui, sans-serif;
  color: #ccfbf1;
  box-sizing: border-box;
  align-items: stretch;
}
.gl-cq-card {
  container-type: inline-size;
  background: #0d3030;
  border: 1px solid rgba(20,184,166,0.28);
  border-radius: 14px;
  padding: 16px;
}
.gl-cq-inner {
  display: grid;
  gap: 14px;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto 1fr;
  grid-template-areas: "name" "img" "body";
}
.gl-cq-name {
  grid-area: name;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px; font-weight: 600; letter-spacing: 0.08em;
  color: #2dd4bf;
  background: rgba(20,184,166,0.14);
  padding: 4px 10px; border-radius: 999px;
  align-self: flex-start; justify-self: flex-start;
}
.gl-cq-img {
  grid-area: img;
  background: linear-gradient(135deg, #14b8a6, #0d9488);
  min-height: 120px;
  border-radius: 10px;
  padding: 14px;
  display: flex; flex-direction: column;
  justify-content: space-between;
  color: #042022;
  margin: 0;
}
.gl-cq-img strong { font-size: 16px; font-weight: 700; }
.gl-cq-coord {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px; letter-spacing: 0.04em;
  align-self: flex-start;
  padding: 2px 8px; border-radius: 999px;
  background: rgba(4,32,34,0.25); color: #042022;
}
.gl-cq-coord-light { color: #2dd4bf; background: rgba(20,184,166,0.14); }
.gl-cq-body {
  grid-area: body;
  padding: 4px 2px;
  display: flex; flex-direction: column;
  gap: 6px;
}
.gl-cq-body h4 { margin: 0; font-size: 14px; font-weight: 700; color: #fff; }
.gl-cq-body p { margin: 0; font-size: 12.5px; color: #99d4cb; line-height: 1.5; }
.gl-cq-body .gl-cq-coord { align-self: flex-start; }
@container (min-width: 320px) {
  .gl-cq-inner {
    grid-template-columns: 180px 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "name name"
      "img  body";
  }
  .gl-cq-inner .gl-cq-img { min-height: 200px; }
  .gl-cq-inner .gl-cq-body { justify-content: center; }
}
.gl-cq-narrow { max-width: 260px; }
@media (max-width: 720px) {
  .gl-cq { grid-template-columns: 1fr; }
  .gl-cq-narrow { max-width: 100%; }
}