Skip to main content

Command Palette

Search for a command to run...

Flexbox in CSS

Published
•2 min read•View as Markdown
Flexbox in CSS

Before starting with Flexbox we must be familiar with the Parent-Child relationship in HTML elements. So, when one or more elements are declared inside an element then the outer element is called the Parent element, and all the elements inside the parent element are called its children. Let's understand this with one example,

<!-- Parent Element-->
    <div class="parent"> 
        Parent element
        <!-- Child Element-->
        <div class="child">
            child 1
            <img src="./assets/lco_img1.png" alt="image not available">
        </div>
        <!-- Child Element-->
        <div class="child">
            child 2
            <img src="./assets/lco_img2.png" alt="image not available">
        </div>
        <!-- Child Element-->
        <div class="child">
            child 3
            <img src="./assets/lco_img3.png" alt="image not available">
        </div>
    </div>
.parent{
    background-color: #00FFFF;
    padding: 10px;
}
img{
    width: 50px;
    height: 50px;
}

image.png

Flexbox Properties

Display

Flex

Setting element display property as Flex (to parent element), set its children in Flex layout.

display: flex; /* Enables flex for all children. */

Flex direction

flex-direction set the direction of the main axis.

Full CSS

.parent{
    background-color: #00FFFF;
    padding: 10px;
    display: flex;
    flex-direction: row / column / row-reverse / column-reverse;
}
.child{
    border: 2px solid black;
    margin: 10px;
}
img{
    width: 100px;
    height: 100px;
}

Row

Syntax

flex-direction: row;

Output

image.png

Column

flex-direction: column;

Output

image.png

Row reverse

Syntax

flex-direction: row-reverse;

Output

image.png

Column reverse

Syntax

flex-direction: column-reverse;

Output image.png

Justify content

Attempts to distribute extra space on the main axis.

Flex start

Syntax

justify-content: flex-start;

Output image.png

Flex end

Syntax

justify-content: flex-end;

Output

image.png

Center

Syntax

justify-content: center;

Output

image.png

Space-between

Syntax

justify-content: space-between;

Output

image.png

Space around

Syntax

justify-content: space-around;

Output

image.png

Space evenly

Syntax

justify-content: space-evenly;

Output

image.png

Align Items

Determines how items are laid out on the cross axis.

Flex start

Syntax

align-items: flex-start;

Output

image.png

Flex end

Syntax

align-items: flex-end;

Output

image.png

Center

Syntax

align-items: flex-center;

Output

image.png

Flex grow

Allows determining how each child is allowed to grow as a part of a whole. Syntax

.child1{
    flex-grow: 1;
}
.child2{
    flex-grow: 2;
}
.child3{
    flex-grow: 3;
}

Output

image.png

Flex wrap

Wraps items if they can't all be made to fit on one line.

No Wrap

Syntax

flex-wrap: nowrap;

Output

image.png

Wrap

Syntax

flex-wrap: wrap;

Output

image.png

Wrap reverse

Syntax

flex-wrap: wrap-reverse;

Output

image.png