Flexbox: Creating a Responsive Pricing Grid
Master responsive pricing layouts with flexbox techniques
Core Flexbox Concepts for Pricing Grids
Flexible Layouts
Create responsive column structures that adapt to different screen sizes. Flexbox automatically handles width distribution and alignment.
Nested Containers
Use multiple flex containers within each other to achieve complex layouts. Each level controls different aspects of positioning.
Visual Reordering
Change the visual order of elements without modifying HTML structure. Perfect for responsive design priorities.
Prerequisites for This Exercise
Visual Studio Code recommended for opening entire project folders
Required for inspecting flex containers and debugging layouts
Understanding of selectors, properties, and media queries
Knowledge of display flex, flex-direction, and basic flex properties
Project Setup Process
Open Project Files
Navigate to Desktop > Class Files > yourname-Flexbox Grid Class and open the Flexbox Pricing Grid folder in your code editor.
Load HTML File
Open hikes.html from the project folder and preview it in Firefox browser for live testing.
Access CSS File
Open main.css from the css folder where you'll write all the flexbox styling rules.
Test Responsiveness
Keep Firefox open and resize the window to see how the layout responds to different screen sizes.
Flex-basis is an ideal size, not guaranteed like width. Flexbox can shrink or grow items to fit the container, which is why 33.33% columns plus margins still fit on one line without wrapping.
Before vs After Flexbox Implementation
| Feature | Before Flexbox | After Flexbox |
|---|---|---|
| Layout Structure | Stacked sections | Three equal columns |
| Column Width | Full width | 33.33% each with flex-basis |
| Spacing Control | Default margins | Custom 30px between columns |
| Alignment | Left-aligned | Aligned with header |
CSS order property changes visual arrangement without modifying HTML. CSS selectors still reference the original code order, not the visual order.
Nested Flexbox Structure
Main Container
The main tag serves as the primary flex container, controlling the three column layout and overall spacing.
Section Elements
Each section becomes both a flex item within main and a flex container for its internal content.
Text Wrapper
The text-wrapper div grows to fill available space and becomes a flex container for content alignment.
Margins work differently on flex items - they stack instead of collapsing. This means both top and bottom margins are applied, creating more space between elements than in normal document flow.
Creating the Nested Structure
Make Sections Flex Containers
Add display: flex and flex-direction: column to sections so text-wrapper can grow vertically.
Grow Text Wrapper
Use flex-grow: 1 on text-wrapper to fill the available vertical space in each column.
Nest Another Flex Container
Make text-wrapper a flex container with column direction for internal content control.
Push Content to Bottom
Apply margin-top: auto to the price section to push it to the bottom of each column.
Layout Approach Comparison
| Feature | Bottom-Aligned Buttons | Vertically Centered |
|---|---|---|
| Complexity | Complex nesting required | Simple align-items property |
| Button Alignment | Buttons at same level | Natural content flow |
| Visual Focus | Emphasizes pricing action | Highlights center column |
| Code Maintenance | More CSS rules | Cleaner, fewer rules |
Key Takeaways
