Logo hdtoledo
hdtoledo.dev
Módulos_del_Sistema
MÓDULO_TEÓRICO

CSS Grid: The Master Dashboard Layout

CSS Grid Mastery

The Power of Two Dimensions (2D Layouts)

While Flexbox is the king of one-dimensional distribution (a single row or column), CSS Grid is the supreme architect for orchestrating complex two-dimensional layouts (rows and columns simultaneously).

In this lesson, we will use CSS Grid to build the master layout structure of our Nexus Dashboard.


1. The Magic Fraction Unit (fr) and repeat()

The fr unit represents a fraction of remaining free space. Forget calculated hacks like 33.333%:

css
/* Grid of 3 equal metric cards */ .metrics-grid { display: grid; grid-template-columns: repeat(3, 1fr); /* 3 equal fraction columns */ gap: 20px; /* Horizontal and vertical gutter */ }

2. Master Blueprint with grid-template-areas

grid-template-areas is one of the most visual features in CSS. It allows you to draw your interface layout using semantic ASCII words:

css
.dashboard-layout { display: grid; grid-template-columns: 260px 1fr; /* 260px fixed sidebar + Flexible content */ grid-template-rows: 70px 1fr; /* 70px topbar + Remaining screen height */ grid-template-areas: "sidebar header" "sidebar content"; min-height: 100vh; } /* Assign each element to its named slot */ .sidebar { grid-area: sidebar; background: #0d0f18; border-right: 1px solid var(--border-glow); } .topbar { grid-area: header; background: var(--bg-card); } .dashboard-content { grid-area: content; padding: 30px; overflow-y: auto; /* Scannable scroll area */ }

3. Responsive Auto-Fit Grid without Media Queries

Want your widgets to automatically adjust columns across any device width? Use this one-line superpower:

css
.cards-container { display: grid; /* Automatically fits columns with a minimum of 280px and max 1fr */ grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 24px; }