Controlling the flow of flex items with row and column layouts
(struggling to place sticky notes) For the LAST TIME, these navigation buttons should go side by side! HORIZONTALLY! That's how menus are SUPPOSED to work! (attempts to arrange them in a row but they fall off the phone mockup)
(timidly) But Trashy, it's a mobile design... there's not enough space for five buttons in a row. They'll be too small to tap.
(dramatically) NONSENSE! I refuse to be constrained by your so-called "screen dimensions"! I'm an ARTIST! A VISIONARY! A—
(Sticky notes fall to the floor again)
(glaring at sticky notes) BETRAYED by adhesive technology once again!
(entering with coffee) What's with the neck brace?
(adjusts fake neck brace) I have a very serious condition called Flex-Direction-Dysfunction. Or FDD. I'm physically incapable of arranging things in columns. My doctor says it's terminal. (dramatic sigh) Everything I create can only flow in rows... from left to right... forever... like my tragic existence.
(unimpressed) That's not a real condition.
It absolutely is! Just like Vertical Alignment Deficiency and Float Clearance Syndrome! Very serious CSS afflictions!
(strolling in, carrying a phone sideways) Hey, is anyone else's phone stuck in landscape mode? I can't get mine to... (turns phone, screen rotates) ...wait, nevermind.
(pointing to the mobile mockup) Look, the issue is simple. On desktop screens, we have plenty of horizontal space, so row layouts make sense. On mobile devices, we have limited width but plenty of height, so column layouts are often better. That's why flex-direction exists – to let us change directions based on the device.
(dramatically removes neck brace) LIES! Next you'll tell me we should put doors on the ceiling and drive cars sideways! MADNESS!
(stomping in) WHO'S responsible for this MOBILE NIGHTMARE?! (waves phone around) This UI is a SECURITY CATASTROPHE! The buttons are so TINY only infant fingers or highly trained government assassins could press them accurately!
That's what I was trying to tell Trashy! They need to be in a column for mobile.
(suddenly concerned) But what about tablet users?! Or smartwatch users?! OR REFRIGERATOR BROWSER USERS?! Have you considered the ENTIRE SPECTRUM of potential devices? What about SMART TOASTERS?!
(sighs) That's exactly why we need to learn proper flex direction. One layout won't fit all devices.
(thoughtfully) It's like a buffet, really. On a small table, you arrange the dishes in a line. On a big table, you can spread them out side-by-side. But either way, you still get to eat all the delicious... (notices everyone staring) What? Food analogies are my thing.
(dramatically spinning to face the wall) I REFUSE to accept vertical navigation! It's against the natural order! Humans read horizontally! Rivers flow horizontally! Horizon is HORIZONTAL! It's in the NAME!
Actually, Ancient Chinese and Japanese text was written vertically. Many languages can be read in different directions.
(gasps, clutches chest) My entire worldview... SHATTERED!
(entering, carrying multiple devices) The client just sent over device testing results. Our site is completely unusable on mobile. The navigation buttons are too small, and users have to zoom in just to click them.
(proudly) I call that feature "Precision Targeting Mode!" It keeps out the weak-fingered undesirables!
(ignoring him) We need a complete overhaul of the navigation system. It needs to display as a column on mobile and a row on desktop.
That sounds like we need flex-direction.
(without looking up from phone) Display flex. Flex-direction: column for mobile. Media query for larger screens. Flex-direction: row. Done. (continues scrolling on phone)
(to students) What Garbage means is that we can use the flex-direction property to easily switch between row and column layouts based on screen size.
(dramatically falling to knees) TWO LINES OF CSS? That's all it takes to solve this COSMIC DIRECTIONAL DILEMMA? Impossible!
(nods) It's like being able to rearrange a buffet table instantly when more guests arrive. (makes chef's kiss gesture) Beautiful.
Flex direction is a core property of CSS Flexbox that determines the primary axis along which flex items are placed in the flex container.
When we use display: flex, items are laid out along a line called the "main axis." By default, this axis runs horizontally from left to right, creating a row of items. However, with flex-direction, we can change the direction of this main axis.
(finally looking up) It's just direction. Not cosmic. Not life-changing. Just tells elements which way to flow. (demonstrates with hand motion) Row: side-by-side. Column: stacked.
The flex-direction property accepts four possible values:
| Value | Description | Layout |
|---|---|---|
row |
Default value. Items flow horizontally from left to right. | →→→ |
row-reverse |
Items flow horizontally from right to left. | ←←← |
column |
Items flow vertically from top to bottom. | ↓ ↓ ↓ |
column-reverse |
Items flow vertically from bottom to top. | ↑ ↑ ↑ |
/* Basic flex container with row direction (default) */ .container { display: flex; flex-direction: row; /* This is the default value */ } /* Container with column direction */ .container { display: flex; flex-direction: column; }
(excitedly) And we can use it to make our layouts responsive to different screen sizes!
💡 Pro Tip:
Always remember that when you change flex-direction from row to column, the main axis changes too. This means other flex properties like justify-content and align-items will work along different axes!
To better understand the difference between row and column directions, let's look at a visual comparison:
display: flex; flex-direction: row;
display: flex; flex-direction: column;
(getting up, suddenly excited) Wait! If we can change DIRECTIONS... CAN WE DEFY GRAVITY TOO? (starts trying to place sticky notes on the ceiling)
display: flex; flex-direction: row;
Mobile app interfaces typically have different layout requirements compared to desktop interfaces. With limited screen width but more vertical space, mobile apps often stack elements vertically (column direction) rather than horizontally (row direction).
The key to mobile layouts is thinking vertically. Navigation bars, content sections, and interactive elements often need to stack rather than sit side by side.
Main content goes here...
.nav-container { display: flex; flex-direction: column; width: 100%; }
Card content 1
Card content 2
Card content 3
.card-container { display: flex; flex-direction: column; gap: 10px; }
⚠️ Important:
When designing mobile layouts, be mindful of touch target sizes. The recommended minimum size for touch targets is 44×44 pixels, according to Apple's Human Interface Guidelines. This is why elements often need more vertical space in mobile interfaces.
SECURITY REMINDER! Mobile layouts with proper sizing reduce the risk of MISCLICKS, ACCIDENTAL PURCHASES, and UNAUTHORIZED ACCESS! Touch targets that are too small increase the chances of user error by 76.4%!
One of the most powerful uses of flex-direction is switching between row and column layouts based on screen size. This allows us to optimize our layouts for both desktop and mobile viewing.
(produces tablet from nowhere, shows two versions of the same layout) Row for wide screens. Column for narrow screens.
/* Mobile-first approach: column by default */ .navbar { display: flex; flex-direction: column; /* Default for mobile */ width: 100%; gap: 10px; } /* Switch to row for larger screens */ @media (min-width: 768px) { .navbar { flex-direction: row; /* Row for desktop */ justify-content: space-between; } }
Here's how the same navigation menu adapts between mobile and desktop screens:
Remember that responsive design is not just about convenience—it directly impacts user satisfaction and site usability. Some companies have even faced legal issues over websites that aren't properly optimized for mobile devices.
💡 Pro Tip:
Use a mobile-first approach by setting your default flex-direction for small screens, then use media queries to adapt for larger screens. This ensures your site works well on mobile even if the media queries fail to load.
Now it's time to put your knowledge into practice! You'll create a simple mobile app interface using flex-direction to properly organize elements.
Create a mobile-style app layout that includes:
For mobile, think of serving your navigation buttons like a stack of pancakes—one on top of the other. For desktop, serve them side-by-side like a breakfast buffet.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Mobile App Layout</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 0; } .app-container { height: 100vh; display: flex; flex-direction: column; } .app-header { background-color: #333; color: white; padding: 1rem; text-align: center; } .app-content { flex: 1; padding: 1rem; background-color: #f5f5f5; overflow-y: auto; } .app-nav { /* Add your navigation styles here */ background-color: #eaeaea; padding: 0.5rem; /* TODO: Add flexbox properties */ } .nav-button { background-color: white; border: 1px solid #ddd; border-radius: 4px; padding: 0.75rem; margin: 0.25rem; text-align: center; cursor: pointer; } /* TODO: Add media query for desktop layout */ @media (min-width: 768px) { /* Change navigation direction for desktop */ } </style> </head> <body> <div class="app-container"> <header class="app-header"> <h1>My App</h1> </header> <main class="app-content"> <h2>Welcome to My App</h2> <p>This is the main content area.</p> </main> <nav class="app-nav"> <!-- Add your navigation buttons here --> <div class="nav-button">Home</div> <!-- Add more buttons --> </nav> </div> </body> </html>
💡 Hint:
For the mobile layout, you'll need to use flex-direction: column for the .app-nav. For desktop, you'll change it to flex-direction: row in the media query.
Don't forget to test your layout at different screen sizes. Resize your browser window to see how it responds, or use the device toolbar in your browser's dev tools.
flex-direction controls the flow of items in a flex containerrow arranges items horizontally (default)column stacks items vertically
(looks pointedly at the mobile app layout with tail wagging slightly)
*Woof!*
(enthusiastically) THE BOSS APPROVES! And I've been miraculously cured of my Flex-Direction-Dysfunction! It's a coding MIRACLE!
(rolling her eyes) It was never a real condition to begin with...
(to students) Remember: row for wide, column for narrow. Adapt your layout to the available space. Always test on real devices. That's it.
Next up: Justify Content - Horizontal alignment options