Hello Everyone, hope you all are doing well. Today in this article we will learn about FlexBox. so let's first understand what is flexbox?
What is FlexBox?
FlexBox is a box model in CSS which allows the developer to design the web page more faster and conveniently
Flex Box allows the developer to align the items on the web page either in the horizontal direction (left-to-right) or in the vertical direction(top-down)
With the help of FlexBox, we can design the webPage more efficiently by breaking the webPage into rows and columns and then building them using the flexbox
As you can in image 1 we have divided the web page into 2 equal columns.
1st Column consists of 3 rows and 2nd column has an image
With this approach, we can easily design any web page.
How to use Flex Box?
To work with FlexBox we need to set the property of display to flex
display: flex
By setting the property of display flex, we can access all the features of flexbox
All the flex properties with default values are applied to the container and its direct children
By using display Flex
All the items we get align side by side
Example
div {
display: flex;
}
By setting the property of display flex, all 4 boxes are aligned side by side
By applying display: flex, we can use various display properties like:
flex-direction
justify-content
align-items
Flex-Wrap
Align-Content
Order
Flex-Direction:
Flex-Direction allows the developer to arrange the items in row or column
By default the flex Direction is ROW
The developer can change The Flex-Direction to Row, row-reverse, column, column-reverse
flex-direction: row | column | row-reverse | column-reverse
In the above image 1st, we have set the
flex direction to row.
2nd-time
flex-direction: row-reverse
3rd-time
flex-direction: column
4th-time
flex-direction: column-reverse
Justify-Content:
Justify-Content is one of the most used properties in FlexBox
It is used to align or manage the space between the items
By-Default its property is set to
justify-content: flex-start
which means items should be aligned to leftBe Cautions while using justify-Content because
flex-direction
has a direct impact onjustify-content
justify-content: flex-start | flex-end | center | space-between | space-around
justify-content: flex-Start
will align the content toward the start of the page/left sidejustify-content:flex-End
will align the content towards the end of the page/right sidejustify-content:flex-center
will align the content toward the center of the page/centerjustify-content:space-between
will distribute space evenly between the items, and all items will be separated by equal spacejustify-content:space-around
accomplishes the same thing except that the first and last items have no space toward the container's boundary.justify-content:space-evenly
will distribute space evenly between the items, now all the nodes will have the same space on both sides including the first and last item
Align-Items:
Align-Items is one of the most used properties in FlexBox
It is used to align or manage the space between the items
align-items
is the same asjustify-content
only for the cross-axis.If the main axis is horizontal, the cross axis is vertical (top-to-bottom)
if the main axis is vertical, the cross axis is horizontal (left-to-right).
align-items: flex-start | flex-end | center
are self-explanatory andbaseline
is useful if you want your item's text content to be aligned.
align-items: flex-start
items are aligned toward the top of the pagealign-items: flex-end
items are aligned toward the end of the pagealign-items: flex-center
items are aligned toward the center of the pageBut Always remember it is based on MAIN Axis
Flex-Wrap:
In this instance, all the flex items have explicit widths set to 30%.
If there are more than three things in the container, we would ordinarily expect the fourth item to overflow or break into the following row.
flex-wrap
is set towrap
, the fourth item becomes a second row as the width increaseflex-wrap
is set tono-wrap
, the fourth item remains in its place onlyflex-wrap
is set towrap-reverse
, it reverses the wrap
Align Item:
align-items
is the same asjustify-content
only for the cross-axis.If the main axis is horizontal, the cross axis is vertical (top-to-bottom), if the main axis is vertical, the cross axis is horizontal (left-to-right).
align-items: flex-start | flex-end | center
are self-explanatory andbaseline
is useful if you want your item's text content to be aligned.
Thanks, for reading the Article, Please give your valuable Feedback