Skip to main content
April 1, 2026Dan Rodney/10 min read

Flexbox: Sizing & Alignment

Master Flexbox sizing and alignment fundamentals

Flexbox Fundamentals You'll Master

Flex-Grow Control

Learn how to distribute available space proportionally between flex items using the flex-grow property.

Alignment Techniques

Master both container-wide and individual item alignment on main and cross axes.

Nested Flexbox

Understand how to create flexible layouts by nesting flex containers within flex items.

Topics Covered in This Web Development Tutorial:

Master advanced Flexbox techniques including precise size control with Flex-Grow, Flex-Shrink, & Flex-Basis, strategic alignment of all versus specific flex items, professional nesting patterns, and efficient Flex shorthand syntax.

Exercise Preview

preview flexbox sizing

Exercise Overview

This exercise will transform your understanding of flex item manipulation. You'll master the precise control of sizing and alignment for flex items (children of a flex container), learning techniques that professional developers rely on for responsive, maintainable layouts in modern web applications.

Getting Started

  1. In your code editor, close any files you may have open to maintain focus.
  2. For this exercise, navigate to the Flexbox Size Alignment folder located in Desktop > Class Files > yourname-Flexbox Grid Class. Consider opening the entire folder in your code editor (Visual Studio Code, Sublime Text, or similar) for efficient file management.
  3. Open index.html from the Flexbox Size Alignment folder.
  4. Preview index.html in a modern browser (Chrome, Firefox, Safari, or Edge).

    • This page builds upon the foundation you established in the previous exercise.
    • Observe how the colored navigation items don't utilize the full width of the navbar—a common layout challenge we'll solve.
    • Notice the red-outlined divs in the footer stack vertically due to their default block behavior. We'll transform these into a professional horizontal layout with precise sizing and alignment.
  5. Keep index.html open in your browser throughout this exercise. This live preview approach mirrors professional development workflows where immediate visual feedback accelerates learning and debugging.

Setup Process

1

Open Project Files

Navigate to Desktop > Class Files > yourname-Flexbox Grid Class and locate the Flexbox Size Alignment folder

2

Launch Files

Open index.html in your code editor and preview it in a browser to see the starting layout

3

Identify Issues

Notice the navbar items don't fill the width and footer divs are stacked instead of in a row

Controlling Size with Flex-Grow

The flex-grow property is your primary tool for distributing available space among flex items. Understanding its behavior is crucial for creating responsive layouts that adapt elegantly to different screen sizes. The default value of 0 means "don't grow," but positive numbers unlock powerful space distribution capabilities.

  1. Return to your code editor.
  2. Open main.css from the css folder within the Flexbox Size Alignment directory.
  3. In the .navbar li rule, add the following code to enable growth:

    .navbar li {
       flex-grow: 1;
       color: white;
       padding: 10px;
    }
  4. Save the file and reload your browser.

    • The navigation items now span the full navbar width—a significant improvement in visual balance.
    • Here's the key insight: Flexbox calculates remaining space after content sizing, then distributes that space proportionally based on flex-grow values. Since all items share the same flex-grow value (1), they receive equal portions of the available space.
  5. Switch back to your code editor.
  6. Navigate to index.html.
  7. Add a new navigation item to test Flexbox's adaptive behavior:

    <li class="hikes">Hikes & Excursions</li>
    <li class="about">About</li>
    <li class="contact">Contact</li>
  8. Save and reload to observe Flexbox's intelligent rebalancing.

    • The existing CSS automatically styles the new About item—demonstrating the power of well-structured stylesheets.
    • Notice how Flexbox seamlessly accommodates the additional item, redistributing space while maintaining visual harmony.
    • This adaptive behavior is why Flexbox has become the foundation for modern responsive design patterns.
  9. Return to your code editor.
  10. Switch to main.css.
  11. Let's explore proportional space distribution by modifying the .navbar .about rule:

    .navbar .about {
       flex-grow: 2;
       background: #0000d5;
    }
  12. Save and reload to see proportional growth in action.

    • The About element now receives twice the extra space of its siblings, creating visual emphasis through size.
    Flex-Grow Default Behavior

    The default value for flex-grow is 0, which means elements do not grow. Setting it to a positive number allows flex items to expand and fill available space.

    Flex-Grow Values Impact

    FeatureBefore (flex-grow: 0)After (flex-grow: 1)
    Space UsageItems use minimum spaceItems fill entire width
    DistributionLeftover space unusedSpace divided equally
    ResponsivenessFixed item sizesDynamic sizing
    Recommended: Use flex-grow: 1 for equal distribution, higher values for proportionally larger items

How Flex-Grow is Calculated

Flex-grow operates on a proportional system that distributes free space rather than setting absolute sizes. This mathematical approach ensures predictable, responsive behavior across all viewport sizes.

The calculation process: First, the browser determines each element's content size. Then it calculates available free space. Finally, it divides this free space by the sum of all flex-grow values, creating a "unit" of space. Elements receive space equal to their flex-grow value multiplied by this unit.

For example: With 500px of free space and total flex-grow values summing to 5, each unit equals 100px. An element with flex-grow: 1 receives 100px additional width, while flex-grow: 2 receives 200px.

For a comprehensive visual explanation, visit css-tricks.com/flex-grow-is-weird—an essential resource for mastering this concept.

  • Switch back to your code editor.
  • Remove the flex-grow: 2; from the .navbar .about rule to restore balanced distribution.
  • Now let's transform the footer layout. Convert the footer into a flex container:

    footer {
       display: flex;
       border: 8px solid gray;
       margin-top: 20px;
    }
  • Save and observe the immediate transformation.

    • The red-outlined divs now form a horizontal row—the fundamental change from block to flex layout.
    • Notice how the div widths collapse to their content size, leaving unused space on the right. This behavior reveals the difference between content-driven and space-filling layouts.
  • Return to your code editor.
  • Enable growth for all footer divisions:

    footer div {
       flex-grow: 1;
       border: 2px solid red;
       padding: 10px;
    }
  • Save and reload to see full-width utilization.

    • The footer now efficiently uses all available horizontal space, creating a more professional appearance.
    • However, uniform growth might not serve all content types equally well. Let's create more nuanced control.
  • Switch back to your code editor.
  • Override the growth behavior for the social media section:

    footer .social {
       flex-grow: 0;
    }
  • Save and reload to see selective sizing.

    • The social networks div now maintains its content-driven width while other sections expand.
    • This selective approach demonstrates how to balance content-appropriate sizing with space utilization.
  • Remove the flex-grow: 1; from the footer div rule for more precise control.
  • Rename and reconfigure the rule to target the copyright section specifically:

    footer .copyright {
       flex-grow: 1;
    }
  • Enhance the copyright section with centered text alignment:

    footer .copyright {
       flex-grow: 1;
       text-align: center;
    }
  • Save and reload to see the refined footer layout.

    • The address and social sections maintain content-appropriate widths, while the copyright expands to fill remaining space with centered text—a classic professional footer pattern.
  • Flex-Grow Calculation Example

    500px
    pixels of leftover space
    5
    total flex-grow value
    100px
    pixels per flex-grow unit
    Visual Learning Resource

    Visit css-tricks.com/flex-grow-is-weird to see excellent illustrations of how flex-grow calculations work in practice.

    Aligning All Vs. Specific Flex Items

    Flexbox provides dual alignment control: container-level properties that affect all items, and item-level properties for individual customization. This hierarchical approach enables both consistent layouts and strategic exceptions—essential for professional interface design.

    1. Return to your code editor.
    2. Implement vertical centering across all footer items. Since we're using the default row direction, the cross axis controls vertical alignment:

      footer {
         display: flex;
         align-items: center;
         border: 8px solid gray;
         margin-top: 20px;
      }
    3. Save and observe the vertical centering effect.

      • All footer sections now align to their vertical center, creating visual balance regardless of content height differences.
      • This container-level alignment provides consistent baseline behavior while maintaining flexibility for individual adjustments.
    4. Switch back to your code editor.
    5. Override the container alignment for the copyright section to demonstrate individual control:

      footer .copyright {
         flex-grow: 1;
         align-self: flex-end;
         text-align: center;
      }
    6. Save and reload to see the individual alignment override.

      • The copyright text now aligns to the footer's bottom edge, creating visual distinction while maintaining the centered horizontal alignment.

      NOTE: Understanding the distinction between container and item properties is fundamental to Flexbox mastery. Container properties (align-items, justify-content) establish default behavior, while item properties (align-self, flex-grow) enable precise individual control. CSS Tricks' Complete Guide to Flexbox at tinyurl.com/flexref remains the definitive reference, with clear property classifications and real-world examples.

    Container vs Individual Alignment

    Featurealign-items (container)align-self (individual)
    ScopeAll flex itemsSingle flex item
    Applied toFlex container (parent)Flex item (child)
    PriorityDefault alignmentOverrides container setting
    Recommended: Use align-items for consistent alignment, align-self for exceptions
    Property Application Context

    Remember that some flexbox properties apply to the flex container (parent), while others apply to flex items (children). CSS Trick's Complete Guide to Flexbox is the definitive reference.

    Nesting Flexbox

    Flexbox's true power emerges through nesting—using flex containers as flex items within larger flex containers. This pattern enables complex, responsive layouts while maintaining clean, semantic HTML structure.

    1. Return to your code editor.
    2. Remove the flex-grow: 1; from the .navbar li rule to reset navigation sizing.
    3. Add vertical centering to the navbar for improved visual alignment:

      .navbar {
         display: flex;
         align-items: center;
         border: 8px solid gray;

      Code Omitted To Save Space

      }

    4. Save and reload to observe the improved navbar alignment.

      • The logo and navigation items now align along their vertical centers, creating a more polished, professional appearance.
      • This foundation enables more sophisticated individual item positioning.
    5. Switch back to your code editor.
    6. Make the Sign Up button visually prominent by stretching it to full navbar height:

      .navbar .signup {
         align-self: stretch;
         background: #dc00d4;
         font-size: 1.5em;
      }
    7. Save and reload to see the height extension.

      • The Sign Up button now spans the full navbar height, creating strong visual emphasis.
      • However, the text alignment needs refinement—this is where nesting becomes essential.
    8. Switch back to your code editor.
    9. Transform the Sign Up item into a nested flex container for precise content control:

      .navbar .signup {
         align-self: stretch;
         display: flex;
         align-items: center;
         background: #dc00d4;
         font-size: 1.5em;
      }
    10. Save and reload to see nested flexbox in action.

      • The Sign Up text now centers perfectly within the full-height colored background—demonstrating how nesting solves complex alignment challenges elegantly.
    11. Return to your code editor.
    12. Add consistent spacing throughout the navigation:

      .navbar li {
         margin: 10px;
         color: white;
         padding: 10px;
      }
    13. Save and test the layout at various window sizes.

      • The added whitespace improves visual breathing room and touch target accessibility.
      • Notice how the logo scales down as its container shrinks, and the Sign Up text wraps at narrow widths—behaviors we'll refine next.

    Nesting Benefits

    Dual Role Elements

    Flex items can become flex containers themselves, allowing complex layout control.

    Stretch and Center

    Achieve effects like stretching to full height while centering content vertically.

    Creating Nested Flexbox

    1

    Make Item Stretch

    Use align-self: stretch on the flex item to fill container height

    2

    Convert to Container

    Add display: flex to the same element to make it a flex container

    3

    Align Contents

    Use align-items: center to vertically center content within the stretched item

    Controlling Size with Flex-Shrink

    While flex-grow manages space distribution, flex-shrink controls how elements respond to space constraints. In an era where responsive design spans from smartwatches to ultrawide monitors, mastering shrink behavior is crucial for maintaining layout integrity across all viewports.

    1. Return to your code editor.
    2. Prevent the Sign Up button from shrinking to maintain its prominence:

      .navbar .signup {
         align-self: stretch;
         flex-shrink: 0;
         display: flex;
         align-items: center;
         background: #dc00d4;
         font-size: 1.5em;
      }
    3. Apply the same protection to the logo for brand consistency:

      .navbar .logo {
         flex-shrink: 0;
         margin-right: auto;
         background: #0db8e8;
      }
    4. Save and test the responsive behavior by resizing your browser window.

      • The logo and Sign Up button now maintain their sizes, while other navigation items adapt to space constraints—a professional approach that preserves brand elements and key calls-to-action.
    Flex-Shrink Default

    The default flex-shrink value is 1, meaning items will shrink when space is limited. Set to 0 to prevent shrinking.

    Flex-Shrink: 0 Trade-offs

    Pros
    Prevents important elements from becoming too small
    Maintains consistent sizing for logos and buttons
    Ensures text doesn't break awkwardly on narrow screens
    Cons
    Can cause horizontal overflow on very narrow screens
    May push other elements to shrink more aggressively
    Requires careful consideration of mobile layouts

    Sizing with Flex-Basis

    The flex-basis property defines the initial main size of flex items before free space distribution. Think of it as the "preferred size" that serves as the starting point for grow and shrink calculations—essential for creating predictable, maintainable layouts.

    1. Return to your code editor.
    2. Set an optimal width for the logo container:

      .navbar .logo {
         flex-shrink: 0;
         flex-basis: 160px;
         margin-right: auto;
         background: #0db8e8;
      }

      NOTE: The provided CSS includes max-width: 100%; on images, ensuring the logo image scales proportionally as its container size changes. This responsive image behavior is crucial for maintaining visual quality across different layouts and prevents overflow issues.

    3. Save and observe the refined logo sizing.

      • The logo container now maintains an optimal width that balances brand visibility with efficient space utilization.

      NOTE: Flex-basis establishes the starting point before growth or shrinkage occurs. The final rendered size depends on available space and the flex-grow/flex-shrink values. For guaranteed sizing, combine flex-basis with flex-grow: 0 and flex-shrink: 0.

    Flex-Basis Behavior

    Flex-basis sets the initial size before growing or shrinking occurs. It doesn't guarantee final size unless flex-grow and flex-shrink are disabled.

    Flex Basis Vs. Width

    According to Mozilla's MDN documentation (tinyurl.com/fba-vs.-w): "When both flex-basis (other than auto) and width are specified, flex-basis takes priority." This hierarchy ensures consistent behavior within flex contexts.

    The strategic advantage of flex-basis over width lies in its axis awareness. Flex-basis automatically works with the main axis—functioning as width for flex-direction: row or height for flex-direction: column. This adaptability makes layouts more maintainable when direction changes occur, and enables the powerful flex shorthand syntax that combines all three sizing properties.

    Flex-Basis vs Width Property

    Featureflex-basiswidth
    PriorityHigher priority when both setLower priority
    Direction AwarenessWorks on main axis (row/column)Always horizontal
    FlexibilityAdapts to flex-directionFixed to width only
    Shorthand UsePart of flex shorthandSeparate property
    Recommended: Use flex-basis for flexible layouts, especially when using flex shorthand

    Flex Shorthand

    The flex shorthand property combines flex-grow, flex-shrink, and flex-basis into a single, efficient declaration. This consolidation not only reduces code volume but also encourages thinking about all three properties as an integrated sizing system.

    • The syntax follows the pattern: flex: flex-grow flex-shrink flex-basis;
    • The default value is flex: 0 1 auto; (don't grow, allow shrinking, size to content)
    1. Return to your code editor.

    2. Consolidate the logo's flex properties using the shorthand syntax:

      .navbar .logo {
         flex: 0 0 160px;
         margin-right: auto;
         background: #0db8e8;
      }
    3. Save and reload to confirm identical visual behavior.

      • The layout remains unchanged, but your CSS is now more concise and explicitly declares all sizing intentions in a single property.

    You've now mastered the core Flexbox sizing and alignment techniques that form the foundation of modern, responsive web layouts. These patterns—proportional growth, selective shrinking, strategic alignment, and efficient nesting—are the building blocks for creating professional interfaces that adapt seamlessly across all devices and screen sizes.

    Flex Shorthand Syntax

    0
    default flex-grow
    1
    default flex-shrink
    auto
    default flex-basis
    Code Efficiency

    Using flex shorthand combines flex-grow, flex-shrink, and flex-basis into one property: flex: 0 1 auto. This creates more concise and maintainable CSS.

    Key Takeaways

    1Flex-grow distributes leftover space proportionally, with 0 meaning no growth and higher numbers claiming more space
    2The browser calculates flex-grow by dividing available space by total flex-grow values across all items
    3Use align-items on containers for universal alignment and align-self on individual items for exceptions
    4Flex items can become flex containers themselves, enabling complex nested layouts like stretched elements with centered content
    5Flex-shrink controls how items reduce in size when space is limited, with 0 preventing shrinking entirely
    6Flex-basis sets initial size before growth or shrinkage and takes priority over width/height properties
    7Flex-basis works on the main axis, functioning as width for row direction and height for column direction
    8The flex shorthand property combines all three sizing properties into one declaration for cleaner, more maintainable code

    RELATED ARTICLES