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.
Setup Process
Open Project Files
Navigate to Desktop > Class Files > yourname-Flexbox Grid Class and locate the Flexbox Size Alignment folder
Launch Files
Open index.html in your code editor and preview it in a browser to see the starting layout
Identify Issues
Notice the navbar items don't fill the width and footer divs are stacked instead of in a row
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.
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.
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.
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
Visit css-tricks.com/flex-grow-is-weird to see excellent illustrations of how flex-grow calculations work in practice.
Container vs Individual Alignment
| Feature | align-items (container) | align-self (individual) |
|---|---|---|
| Scope | All flex items | Single flex item |
| Applied to | Flex container (parent) | Flex item (child) |
| Priority | Default alignment | Overrides container setting |
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 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
Make Item Stretch
Use align-self: stretch on the flex item to fill container height
Convert to Container
Add display: flex to the same element to make it a flex container
Align Contents
Use align-items: center to vertically center content within the stretched item
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
Flex-Basis vs Width Property
| Feature | flex-basis | width |
|---|---|---|
| Priority | Higher priority when both set | Lower priority |
| Direction Awareness | Works on main axis (row/column) | Always horizontal |
| Flexibility | Adapts to flex-direction | Fixed to width only |
| Shorthand Use | Part of flex shorthand | Separate property |
Flex Shorthand Syntax
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
