Flexbox In-Depth Documentation
As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.
With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.
In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.
I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.
Flexbox is a CSS layout module that makes it easier to design flexible and responsive layout structures without using float or positioning. It is especially useful for arranging elements in a predictable way when the size of the container or the items is unknown or dynamic.
Basic Concepts of Flexbox
Flex Container and Flex Items
A flex container is defined by setting the
displayproperty toflexorinline-flex.The children of a flex container are called flex items.
Main Axis and Cross Axis
- The main axis is the primary axis along which flex items are laid out. The cross axis is perpendicular to the main axis.
Flex Container Properties
display
display: flex;ordisplay: inline-flex;Defines a flex container and enables a flex context for all its direct children.
flex-direction
Defines the direction in which the flex items are placed in the flex container.
Possible values:
row(default): left to rightrow-reverse: right to leftcolumn: top to bottomcolumn-reverse: bottom to top
Example:
.container { display: flex; flex-direction: row; }
flex-wrap
Controls whether the flex container is single-line or multi-line, and the direction of the cross-axis.
Possible values:
nowrap(default): single-linewrap: multi-linewrap-reverse: multi-line with reverse order
Example:
.container { display: flex; flex-wrap: wrap; }
justify-content
Defines the alignment along the main axis.
Possible values:
flex-start(default): items are packed toward the start of the main axisflex-end: items are packed toward the end of the main axiscenter: items are centered along the main axisspace-between: items are evenly distributed, with the first item at the start and the last item at the endspace-around: items are evenly distributed with equal space around themspace-evenly: items are evenly distributed with equal space between them
Example:
.container { display: flex; justify-content: space-between; }
align-items
Defines the default behavior for how flex items are laid out along the cross axis.
Possible values:
stretch(default): items stretch to fill the containerflex-start: items are packed toward the start of the cross axisflex-end: items are packed toward the end of the cross axiscenter: items are centered along the cross axisbaseline: items are aligned along their baseline
Example:
.container { display: flex; align-items: center; }
align-content
Aligns a flex container’s lines within the flex container when there is extra space in the cross axis.
Possible values:
stretch(default)flex-startflex-endcenterspace-betweenspace-aroundspace-evenly
Example:
.container { display: flex; align-content: space-around; }
Flex Item Properties
order
Controls the order in which flex items appear within the flex container.
Default value is
0.Example:
.item { order: 2; }
flex-grow
Defines the ability for a flex item to grow if necessary.
Default value is
0.Example:
.item { flex-grow: 1; }
flex-shrink
Defines the ability for a flex item to shrink if necessary.
Default value is
1.Example:
.item { flex-shrink: 1; }
flex-basis
Defines the default size of an element before the remaining space is distributed.
Default value is
auto.Example:
.item { flex-basis: 200px; }
flex
A shorthand for
flex-grow,flex-shrink, andflex-basis.Example:
.item { flex: 1 0 200px; /* flex-grow | flex-shrink | flex-basis */ }
align-self
Allows the default alignment to be overridden for individual flex items.
Possible values:
auto(default)flex-startflex-endcenterbaselinestretch
Example:
.item { align-self: flex-end; }
Practical Examples
Simple Flexbox Layout
<div class="container"> <div class="item">1</div> <div class="item">2</div> <div class="item">3</div> </div> <style> .container { display: flex; justify-content: space-between; align-items: center; height: 100vh; } .item { background: #f3f3f3; padding: 20px; border: 1px solid #ccc; } </style>Responsive Navbar
<nav class="navbar"> <div class="logo">MyLogo</div> <ul class="nav-links"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li> </ul> </nav> <style> .navbar { display: flex; justify-content: space-between; align-items: center; padding: 10px; background: #333; } .logo { color: #fff; font-size: 24px; } .nav-links { display: flex; list-style: none; } .nav-links li { margin: 0 10px; } .nav-links a { color: #fff; text-decoration: none; } </style>
Learning Through Games
To reinforce the understanding of Flexbox, we played two interactive games:
Flexbox Froggy
A game where you help Froggy and friends by writing CSS code.
Flexbox Defense
A tower defense game that teaches you CSS flexbox.
By understanding and practicing with Flexbox, you will be able to create complex layouts more easily and efficiently. Flexbox provides a more robust and predictable way to handle layout compared to traditional methods like floats and positioning.