Master the most powerful layout tool in CSS with hands-on examples, interactive demos, and real-world applications.
What is Flexbox?
Flexbox (Flexible Box Layout) is a powerful CSS layout method that provides an efficient way to arrange, distribute, and align elements within a container, even when their size is unknown or dynamic.
💡 Pro Tip: Flexbox is perfect for one-dimensional layouts (either row or column). For two-dimensional layouts, consider CSS Grid!
Key Benefits
Easy vertical and horizontal centering
Flexible item sizing and distribution
Responsive layouts without media queries
Simplified alignment and spacing
Order control without changing HTML
Perfectly Centered!
Flexbox Fundamentals
To use flexbox, you need two main components: a flex container (parent) and flex items (children).
/* Make an element a flex container */.container {
display: flex;
}
/* All direct children become flex items automatically */.container > * {
/* These are now flex items */
}
The Flex Container
When you set display: flex on an element, it becomes a flex container and its direct children become flex items.
Item 1
Item 2
Item 3
Main Axis vs Cross Axis
Flexbox operates on two axes:
Main Axis: The primary axis along which flex items are laid out
Cross Axis: The axis perpendicular to the main axis
⚠️ Remember: The main axis changes based on flex-direction. In row direction, main axis is horizontal. In column direction, main axis is vertical.
Flex Container Properties
These properties are applied to the flex container (parent element):
Media content goes here. This text will wrap around and take up the remaining space.
Browser Support
Flexbox is widely supported in all modern browsers:
Chrome: 29+
Firefox: 28+
Safari: 9+
Edge: 12+
Internet Explorer: 11 (partial support)
💡 Pro Tip: For the best compatibility, use the standard flexbox properties without vendor prefixes. Autoprefixer can help add necessary prefixes during your build process.
Flexbox is an incredibly powerful layout tool that simplifies many common UI patterns. By mastering the properties and concepts covered in this guide, you'll be able to create flexible, responsive layouts with ease.
🎯 Final Challenge: Try recreating a complex website layout using only Flexbox. Experiment with nesting flex containers and combining different properties to achieve your desired design!