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

Intro to Flexbox: Free Web Development Tutorial

Master CSS Flexbox Layout and Alignment Fundamentals

What You'll Master in This Tutorial

Display Flex Properties

Learn how to transform static layouts into dynamic flex containers with display properties.

Axis Alignment Control

Master justify-content and align-items for precise positioning on main and cross axes.

Flexible Directions

Switch between row and column layouts while understanding how axes change behavior.

Topics Covered in This Web Development Tutorial:

CSS Flexbox fundamentals including Display Flex, strategic alignment and distribution across main and cross axes, Flex Direction properties (Row & Column layouts), and leveraging auto margins for sophisticated positioning

Exercise Preview

preview flexbox intro

Exercise Overview

In this hands-on exercise, you'll master the fundamentals of CSS flexbox—one of the most powerful layout systems in modern web development. Flexbox revolutionizes how we approach content alignment and distribution, offering intuitive solutions to layout challenges that once required complex workarounds. By the end of this tutorial, you'll understand why flexbox has become the go-to choice for responsive layouts and component design.

Why Firefox for This Tutorial

Firefox provides superior DevTools for inspecting flexbox layouts, making it easier to visualize how flex properties affect your elements in real-time.

Getting Started

  1. Launch your code editor (Visual Studio Code, Sublime Text, or your preferred IDE). If you're following along in a Noble Desktop class, we'll be using Visual Studio Code for its excellent CSS debugging capabilities.
  2. Navigate to the Flexbox Intro folder located at Desktop > Class Files > yourname-Flexbox Grid Class > Flexbox Intro. For optimal workflow, open this entire folder in your code editor—this enables better file navigation and IntelliSense features.
  3. Open index.html from the Flexbox Intro folder and examine its structure.
  4. Analyze the HTML architecture:

    • A ul element serving as our navigation navbar
    • A simple text element for content demonstration
    • A footer containing three nested divs: address, copyright, and social links
    • Intentionally minimal content to maintain focus on flexbox mechanics rather than complex layouts
    • The linked main.css file in the head includes foundational styling to enhance visual comprehension of our flexbox demonstrations
  5. Preview index.html in Firefox. We're using Firefox specifically because its Developer Tools offer superior flexbox inspection capabilities compared to other browsers—a crucial advantage when debugging complex layouts.
  6. Observe the current layout characteristics:

    • The navbar ul and footer display gray borders that clearly delineate their boundaries
    • Each navigation list item features a distinct background color, making individual element sizing immediately apparent
    • The three footer divs are outlined in red to visualize their default block behavior
  7. Keep index.html open in Firefox throughout this exercise—you'll be refreshing frequently to observe the real-time impact of your CSS modifications.

Setup Your Development Environment

1

Launch Code Editor

Open Visual Studio Code, Sublime Text, or your preferred editor to access the project files.

2

Navigate to Project

Open the Flexbox Intro folder from Desktop > Class Files > yourname-Flexbox Grid Class.

3

Preview in Firefox

Open index.html in Firefox to see the initial layout with gray borders and colored elements for visual reference.

Display Flex: Transforming Layout Fundamentals

The CSS display property extends beyond traditional values like block, inline, and inline-block. The flex value represents a paradigm shift in layout methodology, providing unprecedented control over element positioning and sizing.

  1. Return to your code editor and open main.css from the css folder.
  2. Locate the .navbar rule and add the fundamental flexbox property:
  3. Add display: flex; to the .navbar rule as highlighted:

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

    Code Omitted To Save Space

    }

  4. Save and refresh Firefox to witness the transformation:

    • The navigation transforms from vertical stack to horizontal layout instantly
    • List item widths collapse to accommodate their content rather than spanning full width
    • The gray border maintains full page width, confirming the container itself hasn't collapsed—only its child behavior has changed

    This single declaration establishes a flex container, automatically converting all direct children into flex items with new layout behaviors.

  5. Right-click (or Ctrl+click on Mac) within the navbar and select Inspect Element to access Firefox's superior flexbox debugging tools.
  6. In DevTools, locate and click the <ul class="navbar"> element to reveal its flex container properties.
  7. The DevTools Flex Container panel displays a comprehensive list of Flex Items, providing real-time insight into your flexbox hierarchy.
  8. In the styles panel, locate the .navbar ruleset and click on the flex value to edit it.
  9. Change flex to inline-flex and observe the immediate layout shift.
  10. Expand your browser window to see how inline-flex affects container behavior—the gray border now wraps content width, allowing the Come Explore Tahoe! text to flow alongside the navbar.

    This demonstrates a key distinction: flex containers behave like block elements (full width), while inline-flex containers size to their content like inline elements. Understanding this difference is crucial for responsive design decisions.

  11. Refresh the page to restore the original flex value and continue.

Flex vs Inline-Flex Behavior

Featuredisplay: flexdisplay: inline-flex
Container WidthFull parent widthContent width only
PositioningBlock-level elementInline-level element
Use CaseMain layout containersInline flex components
Recommended: Use flex for primary layout containers, inline-flex for components within text flow.
Flex Container Terminology

When you set display: flex on an element, it becomes a flex container and its direct children automatically become flex items. This parent-child relationship is fundamental to flexbox behavior.

Mastering Alignment & Distribution: Main Axis vs Cross Axis

Flexbox's true power emerges through its sophisticated alignment system. Rather than thinking in terms of horizontal and vertical, flexbox uses main axis and cross axis concepts that adapt based on flex direction. This flexible framework enables consistent alignment logic regardless of layout orientation.

main axis vs. cross axis

  1. In main.css, enhance the .navbar rule with main axis control:
  2. Add the justify-content property as shown:

    .navbar {
       display: flex;
       justify-content: center;
       border: 8px solid gray;

    Code Omitted To Save Space

    }

  3. Save and refresh to see your navigation items elegantly center along the main axis (horizontally in this row layout).

  4. Open DevTools again, inspect the <ul class="navbar"> element, and locate the justify-content property in the styles panel.
  5. Click on center to edit it, then experiment with these essential values:
  6. Test each justify-content value systematically:

    • flex-start (default): Items align to the beginning of the main axis
    • flex-end: Items align to the end of the main axis
    • center: Items center along the main axis
    • space-between: Items distribute with equal space between them
    • space-around: Items distribute with equal space around each item
    • space-evenly: Items distribute with perfectly equal spacing

    For detailed visual examples of each property, reference the comprehensive guide at tinyurl.com/csstricks-justify-content

  7. Return to main.css and add cross-axis control with align-items:
  8. Expand the .navbar rule:

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

    Code Omitted To Save Space

    }

  9. Save and refresh—notice how items now center perfectly along both axes, creating a polished, professional appearance.

  10. In DevTools, experiment with align-items values to understand cross-axis behavior:
  11. Click the align-items: center value and test these options:
  12. Explore each align-items value:

    • stretch (default): Items expand to fill the cross axis
    • flex-start: Items align to the cross axis start
    • flex-end: Items align to the cross axis end
    • center: Items center on the cross axis
    • baseline: Items align along their text baseline

    For comprehensive visual documentation, visit tinyurl.com/csstricks-align-items

Professional Memory Techniques: Justify-Content vs Align-Items

Veteran developers use these proven mnemonics to distinguish these critical properties:

  • justify-content: Longer word = Main axis control
    (horizontal for row direction, vertical for column direction)
  • align-items: Shorter word = Cross axis control
    (vertical for row direction, horizontal for column direction)

Developer Marcus Herrmann offers an entertaining alternative: "Michael Jackson = MJ = Main Axis → justify-content"—whatever works for your mental model!

Flex Direction: Row & Column Layouts

While flexbox defaults to row direction, mastering column layouts unlocks powerful vertical design patterns. Understanding how axis behavior flips with direction changes is fundamental to responsive design expertise.

  1. Return to your code editor and modify the flex direction:
  2. Add flex-direction to transform your layout:

    .navbar {
       display: flex;
       flex-direction: column;
       justify-content: center;
       align-items: center;

    Code Omitted To Save Space

    }

  3. Save and refresh to observe the dramatic transformation:

    • Navigation reverts to vertical column layout
    • align-items: center now controls horizontal positioning (cross axis)
    • justify-content: center affects vertical positioning, but requires sufficient height to be visible
  4. To demonstrate main axis distribution in column layout, we need adequate container height:
  5. Add viewport-based height sizing:

    .navbar {
       display: flex;
       flex-direction: column;
       min-height: 100vh;
       justify-content: center;
       align-items: center;

    Code Omitted To Save Space

    }

    Note: Viewport units (vh, vw) provide responsive sizing relative to browser window dimensions. 100vh equals 100% of viewport height—more reliable than percentage-based height calculations.

  6. Save and refresh to see a stunning full-screen centered layout achieved with minimal CSS—showcasing flexbox's elegant simplicity.

  7. Use DevTools to experiment with different justify-content and align-items combinations in this column layout:
  8. Inspect the navbar and systematically test property combinations:
  9. Reference these complete property value sets:

    Justify-Content Align-Items
    flex-start (default) stretch (default)
    flex-end flex-start
    center flex-end
    space-between center
    space-around baseline
    space-evenly

    Advanced tip: flex-direction also accepts row-reverse and column-reverse values for content reordering without HTML modifications—invaluable for responsive design scenarios.

Row vs Column Direction Impact

Featureflex-direction: rowflex-direction: column
Main AxisHorizontalVertical
Cross AxisVerticalHorizontal
justify-content ControlsHorizontal alignmentVertical alignment
align-items ControlsVertical alignmentHorizontal alignment
Recommended: Remember that axes switch when direction changes - this affects how justify-content and align-items behave.
Viewport Units for Height

Use 100vh instead of 100% for full viewport height. Viewport units (vh, vw) are relative to the visible portion of the page, making them more reliable for layout calculations.

Leveraging Auto Margins: Advanced Positioning Techniques

Auto margins in flexbox contexts unlock sophisticated positioning capabilities that often surpass what's achievable with justify-content alone. This technique is particularly valuable for navigation layouts and component positioning.

  1. Reset your navbar to demonstrate auto margin techniques:
  2. Simplify the .navbar rule to baseline flex behavior:

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

    Code Omitted To Save Space

    }

  3. Target the signup button with strategic margin application:

    .navbar .signup {
       margin-left: auto;
       background: #dc00d4;
       font-size: 1.5em;
    }
  4. Save and refresh to witness the signup button's migration to the far right—auto margins consume all available space, creating perfect separation.

  5. Now apply auto margins to the logo for sophisticated layout control:
  6. Enhance the logo styling:

    .navbar .logo {
       margin-right: auto;
       background: #0db8e8;
    }
  7. Save and refresh to see professional navigation layout emerge: logo anchored left, signup button anchored right, with remaining items elegantly centered in the available space.

  8. Experiment with removing the signup margin to understand the cascading effects:
  9. Delete the margin-left: auto; from .navbar .signup and observe how all items following the logo shift to the container's right edge.
  10. Save and refresh to see this alternative layout pattern—useful for different navigation design requirements.

Key Takeaways

1Setting display: flex transforms an element into a flex container, automatically making its direct children flex items with horizontal layout by default.
2justify-content controls alignment along the main axis, while align-items controls alignment along the cross axis, with their effects changing based on flex-direction.
3The main axis runs horizontally for row direction and vertically for column direction, with the cross axis always perpendicular to the main axis.
4Firefox DevTools provide superior flexbox inspection capabilities, allowing real-time experimentation with flex property values.
5Auto margins on flex items consume available space and push items to container edges, offering more granular control than container-level alignment properties.
6flex-direction: column switches the axes, making justify-content control vertical alignment and align-items control horizontal alignment.
7Viewport units (vh, vw) work better than percentages for full-height layouts, with 100vh representing the full visible height of the browser window.
8Understanding the flex container and flex item relationship is fundamental to mastering flexbox layouts and troubleshooting alignment issues.

RELATED ARTICLES