Author: M Abo Bakar Aslam

Display as Flex

Flexbox is a powerful CSS layout system designed to arrange elements in a flexible and responsive way. It helps developers easily align, distribute, and control the size of items within a container, making modern web layouts more efficient and adaptive to different screen sizes. In this section, you will explore the core concepts of Flexbox and learn how to use its key properties to build responsive and well-structured layouts.

Example Code 1: Creating a simple container and nested container.

<!--File: myFile.html -->
<div style="border: 1px solid black;"><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 1 - visualized by edu2skill

Example Code 2: Defining flex container

    <!--File: myFile.html -->
<div style="border: 1px solid black; display: flex; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 2 - visualized by edu2skill

Example Code 3: Defining flex-direction along with flex display.

    <!--File: myFile.html -->
<div style="border: 1px solid black; display: flex; flex-direction: column; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 3 - visualized by edu2skill

Example Code 4: Set value of flex-wrap as wrap

<!--File: myFile.html -->
<div style="border: 1px solid black; display: flex; flex-wrap: wrap; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 4-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>4</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 5-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>5</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 6-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>6</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 7-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>7</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 8-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>8</div>
        </div>
    </div>
</div>

Output

Flex Example 4 - visualized by edu2skill

Example Code 5: justify-content

    <!--File: myFile.html -->
<div style="border: 1px solid black; display: flex; justify-content: center; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 5 - visualized by edu2skill

Example Code 6: align-items

    <!--File: myFile.html -->
<div style="border: 1px solid black; height: 200px; display: flex; align-items: flex-start; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 6 - visualized by edu2skill

Example Code 7: Both align-items and justify-content

<!--File: myFile.html -->
<div style="border: 1px solid black; height: 200px; display: flex; justify-content: center; align-items: center; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
</div>

Output

Flex Example 7 - visualized by edu2skill

Example Code 8: align-content (Can be skipped. You can do its work by using justify-content and align-items together. Moreover, you will have to use multiple flex containers.)

    <!--File: myFile.html -->
<div style="border: 1px solid black; height: 200px; display: flex; align-content: space-around; flex-wrap: wrap; "><!--Outermost container-->
    <div style="margin: 2px; border: 1px solid red;"><!--nested containter 1-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>1</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid blue;"><!--nested containter 2-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>2</div>
        </div>
    </div>
    <div style="margin: 2px; border: 1px solid yellow;"><!--nested containter 3-->
        <div style="width: 50px; height: 50px; background-color: #FFCB9A; display:flex; justify-content: center; align-items: center;">
            <div>3</div>
        </div>
    </div>
</div>

Output

Flex Example 8 - visualized by edu2skill