Aller au contenu principal
← 🎨 CSS as a design system·Module B3 · Leçon 6
TâcheBuild a dashboard using grid-template-areas. .dashboard: display: grid, grid-template-columns: 200px 1fr, grid-template-rows: 60px 1fr 40px, grid-template-areas: 'header header' 'sidebar main' 'footer footer', min-height: 200px. Children: header.h, aside.s, main.m, footer.f — each assigned via grid-area.

grid-template-areas : régions nommées

100 XP9 min
Théorie

Visual layout as ASCII

The most readable grid syntax in CSS:

.dashboard {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: 60px 1fr 40px;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  min-height: 100vh;
}
.dashboard > header { grid-area: header; }
.dashboard > .sidebar { grid-area: sidebar; }
.dashboard > .main { grid-area: main; }
.dashboard > footer { grid-area: footer; }

The strings inside grid-template-areas are the layout WIRE-FRAME. Each name marks a region. Then assign each child to a region with grid-area.

Why this is unbeatable for layouts

  • You can SEE the layout in the CSS.
  • Reordering means moving names around — no math.
  • Empty cell? Use . (a period).
  • Same area on multiple lines? It spans.
grid-template-areas:
  "header header header"
  "sidebar main main"
  "sidebar . extras"          /* . = empty cell */
  "footer footer footer";

Responsive: just rewrite the areas

@media (max-width: 700px) {
  .dashboard {
    grid-template-columns: 1fr;        /* stack everything */
    grid-template-areas:
      "header"
      "main"
      "sidebar"
      "footer";
  }
}

The CHILD selectors don't change. You just describe a new wire-frame for the smaller screen.

🔒

Inscris-toi pour coder

La théorie est ouverte à tous. L'éditeur, l'aperçu live et le check se débloquent avec un essai gratuit de 7 jours — carte requise, annulable à tout moment.

S'inscrire — essai gratuit →

Les 10 premières leçons de chaque parcours sont gratuites. Pas de carte requise.

← PrécédentLeçon suivante →

Recevez une astuce Python ou web par jour — par email

Court, écrit à la main, sans spam. Désabonnement en un clic.