Courses/HTML & CSS/Flexbox Advanced Patterns

    CSS Flexbox: Advanced Patterns & Real Layouts

    Lesson 21 โ€ข Advanced Track

    What You'll Learn

    Build a sticky footer layout with Flexbox
    Create the Holy Grail layout (header, sidebar, main, footer)
    Responsive card grids using flex-wrap and flex-basis
    The flex shorthand: flex-grow, flex-shrink, flex-basis
    Auto margins for powerful alignment tricks
    Nested flex containers for complex UIs

    ๐Ÿ’ก Think of It Like This

    flex-grow is like appetite. If three children are at a buffet and one has flex-grow: 2, it takes twice as much of the leftover food (space) as its siblings with flex-grow: 1.

    PropertyDefaultWhat It Controls
    flex-grow0How much extra space to absorb
    flex-shrink1How much to shrink when space is tight
    flex-basisautoStarting size before grow/shrink
    flex0 1 autoShorthand: grow shrink basis

    Sticky Footer with Flexbox

    The sticky footer stays at the bottom of the viewport even when content is short, and pushes down naturally when content is long.

    Holy Grail Layout

    The classic header + sidebar + main + sidebar + footer layout, built entirely with Flexbox.

    Holy Grail Layout

    Header, left sidebar, main content, right sidebar, footer

    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>Holy Grail</title>
      <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
          background: #0f172a;
          font-family: system-ui, sans-serif;
          color: #e5e7eb;
          display: flex;
          flex-direction: column;
          min-height: 100vh;
        }
    
        header, footer {
          background: #1e293b;
          padding: 15px 20px;
          te
    ...

    Responsive Card Grid with Flex-Wrap

    Create a card grid that adapts to any screen size using flex-wrap and flex-basis.

    Flex-Wrap Card Grid

    Cards wrap to new rows based on available space

    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>Card Grid</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; }
    
        .card-grid {
          display: flex;
          flex-wrap: wrap;
          gap: 20px;
        }
    
        .card {
          /* flex: 1 1 2
    ...

    โš ๏ธ Common Mistakes

    • Using flex: 1 without understanding it โ€” flex: 1 means flex: 1 1 0% (grow, shrink, 0 basis). For cards, use flex: 1 1 280px to set a minimum width.
    • Forgetting min-width: 0 on flex children โ€” Flex items won't shrink below their content width by default. Add min-width: 0 to allow text truncation.
    • Not using gap โ€” Using margins for spacing between flex items is fragile. Use gap instead.
    • Overusing Flexbox when Grid is better โ€” For 2D layouts (rows AND columns), CSS Grid is more appropriate.

    ๐ŸŽ‰ Lesson Complete

    You've mastered advanced Flexbox patterns:

    • โœ… Sticky footer with flex: 1 on main content
    • โœ… Holy Grail layout with header, sidebars, main, and footer
    • โœ… Responsive card grids with flex-wrap and flex-basis
    • โœ… The flex shorthand: grow, shrink, basis

    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