x ai CSS: grid layout 2026-06-06

xtodo

ai generated

Repeat notation (very common):

.xgrid {
 grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
 grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* Responsive cards */
}

3. Placing Grid Items

Items are placed automatically by default (row by row). You can control placement manually:

.item {
 /* Start and end lines (1-based indexing) */
 grid-column: 1 / 3; /* Spans from column line 1 to 3 */
 grid-row: 2 / 4; /* Spans from row line 2 to 4 */

 /* Shorthand */
 grid-area: row-start / column-start / row-end / column-end;
}

Named lines (great for clarity):

.xgrid {
 grid-template-columns: [sidebar] 250px [content] 1fr [end];
}

.sidebar {
 grid-column: sidebar;
}

4. Grid Areas (Named Regions)

This is one of the most powerful features:

.xgrid {
 display: grid;
 grid-template-areas: "header header" "sidebar content" "footer footer";
 grid-template-columns: 250px 1fr;
 grid-template-rows: 80px 1fr 60px;
}

.header {
 grid-area: header;
}
.sidebar {
 grid-area: sidebar;
}
.content {
 grid-area: content;
}
.footer {
 grid-area: footer;
}

5. Gaps (Gutters)

.x2829 {
 grid-gap: 20px; /* Old shorthand */
 gap: 20px; /* Modern */
 row-gap: 10px;
 column-gap: 30px;
}

6. Alignment

On the container (aligns the grid tracks):

On items:

Individual item overrides:

.item {
 justify-self: center;
 align-self: end;
}

7. Responsive Design Examples

Mobile-first responsive grid:

.xgrid {
 display: grid;
 grid-template-columns: 1fr; /* Single column on mobile */
 gap: 16px;
}

@media (min-width: 600px) {
 grid-template-columns: repeat(2, 1fr);
}

@media (min-width: 900px) {
 grid-template-columns: repeat(3, 1fr);
}

Holy Grail Layout (classic example):

.holy-grail {
 display: grid;
 grid-template-areas:
  "header"
  "nav"
  "main"
  "aside"
  "footer";
 grid-template-rows: auto auto 1fr auto auto;
 min-height: 100vh;
}

@media (min-width: 768px) {
 grid-template-areas:
  "header header"
  "nav main"
  "aside main"
  "footer footer";
 grid-template-columns: 250px 1fr;
}

8. Auto Placement & Implicit Grid

.x8936 {
 /* grid-auto-flow: row | column | dense;  */
 /* dense fills holes */
 grid-auto-rows: 150px;
 grid-auto-columns: minmax(200px, 1fr);
}

9. Complete Practical Example: Card Grid

<div class="cards">
 <div class="card">...</div>
 <!-- many more -->
</div>
.cards {
 display: grid;
 grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
 gap: 24px;
 padding: 20px;
}

.card {
 background: #fff;
 border-radius: 12px;
 padding: 20px;
 box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
 display: flex;
 flex-direction: column;
}

Quick Reference Cheat Sheet

Property Purpose Common Values
display: grid Activate grid
grid-template-columns Column tracks repeat(3, 1fr)
grid-template-rows Row tracks auto 1fr auto
gap Spacing between tracks 20px 30px
grid-column Place item horizontally 2 / 4 or span 2
grid-area Named area or shorthand header
justify-content Align grid horizontally center, space-between
align-items Align items inside cells center, stretch

Tips & Best Practices

Practice by rebuilding common layouts: dashboards, photo galleries, product pages, or the classic Holy Grail.

Would you like a full interactive demo (CodePen-style HTML/CSS), advanced topics (subgrids, masonry-like behavior, animation with Grid), or a specific real-world project example?