CSS Grid Level 2: Subgrid & Masonry

    Lesson 22 โ€ข Advanced Track

    What You'll Learn

    Named grid template areas for readable layouts
    Subgrid: inherit parent grid tracks in child elements
    Masonry-style layouts with CSS Grid
    Auto-fill vs auto-fit for responsive grids
    Grid placement shortcuts: span, line names, areas
    Complex dashboard layouts with nested grids

    ๐Ÿ’ก Think of It Like This

    Grid template areas are like drawing a floor plan. You name each room (area) and arrange them visually in your CSS. Subgrid is like a room that inherits the building's structural columns โ€” child elements align to the parent grid's tracks automatically.

    FeatureBrowser SupportUse Case
    grid-template-areasโœ… All browsersVisual page layouts
    subgridโœ… Firefox, Chrome 117+, Safari 16+Aligned card content
    auto-fillโœ… All browsersFill available space with tracks
    auto-fitโœ… All browsersLike auto-fill but collapses empty tracks

    Named Grid Template Areas

    Draw your layout visually in CSS using named areas โ€” the most readable way to build complex layouts.

    Grid Template Areas Dashboard

    A dashboard layout using named grid areas

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Grid Template Areas</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; }
    
        .dashboard {
          display: grid;
          grid-template-areas:
            "header header header"
            "nav    main   aside"
            "nav    footer footer";
          grid-te
    ...

    auto-fill vs auto-fit

    Both create responsive grids without media queries, but they handle empty space differently.

    auto-fill vs auto-fit

    See the difference when there are fewer items than columns

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>auto-fill vs auto-fit</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 25px; }
        h3 { margin: 25px 0 10px; }
    
        .auto-fill {
          display: grid;
          grid-template-columns: repeat
    ...

    Masonry-Style Layout

    Create a Pinterest-style masonry layout where items of different heights pack tightly together.

    CSS Masonry Layout

    Items of varying height pack together beautifully

    Try it Yourself ยป
    Code Preview
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Masonry Layout</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { background: #0f172a; font-family: system-ui, sans-serif; color: #e5e7eb; padding: 30px; }
    
        h1 { color: #3b82f6; margin-bottom: 20px; }
    
        /* CSS Columns approach (widely supported) */
        .masonry {
          columns: 3;
          column-gap: 15px;
        }
    ...

    โš ๏ธ Common Mistakes

    • Confusing auto-fill and auto-fit โ€” auto-fill creates empty tracks; auto-fit collapses them. When you have few items, auto-fit makes them stretch to fill the row.
    • Expecting subgrid to work everywhere โ€” Check browser support. Subgrid is well-supported now but not in older browsers.
    • Using area names with spaces/special chars โ€” Grid area names must be valid CSS identifiers (no spaces, hyphens are okay).
    • Forgetting break-inside: avoid for masonry โ€” Without it, items can be split across columns.

    ๐ŸŽ‰ Lesson Complete

    You've mastered CSS Grid Level 2 features:

    • โœ… Named template areas for visual, readable layouts
    • โœ… auto-fill vs auto-fit for responsive grids without media queries
    • โœ… Masonry layouts using CSS columns with break-inside: avoid
    • โœ… Subgrid for aligned child elements within nested grids

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service