DEV Community

Cover image for flexbox
GalinaLK
GalinaLK

Posted on

flexbox

I'll show you flexbox with a few examples from my video on YouTube - https://youtu.be/k1CigYz5Tlg. The images are screenshots.They don't have a good quality but everything is quite simple.
This is a div with a class named container with 3 divs inside with a class called box:
Image description

This is the styling for the container and the boxes:
Image description

And this is what it looks like in the browser:
Image description

This is what it looks like when you add display: flex;
Image description

The items display as a row from left to right, like we read sentences in English.
The gap property creates a gap between the items:
Image description

flex-direction: column; will make them display as a column:
Image description

When you add display: flex; the property flex-direction is set to row. When flex-direction is set to row the row is called the main axis. When it's set to column the column is called the main axis. The cross axis is perpendicular to the main axis:
Image description

The justify-content property changes their position along the main axis. justify-content: center; will place the items at the center of the container:
Image description

The justify-content property changes their position along the main axis. justify-content: center; will place the items at the center of the container:

Image description

justify-content: flex-end; will position them at the end of the container:

Image description

justify-content: flex-start; will place them at the start of the container:

Image description

justify-content: space-around; gives every item equal space on both sides (the space between them looks bigger because of the gap):

Image description

justify-content: space-between; create space between them, but not around the container:

Image description

justify-content: space-equal; makes the space between them and around the container equal:

Image description

I added height: 80vh; to the container:

Image description

And this is what it looked like:

Image description

The items are stretched. This is the default behavior when you add display: flex;
The align-items property aligns them vertically. When it's set to center they're vertically centered:

Image description

This is the default value:

Image description

align-items: flex-end; will place them at the bottom of the container:

Image description

align-items: flex-start; will place them at the top:
Image description

The items don't break into multiple lines. I added more items to show you how to make them wrap:

Image description

I added flex-wrap: wrap; to the container. It will make them wrap onto multiple lines:
Image description

flex-wrap: nowrap; is the default value when you add display: flex;

Image description

I removed the gap and changed the names of these three classes to show you how to make elements bigger than the others when there's available space:

Image description

I'll show you flex-grow. box1 gets flex-grow: 1;
box2 gets flex-grow: 2;
box3 gets flex-grow: 3;
This is what the styling looks like:

Image description

Image description

Image description

box3 got three parts of the space that is not needed, box2 got two and box1 got one:

Image description

I added another container with a class called container2 with 3 divs inside with classes named child to show you the flex-basis property:

Image description

This is the styling for container2:

Image description

I also added a gap to the container. This is the styling for the items and what they look like:

Image description

This is what they look like with flex-basis: 20px;
Image description

This is what they look like with flex-basis: 50px;

Image description

And this the items with flex-basis: 100px;
Image description

flex-basis changes the length of the elements.
flex-basis: auto; is the default value. It depends on the length of the element or its content if length is not set:
Image description

The elements can grow if we change the flex-grow property.
I'll change two of the class names and delete flex-basis: auto; to show you flex-shrink. Now the second element is has a class named child1 and the third has a class called child2:

Image description

Image description

I'll give all divs inside container2 flex-basis: 150; It'll make them so big that there won't be enough room for all of them in the container and they'll have to shrink:

Image description

child1 will get flex-shrink: 1;
child2 will get flex-shrink: 2;
and child3 will get flex-shrink: 3;
This is the styling for the divs inside .container:

Image description

Image description

child3 has to shrink 3 times more than the others. With flex-shrink you can choose how big they should be when they have to shrink:

Image description

I'll give the container a height of 100px to show you the property called order:

Image description

The first item will get order: 3;

Image description

Now it's in the third position. The order property changes the order of the elements:

Image description

But if the order of your items is not correct you should change it in the html file. It's not a good idea to use the order property for that.

The align-self property changes the position of one element. I'll give child2 align-self: center; It will position it at the center:

Image description

align-self: stretch; will stretch it. That's the default:

Image description

align-self: flex-start; will put it at the top of the container:

Image description

With align-self set to flex-end the item will be at the bottom of the container:

Image description

Top comments (0)