CSS

Responsive Auto-Placing Grid Items with minmax()

Efficiently create responsive grid layouts that automatically adjust column count and size based on available space using CSS Grid's `repeat(auto-fit, minmax())`.

<style>
  .responsive-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    padding: 1rem;
    max-width: 1200px;
    margin: 0 auto;
  }

  .grid-item {
    background-color: #e0f2f7;
    border: 1px solid #a7d9e8;
    padding: 1rem;
    text-align: center;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  }
</style>

<div class="responsive-grid">
  <div class="grid-item">Item 1</div>
  <div class="grid-item">Item 2</div>
  <div class="grid-item">Item 3</div>
  <div class="grid-item">Item 4</div>
  <div class="grid-item">Item 5</div>
  <div class="grid-item">Item 6</div>
  <div class="grid-item">Item 7</div>
  <div class="grid-item">Item 8</div>
</div>
How it works: This CSS Grid snippet creates a highly responsive layout where items automatically arrange themselves into the optimal number of columns. `grid-template-columns: repeat(auto-fit, minmax(200px, 1fr))` is the key. `auto-fit` will fit as many columns as possible, expanding them to fill the space. `minmax(200px, 1fr)` ensures each column is at least 200px wide, but can grow up to `1fr` (one fraction of the available space), maintaining equal widths among columns if there's extra space. `gap` provides consistent spacing between grid items.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs