Flexbox: A Smart Way To Make The Webpages Responsive. What Is Flexbox?
Flexbox: A Smart Way To Make The Webpages Responsive. What Is Flexbox?
What is Flexbox?
The Flexbox Layout is derived from the word “Flexible” which means to emphasize on making the items
in the CSS containers flexible enough to adjust or align according to the display devices. Flexbox module
is responsible to adjust the spaces between the items in the container by altering their width or height.
Flex layout has a big advantage over the regular layout as it can work well for even the most detailed
applications in case of layout changes or resizing them. Flex layout follows the flex flow direction for the
alignment unlike the regular layout which is based on block flow and inline flow directions. Flex layout
has two axes one is the main axis, the other one is cross axis which lies perpendicular to the main.
The Flexible Box Layout Module (Flexbox) is an excellent css tool which will make your content in the
container responsive. There are majorly two main parts of a flexbox i.e. flex items and flex containers.
The flex container can be termed as parent and the flex item can be termed as children.
Flex properties
Parent properties (flex container): the definition of the container is wrapped inside a body having a
keyword .container. Each flex property of the container can be defined inside this body.
.container {
1. Display: To define the flex container by initializing it with the space allocation we use this
property. It can be either an inline or block allocation. We set the display property to flex like
below.
.container {
display: flex;
2. flex-direction: This is the most important property in the flexbox layout which defines the axis
for the item layout. Flex-direction can be used to stack the items either vertically or horizontally.
Once the main-axis for the container is defined the flex items can be placed accordingly as per
the direction. For stacking the items vertically we use the column option whereas row option is
used to stack them horizontally.
.container {
flex-direction: row | row-reverse | column | column-reverse;
}
3. flex-wrap- The flexbox layout is by default responsible to fit all the flex-item in single line but
you can explicitly change this property using flex-wrap which defines if the items should be
wrapped or not. By default flexbox has nowrap property which fits all the items in a single line
(in this case items may overflow) while the wrap property will allocate the flex items to multiple
lines.
Below is the syntax demonstrated:-
.container{
flex-wrap: nowrap | wrap | wrap-reverse;
}
4. The flex-flow – This property is a together defines the flex-direction and flex-wrap properties.
Rather than providing separate definitions to these two properties we can define both of them
with flex-flow which will set main and cross axis.
Example:
.container {
5. justify-content- justify-content property will align the flex items horizontally along the main
axis. This property is used to manage the space that is left after the flex-items were allocated
space in container. If in case of the overflow it also helps to manage the alignment of items.
There are several keywords of this property with which flex items can be arranged.
Example:
.container {
justify-content: flex-start | flex-end | center | space-between | space-around |
space-evenly | start | end | left | right ... + safe | unsafe;
}
6. align-items- This property aligns the flex items along the cross axis or vertically. As we know the
cross axis lies perpendicular to the main axis hence it aligns the flex items perpendicular to main
axis.
Example:
.container {
align-items: stretch | flex-start | flex-end | center | baseline | first baseline | last
baseline | start | end | self-start | self-end + ... safe | unsafe;
}
3. flex-shrink- This property adjusts the flex-item by making them shrink relative to the other
items. The value of the item can be assigned and the flex shrink has a value of default value of 1.
This means that in case of the lack of space all items will shrink by a factor 1 or equally. If any
element is given the flex-shrink value as 0 that item is exempted from the shrinking.
Syntax:
.item {
5. align-self- This property can override the alignment that was defined by the align-items property
for a flex-item.
Syntax:
.item {
}
We can make responsive & progressive web apps to give best UI experience and readability to user
using the flexbox layout. Flex is intelligent enough to understand the content of application and
adjust its alignment.