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

Flexbox Wrapping: Free Web Development Tutorial

Master Responsive Navigation with CSS Flexbox Wrapping

Tutorial Prerequisites

This tutorial assumes you have basic knowledge of HTML and CSS. You'll need a code editor and Firefox browser for the DevTools exercises.

Topics Covered in This Web Development Tutorial:

Master Flex-Wrap and Sizing Flex Items (Flex-Grow & Flex-Basis) for responsive navigation design

Core Flexbox Concepts You'll Master

Flex-Wrap Property

Learn how to allow flex items to wrap onto multiple lines when they don't fit in the container. Essential for responsive design.

Flex-Grow & Flex-Basis

Master sizing flex items to fill available space and set their initial size before free space is distributed.

Responsive Navigation

Build a navigation that adapts from single-line desktop layout to multi-line mobile grid using media queries.

Exercise Preview

preview flex wrap

Exercise Overview

In this hands-on exercise, you'll master one of the most powerful aspects of Flexbox: creating responsive layouts that adapt gracefully across screen sizes. You'll learn how to make flex items wrap onto multiple lines intelligently, then size them proportionally using flex-grow and flex-basis properties. By the end, you'll have built a professional navigation component that transforms seamlessly from desktop to mobile—a critical skill for modern web development.

Getting Started

  1. In your code editor, close any files you may have open to start with a clean workspace.
  2. For this exercise we'll be working with the Flexbox Wrapping folder located in Desktop > Class Files > yourname-Flexbox Grid Class. If you're using a modern editor like Visual Studio Code, Sublime Text, or WebStorm, open the entire folder to take advantage of your editor's file tree navigation.
  3. Open index.html from the Flexbox Wrapping folder.
  4. Preview index.html in Firefox (we'll be leveraging its superior DevTools for this exercise).

    • This page builds upon previous exercises, but this time we're implementing a more sophisticated navigation pattern that responds dynamically to viewport changes.
  5. Keep index.html open in Firefox as you work—you'll be refreshing frequently to see your changes take effect in real-time.

Setup Your Development Environment

1

Open Project Files

Navigate to Desktop > Class Files > yourname-Flexbox Grid Class > Flexbox Wrapping folder and open it in your code editor

2

Launch Preview

Open index.html in Firefox browser to preview your work and access DevTools for responsive testing

3

Prepare Workflow

Keep both your code editor and Firefox open side-by-side for efficient development and testing

Setting Display Flex & Wrapping

Now we'll transform a basic navigation into a flexible layout system. First, we'll establish the flex container, then add intelligent wrapping behavior.

  1. Switch back to your code editor.
  2. Open main.css from the css folder (in the Flexbox Wrapping folder).
  3. In the .navbar rule add the following bold code:

    .navbar {
       display: flex;
       border: 8px solid gray;

    Code Omitted To Save Space

    }

  4. Save the file, and reload the page in Firefox.

    • Notice how the navigation items now display as a horizontal row instead of stacking vertically—this is the default flex behavior in action.
  5. Ctrl–click (Mac) or Right–click (Windows) anywhere on the page and choose Inspect Element.
  6. If the DevTools are not docked to the right side of the window, click the firefox devtools menu button at the top right of the DevTools panel and choose Dock to Right.

    firefox devtools dock right

    This configuration allows you to resize the webpage area more precisely than you could with DevTools closed, simulating various device widths effectively.

  7. Gradually resize the webpage area narrower until you observe how the navigation items stubbornly remain on one line, even when it forces horizontal scrolling—this demonstrates the default flex behavior of keeping items on a single line.
  8. Keep the DevTools open for the next steps.
  9. Return to your code editor.
  10. In the .navbar rule add the following bold code:

    .navbar {
       display: flex;
       flex-wrap: wrap;
       border: 8px solid gray;

    Code Omitted To Save Space

    }

  11. Save the file, and reload the page in Firefox.

    • Now resize the webpage area and observe how the items intelligently wrap to new lines when space becomes constrained—this is responsive design in its purest form.
DevTools Pro Tip

Dock DevTools to the right side of Firefox to simulate narrow screen widths more effectively. This allows you to test responsive behavior without making your entire browser window tiny.

Before vs After Adding Flex-Wrap

FeatureWithout Flex-WrapWith Flex-Wrap
Overflow BehaviorItems stay on one line, causing horizontal scrollItems wrap to new lines when space is limited
User ExperiencePoor - requires horizontal scrollingGood - content remains accessible
Responsive DesignBreaks on small screensAdapts to screen size
Recommended: Always use flex-wrap: wrap for responsive navigation components

Sizing the Flex Items

With wrapping established, we'll now implement sophisticated sizing controls that create a polished, professional layout across all screen sizes.

  1. Return to your code editor.
  2. Above the min-width: 571px media query, add the following new media query for smaller screens:

    @media (max-width: 570px) {
       .navbar .logo {
          flex-basis: 100%;
       }
    }
  3. Save the file, and reload the page in Firefox.

    • The logo now claims the full width on small screens, forcing other navigation items to wrap below—a common pattern in mobile-first design.
    • Notice the remaining items don't expand to fill available space, leaving awkward white space. Let's fix this with flex-grow.
  4. Return to your code editor.
  5. In the max-width: 570px media query, below the .navbar .logo rule, add the following new rule:

    @media (max-width: 570px) {
       .navbar .logo {
          flex-basis: 100%;
       }
       .navbar li {
          flex-grow: 1;
       }
    }
  6. Save the file, and reload the page in Firefox.

    • The navigation items now expand proportionally to fill available space. Next, we'll center the text for better visual balance.
  7. Return to your code editor.
  8. In the .navbar li rule add the following bold code:

    @media (max-width: 570px) {
       .navbar .logo {
          flex-basis: 100%;
       }
       .navbar li {
          flex-grow: 1;
          text-align: center;
       }
    }
  9. Save the file, and reload the page in Firefox.

    • The centered text creates better visual hierarchy and balance.
    • Resize the viewport until the 4 navigation links form a 2 × 2 grid. Notice how the widths don't quite align across rows—we'll fix this with explicit sizing.
  10. Return to your code editor.
  11. In the max-width: 570px media query's .navbar li rule, add the following bold code:

    .navbar li {
       flex-grow: 1;
       flex-basis: 50%;
       text-align: center;
    }
  12. Save the file, and reload the page in Firefox.

    • Perfect! The 2 × 2 grid now has consistent widths across both rows, creating visual harmony.
    • Close the DevTools and expand the browser window to full width.
    • On larger screens, notice all the extra space clusters on the right side. For better UX, we want the logo anchored left and navigation items grouped right.
  13. Return to your code editor.
  14. Before adjusting spacing, let's optimize our code using the flex shorthand property—a best practice for maintainable CSS.

    In the max-width: 570px media query's .navbar li rule, replace the two flex properties with the shorthand version as shown below in bold:

    .navbar li {
       flex: 1 0 50%;
       text-align: center;
    }

    NOTE: Remember the syntax is flex: flex-grow flex-shrink flex-basis; — this single property combines three flex behaviors into one concise declaration.

Progressive Enhancement Steps

Step 1

Logo Full Width

Set flex-basis: 100% on logo for screens under 570px wide

Step 2

Items Grow to Fill Space

Add flex-grow: 1 to navigation items to eliminate white space

Step 3

Center Text Content

Apply text-align: center for better visual balance

Step 4

Create Even Grid

Set flex-basis: 50% for consistent 2x2 grid layout

Flex Shorthand Syntax

The flex shorthand property combines flex-grow, flex-shrink, and flex-basis into one declaration: flex: 1 0 50% means grow factor 1, shrink factor 0, and basis 50%.

Adjusting the Layout on Larger Screens

Now we'll implement the finishing touch: creating that classic logo-left, navigation-right layout that's standard in professional web design.

  1. In the min-width: 571px media query, above the footer rule add the following new rule:

    .navbar .logo {
       margin-right: auto;
    }
  2. Save the file, and reload the page in Firefox.

    • Excellent! The navigation now displays the professional standard: logo anchored to the left, navigation items grouped to the right, with flex automatically handling the spacing distribution.
margin-right: auto
This CSS property is the key to pushing navigation links to the right side while keeping the logo on the left. The auto margin consumes all available space between the logo and nav items.

Optional Bonus: Refining the Layout at Another Screen Size

The 2 × 2 grid works well on very small screens, but there's an intermediate screen size where all four navigation items could fit comfortably on a single row below the logo. Let's add this refinement for a truly polished responsive experience.

  1. Above the min-width: 571px media query, add the following media query and rule:

    @media (min-width: 415px) and (max-width: 570px) {
       .navbar li {
          flex-basis: 25%;
       }
    }
  2. Save the file, and reload the page in Firefox.

    • Test your responsive navigation by slowly resizing the browser from narrow to wide. You should see three distinct layouts: a 2 × 2 grid on small screens, transitioning to a single row of navigation items below the logo, and finally the professional logo-left/navigation-right layout on desktop. This progressive enhancement creates an optimal user experience across all device categories.

Navigation Layout Breakpoints

Small Screens (< 415px)
25
Medium Screens (415px - 570px)
40
Large Screens (> 570px)
35

Final Layout Verification

0/4

Key Takeaways

1Flex-wrap property enables responsive design by allowing flex items to wrap onto multiple lines when container width is insufficient
2Combining flex-grow and flex-basis properties gives precise control over how flex items size themselves within available space
3Media queries with different flex-basis values create adaptive layouts that work across all screen sizes
4The margin-right: auto technique effectively separates flex items into left and right groups within a flex container
5Firefox DevTools docked to the right side provides an efficient workflow for testing responsive flexbox layouts
6Flex shorthand syntax (flex: grow shrink basis) creates cleaner, more maintainable CSS code
7Progressive enhancement approach starts with basic layout and adds complexity through targeted media queries
8Text alignment within flex items requires separate text-align properties beyond the flexbox layout properties

RELATED ARTICLES