The Flexbox Layout, also referred to as the flexible box layout, is an incredibly versatile approach to positioning, sizing, and directing items within a container. As the name suggests, it offers a flexible way to arrange elements in a layout that can easily adapt to different screen sizes and resolutions.
It is important to note that, unlike the Grid Layout, which is a two-dimensional layout, the Flexbox Layout focuses on one-dimensional layouts. This means that it can control the layout either in a row or column at a time, but not both simultaneously. However, with its ease of use and powerful capabilities, it is a highly valuable tool in web design.
Rest assured, we will delve further into this topic and explore the many benefits and usages of Flexbox Layout in the upcoming discussion.
Basic Concept
Here is the graph describing the terms of Flexbox layout from MDN.
Let's explain the terms one by one:
-
Flex Container: You should use
display: flex
on the container element to declare that the container element will use Flexbox layout. - Flex Items: Any direct children of the Flex container will become the flex items.
- Main Axis: Main axis is dependent on the direction of flex, by default it is horizontal.
- Cross Axis: Cross axis is dependent on the direction of flex, by default it is vertical.
- Main Start/End, Cross Start/End: They are simply the start or end of the main or cross axis.
Related Properties
Here are almost all the related properties to the flexbox layout, we will explain how they work later.
-
Flex container related properties:
flex-direction
,flex-wrap
,flex-flow
,justify-content
,align-content
,align-items
,place-content
,place-items
,column-gap
,row-gap
,gap
-
Flex items related properties:
flex
,flex-basis
,flex-grow
,flex-shrink
,order
,align-self
A Quick Example
Here is a quick example of how we may use some of the properties. You can click the index.css to say how we use those properties.
Hope the example may give a brief idea about how flexbox works. And now let's talk about how each property works and may affect the layout in detail.
Declare Flexbox Layout
There are two options to declare an element as the flexbox layout. The one we always use is display: flex
which asks an element (container) to use the flexbox layout in which the element (container) itself is considered a block element. Another one is display: inline-flex
which is just similar to the first one but the element (container) itself is the inline block.
Thus, only two ways to declare flexbox layout you should remember: display: flex
and display: inline-flex
.
Flexbox Direction
By default, display: flex
will make all the container's direct children lay along the horizontal line. But, you can also make children lay vertically by explicitly applying flex-direction: column
. Barring aligning children horizontally or vertically, you can also reverse the children's order by applying flex-direction: row-reverse
or flex-direction: column-reverse
.
Wrap
By default, the children of flex container will condense as more children are pushed. When the width of all children exceed the width of the container, the content will overflow. Usually, at this time, you will want to break the lines and put overflowed children to the next line. Thus, flex-wrap: wrap
which will be putted on container can help you break the overflowed children to the next line. And one thing worthy to be noted is that you can almost consider each line as a flex container.
Alignment
Within Flexbox layout, you can align children along main axis or cross axis, align a certain specific item, or align multiple axis lines. Let's dicusse them one by one in detail.
Main Axis Items Alignment
justify-content
property defines how the browser distributes space between and around content items along the main-axis of a flex container.
We can use justify-content
property to align direct children of the container along main axis. There are several values we can use for this property to align the content:
-
center
: children will be packed and aligned in the center along the main axis. -
flex-start
andflex-end
: children will be packed and aligned starting from the start or end of the main axis. -
space-betweeen
: spaces between children will be distributed evenly while the first item is at the edge of the start of the main axis and the last item at the edge of the end of the main axis. -
space-around
: similar tospace-between
but the space between the edge of the start and the first item and between the edge of the end and the last item is half space of the space between each item. -
space-evenly
: similar tospace-around
but all the space including the space between items and edges and between items and items are the same.
Cross Axis Items Alignment
We can use align-items
property to align the direct children of the container along the cross-axis. There are several values we can use for this property to align the content:
-
stretch
: This is the default value. Direct children of the container will be stretched to fit the container’s cross-axis size. -
center
: Direct children of the container will be aligned at the center along the cross-axis. -
flex-start
: Direct children of the container will be aligned at the start of the cross-axis. -
flex-end
: Direct children of the container will be aligned at the end of the cross-axis. -
baseline
: Direct children of the container will be aligned at the baseline of the cross-axis, in which baseline means the first text line position.
Item's Self Alignment
We can use align-self
property on a certain direct child of the container to align that specific child’s position along cross-axis. The values of this property are almost the same as the values of align-items
property with the difference that the align-self
only applies to a certain direct child.
Align Multiple Main Axis
When you apply flex-wrap: wrap
on the container, there might be multiple lines. As we mentioned that you can consider each line as a flexbox container, when we want to align how these lines are aligned along the cross-axis, we can use align-content
. The values of align-content
are almost the same as justify-content
but align-content
apply to how lines are aligned along the cross-axis.
Item Size
There are three properties associated with direct child size:
-
flex-basis
: initial item size -
flex-grow
: how to handle the growth of the direct child size -
flex-shrink
: opposite toflex-grow
, how to handle the shrinkness of the direct child size
Initial Size
Apply flex-basis
on a certain direct child to set its initial size and there are several values we can use:
-
max-content
,min-content
,content
: size will be determined by the content of the item -
<'width'>
: set specific size, like10em
,100px
-
auto
: if item has width or height property, the size is determined by its width or height, or it will fallback tocontent
value
Growness
Apply flex-grow
on a certain direct child to control how the item is grown. 0 is not to grow.
Shrinkness
Apply flex-shrink
on a certain direct child to control how the item is shrunk. 0 is not to shrink.
Item Order
We can use order
property to change the order of a certain direct child of the container. The value of the order
property is basically a number, the smaller the more forward, and the bigger the more latter.
Gap
For the gap, we have three associated properties, gap
, row-gap
, and column-gap
. The gap
is the shorthand for row-gap
and column-gap
.
Top comments (0)