Skip to main content

Command Palette

Search for a command to run...

Css Flex Box

Published
•4 min read•View as Markdown
Css Flex Box

Flex is used to align the elements with operation rows and columns.There are some attributes name as justify-content, align-items, flex-direction, order, align-self, flex-wrap,align-content.

Properties of Flex:

Display:

It defines to the webpage as inline box.

code:

.container{
    background-color: #383838;
    display: flex; }
.value{
    height: 50px;
    width: 50px;
    background-color: red;
    border: 2px solid yellow;

}
 html
<div class="container">
        <div class="value">1</div>
        <div class="value">2</div>
        <div class="value">3</div>
        <div class="value">4</div>
        <div class="value">5</div>
    </div>

Result:

The column box wil align to the inline and flex only works at the rows.

image.png

Flex-Direction:

It is define the alignement of the boxes using row,row-reverse,column,column-reverse these attributes.

code:

.container{
    background-color: #383838;
    display: flex; 
    flex-direction: column;
    flex-direction: column-reverse;
    flex-direction: row-reverse;
    flex-direction: row;
}

Result:

flex-direction: column for this the all boxes are align as column.

image.png

flex-direction: column-reverse for this all boxes are align as reverse column.

image.png

flex-direction: rowfor this all boxes are agline as row.

image.png

flex-direction: row-reverse for this all boxes are display as the

image.png

Flex-wrap:

This flex attribute link with all items with in a container and nowrap, wrap, wrap-reverse using these property we can align the flax items.

code:

.container{
    background-color: #383838;
    height: 500px;
    display: flex; 
    flex-wrap: nowrap;
    flex-wrap: wrap;
    flex-wrap: wrap-reverse;


}

Html:
 <div class="container">
        <div class="value first">1</div>
        <div class="value second">2</div>
        <div class="value thrid">3</div>
        <div class="value fourth">4</div>
        <div class="value fifth">5</div>
        <div class="value">1</div>
        <div class="value">2</div>
        <div class="value">3</div>
        <div class="value">4</div>
        <div class="value">5</div>

    </div>

Result:

flex-wrap: nowrap it wrap up the whole items within one row.

image.png

flex-wrap: wrap it adjust with the width of the browser and wrap the items.

image.png

flex-wrap: wrap-reverse it populate at the end of the web page as reverse align

image.png

Justify-content:

This attributie is used on element that are on row in the webpage. It is deal with row oparation.

Code:

.container{
    background-color: #383838;
    display: flex; 
    justify-content: flex-start;
    justify-content: flex-end;
    justify-content: center;
    justify-content: space-around;
    justify-content: space-between;
    justify-content: space-evenly;
}

Result:

justify-content: flex-start start at first of webpage.

image.png

justify-content: flex-end it start with the boxes at the end of the column

image.png

justify-content: center it align the element at center of the policy.

image.png

justify-content: space-around it is given equaly space to left and right side of the boxes.

image.png

justify-content: space-between; it is give space only two boxes side by side.

image.png

justify-content: space-evenly; it is evenly space between left and right side of boxes.

image.png

Align-itmes:

It is workaround in cloumn side.The atttributes are flex-start, flex-end, center ect.

code:

.container{
    background-color: #383838;
    height: 500px;
    display: flex; 
    align-items: flex-start;
    align-items: flex-end;
    align-items: center;
}

Result:

align-items: flex-start it is align to top.

image.png

align-items: center it is align to middle of the container.

image.png

align-items: flex-end it is align to end of the container.

image.png

Gap:

It is used for giving gap between to boxes. gap,row-gap,column-gap these are present in gap.

code:

.container{
    background-color: #383838;
    height: 500px;
    display: flex; 
    flex-direction: column;
    gap: 30px;
    row-gap: 80px;
    column-gap: 40px;
}

Result:

gap it is giving a gap between two box.

image.png

row-gap it is row gap between boxes.

image.png

column-gapit is column gap between boxes.

image.png

Order:

Flex items are set as acending order by defalut. Using Order we can place the items rearange the items.

code:

.first{
    order: 3;
}
.second{
    order: 1;
}
.thrid{
    order: 5;
}
.fourth{
    order: 2;
}
.fifth{
    order: 4;
}

Html:

 <div class="value first">1</div>
        <div class="value second">2</div>
        <div class="value thrid">3</div>
        <div class="value fourth">4</div>
        <div class="value fifth">5</div>

Result:

As we see that the "1 " set using order to third position and other as define in css.

image.png

Align-self:

It is target any flex itmes and oparate column wise.flex-start,flex-end, center, stretch attributes are used.

code:

.first{
    align-self: flex-start;
    align-self: center;
    align-self: flex-end;
    align-self: stretch;
}

Result:

align-self: flex-startAs a result target the "1" box which set as position of start.

image.png

align-self: center it set as center of the container.

image.png

align-self: flex-end it set as the end of the container.

image.png

Align-content: