Hey there, Today we are going to discuss the very important topic for Responsive Web Designs that is CSS flexbox layout with detailed examples.
By the end of this CSS flexbox tutorial, you will be able to use it easily.
CSS Flexbox layout which is officially known as Flexible Box Layout Module is very useful to build responsive websites.
This flexible layout system will help us to design responsive website for all devices with simple and easy CSS flexbox properties.
You must read about CSS media query to understand the responsive web design more deeply.
That’s why we have designed this CSS flexbox tutorial where you can find all the important and useful flexbox properties with examples.
No, not at all, just stay tuned with us.
As its name suggest flexbox, means flexible box. Which can align their items horizontally or vertically.
Even can expand or shrink in size (height and width) to cover the free space or to prevent overflow.
With this characteristic’s CSS flexbox can help us to design very responsive websites for all kind of devices.
CSS Flexbox is a single dimensional layout model.
Single dimensional means one direction at a time either row or column.
Because of this limitation, it used for small scale websites or block sections of websites. Whereas CSS grids used for large websites.
Yeah! Good question.
Because it comes many years ago, it is supported by all major browsers. More on browser support information is available at caniuse.com.
One more resource to check the browser compatibility is MDN browser support chart.
Basically, there are two kind of flex elements,
Because, Flex layout model is a collection of CSS properties. So, there are two kind of properties for two flex elements.
1. | display | flexbox | inline-flex |
2. | flex-direction | row | row-reverse | column | column-reverse |
3. | flex-wrap | nowrap | wrap | wrap-reverse |
4. | flex-flow | <flex-direction> | <flex-wrap> |
5. | Justify-content | flex-start | flex-end | center | space-between | space-around |
6. | align-items | flex-start | flex-end | center | baseline | stretch |
7. | align-content | flex-start | flex-end | center | space-between | space-around | stretch |
1. | order | <integer> |
2. | flex-grow | <number> |
3. | flex-shrink | <number> |
4. | flex-basis | <length> | auto |
5. | flex | none | <flex-grow> | <flex-shrink> | <flex-basis> |
6. | align-self | auto | flex-start | flex-end | center | baseline | stretch |
Ok, let’s get started,
First thing that you have to do is just create a flex container element, like below,
.flex-container { display: flex; }
We can use display: flex
if want to use container at block level. It means flex container will cover the full width and it will not allow another element to take place next to it.
Another thing is inline-level element which allows other elements to take place next to it. Here we can set display: inline-flex.
After creating flex container, it will allow us to apply all flex properties to its direct child elements.
To understand more about direct child approach, look at the below graphics.
Here, you can see, blue element is a flex-container with display: flex
. Orange elements are flex-items because these are direct child elements of flex-container (blue).
But purple elements are not direct child of blue element (flex-container) that’s why purple elements behave as per their own property (block or inline).
First of all, I want discuss flex-direction which is very important to understand before other flex properties.
As we have discussed that each flex element can use one direction at a time (single dimensional) either row or column.
So, flex-direction can have following possible values,
For example, if parent element has flex-direction: row
then it’s child elements will be horizontally aligned.
And if the parent element has flex-direction: column
then it’s child elements will be vertically aligned.
.flex-container { display: flex; flex-direction: row; }
.flex-container { display: flex; flex-direction: column; }
Flex-wrap can help us to handle the overflow of flex-items. We can manage flex-items in a single line or multiple lines with the help of flex-wrap.
The default value of flex-wrap is nowrap, it means by default flex-items will align in a single row.
But we have another value for flex-wrap which is wrap. It allows flex-items to shift to the next row if needed according to the device or window size.
.flex-container { display: flex; flex-wrap: wrap; }
Ok, even we have wrap-reverse that will shift overflowed row to the top
.flex-container { display: flex; flex-wrap: wrap-reverse; }
We have another interesting flex container property that is flex-flow.
Basically, it combines both flex-direction and flex-wrap to create a shorthand property flex-flow: <flex-direction> <flex-wrap>
.
.flex-container { display: flex; flex-flow: row wrap; }
CSS flexbox justify-content is used to align the flex-items with the following possible values.
But here, one question must encounter us that is which direction will be used by justify-content to align flex-items. Like
Justify-content will align items from left to right with the help of flex-start value and right to left with flex-end.
It will horizontally center the flex-items by using justify-content: center.
It means, everything will work horizontally.
.flex-container { display: flex; flex-direction: row; justify-content: flex-start; }
.flex-container { display: flex; flex-direction: row; justify-content: flex-end; }
.flex-container { display: flex; flex-direction: row; justify-content: center; }
.flex-container { display: flex; flex-direction: row; justify-content: space-between; }
.flex-container { display: flex; flex-direction: row; justify-content: space-around; }
Now, justify-content will align flex-items vertically from top to bottom. Like flex-start means top and flex-end means bottom.
Even justify-content: center
will center the flex-items vertically.
.flex-container { display: flex; flex-direction: column; justify-content: flex-start; }
.flex-container { display: flex; flex-direction: column; justify-content: flex-end; }
.flex-container { display: flex; flex-direction: column; justify-content: center; }
I hope you get the picture of justify-content in both context row and column flex-direction.
CSS flexbox Align-items will also work as justify-content to align flex-items but in opposite direction. Like if flex-direction is row then align-items will work vertically and if flex-direction is set to column then align-items will work horizontally.
Align-items have following possible values,
.flex-container { display: flex; flex-direction: column; align-items: center; }
Align-content will align flex line in the same direction as align-items. Like if flex-direction is row then it will align flex-line to vertically and if flex-direction is column then it will align flex-line to horizontally.
Align-content have following possible values,
.flex-container { display: flex; flex-direction: column; align-content: center; }
To use flex-item we don’t need to do anything special or different than flex-container because when we defined the parent element as a flex container then its direct child elements will automatically become flex-items.
Now, we have some properties to use with flex-items.
Guess what?
As its name suggest, CSS flexbox order can change the sequence of flex-items with different order. Like below
<div class="d-flex container"> <div style="order: 3;" class="item"> Item 1 </div> <div style="order: 6;" class="item"> Item 2 </div> <div style="order: 4;" class="item"> Item 3 </div> <div style="order: 5;" class="item"> Item 4 </div> <div style="order: 1;" class="item"> Item 5 </div> <div style="order: 2;" class="item"> Item 6 </div> </div>
So, with the help of order property we can change the layout without actually change the order of elements.
Flex-grow basically use the available space of flex-container to expand the flex-item.
As you can see the difference between two examples where green boxed flex-item shows the flex-grow effect.
It’s basically used to ensure that how much a flex-item can shrink if there is not enough space available in the flex-container. Default value is 1 and value must be a number.
Actually, flex-basis define the default size of flex-item before distributing available space of flex-container.
We can specify the width of the element like below,
flex-basis: 144px | 12em | auto;
or can use keywords
flex-basis: fill | max-content | min-content | fit-content | content;
Flex is basically a shorthand property. It covers flex-grow, flex-shrink, and flex-basis. Like below in the example
Flex: 0 0 100px;
It is good practice to use this shorthand instead of full syntaxes. Flex-shrink and flex-basis are two optional parameters.
Default is
Flex: 0 1 auto;
Now it’s time to align flex-items by themselves. So, we have align-self property which is flex-item based property.
align-self: auto | flex-start | flex-end | center | baseline | stretch;
With the help of this CSS property we can align individual flex-item.
We have found some interesting web games made with CSS flexbox or made to practice CSS flexbox layout.
If you like the effort, please comment and share it with your friends.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…
View Comments