Introduction
Flexbox Deep Dive
/* Flex items / .logo { flex: 0 1 auto; / Do not grow, can shrink */ }
.menu { display: flex; /* Nested flex for horizontal links */ gap: 1.5rem; }
.menu a { color: #fff; text-decoration: none; padding: .5rem 0; } </code></pre>
<p>The <code>.navbar</code> example showcases a classic header where the brand logo stays its natural size while the navigation links are evenly spaced. The <code>gap</code> property (supported from Flexbox Level 2) eliminates the need for margin hacks.</p> <h3>Responsive Patterns with Flexbox</h3> <p>Combine media queries with <code>flex-direction</code> to switch between horizontal and vertical layouts:</p> <pre><code class="language-css">@media (max-width: 768px) { .navbar { flex-direction: column; /* Stack items on small screens */ align-items: stretch; } .menu { flex-direction: column; gap: .75rem; } } </code></pre> <p>This approach removes JavaScript-based toggles for a simple, accessible navigation drawer.</p>CSS Grid Mastery
/* Placing an item / .header { grid-column: 1 / -1; / Span all columns */ grid-row: 1; }
.sidebar { grid-column: 1 / 4; /* Columns 1‑3 / grid-row: 2 / 5; / Rows 2‑4 */ }
.main { grid-column: 4 / -1; /* Columns 4‑12 */ grid-row: 2 / 5; } </code></pre>
<p>The <code>-1</code> line shorthand represents the last grid line, simplifying full‑width spans. The example mimics a classic magazine layout: a header, a left sidebar, and a main content area.</p> <h3>Advanced Techniques - Subgrid and CSS Shapes</h3> <p>With <code>grid-template-areas</code>, you can give semantic names to regions, making the markup readable:</p> <pre><code class="language-css">.container { display: grid; grid-template-areas: "header header header" "nav main aside" "footer footer footer"; grid-template-columns: 1fr 3fr 1fr; grid-template-rows: auto 1fr auto; }header { grid-area: header; } nav { grid-area: nav; } main { grid-area: main; } aside { grid-area: aside; } footer { grid-area: footer; } </code></pre>
<p>Subgrid (available in modern browsers) allows a child grid to inherit its parent’s track definitions, perfect for nested components that need to stay aligned with the page grid:</p> <pre><code class="language-css">.card-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 2rem; }.card { display: grid; /* Subgrid context / grid-template-columns: subgrid; / Inherit columns from .card-list */ grid-template-rows: auto 1fr auto; } </code></pre>
<p>Here, each <code>.card</code> aligns its internal header, body, and footer with the surrounding layout, ensuring consistent gutters across the page.</p> <h3>Responsive Grid Layouts</h3> <p>Combine <code>repeat(auto-fit, minmax())</code> with media queries to create fluid, breakpoint‑free grids:</p> <pre><code class="language-css">.gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; } </code></pre> <p>The grid automatically adds as many 200‑pixel columns as possible, shrinking or expanding to fit the viewport. No explicit breakpoints are required, though you can still tweak <code>minmax</code> values for larger screens.</p>