The flex property for a class is not applying when flex direction is column

Viewed 155

For instance when flex-direction is row the property for .item-2 is in effect but it is not the case when flex-direction is column. What changes do i incorporate for the same. Thank You

This was in a attempt to undersstand how flexbox works in css as opposed to floats that is why . THanks once again

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Flex Basics</title>
        <style>
            * {
                box-sizing: border-box;
            }
    
            body {
                font-family: Arial, Helvetica, sans-serif;
            }
    
            .flex-container {
                background-color: #f4f4f4;
                display: flex;
                flex-direction: column;
                /* Cross axis align */
                align-items: stretch;
               
                /* Main axis align */
                height: 50vh;
            }
    
            .item {
                background: #f4f4f4;
                border: #ccc solid 2px;
                margin: 10px;
                padding: 10px;
                text-align: center;
                border-radius: 10px;
                flex: 1;
            }
    
            .item-2 {flex: 2}
    
        </style>
    </head>
    <body>
        <div class="flex-container">
            <div class="item item-1">
                <h3>Item 1</h3>
            </div>
            <div class="item item-2">
                <h3>Item 2</h3>
            </div>
            <div class="item item-3">
                <h3>Item 3</h3>
            </div>
        </div>
    </body>
    </html>
1 Answers

Everything works.

flex: 2 is a shorthand for three properties. One of them being flex-grow. Your item is growing in the case of flex-direction: row because there is a space to grow.

But when you do flex-direction: column, the height of the container is not enough for the 2nd child to grow. Here I have increased the height a bit:

* {
                box-sizing: border-box;
            }
    
            body {
                font-family: Arial, Helvetica, sans-serif;
            }
    
            .flex-container {
                background-color: #f4f4f4;
                display: flex;
                flex-direction: column;
                /* Cross axis align */
                align-items: stretch;
               
                /* Main axis align */
                height: 150vh;
            }
    
            .item {
                background: #f4f4f4;
                border: #ccc solid 2px;
                margin: 10px;
                padding: 10px;
                text-align: center;
                border-radius: 10px;
                flex: 1;
            }
    
            .item-2 {flex: 2}
<div class="flex-container">
            <div class="item item-1">
                <h3>Item 1</h3>
            </div>
            <div class="item item-2">
                <h3>Item 2</h3>
            </div>
            <div class="item item-3">
                <h3>Item 3</h3>
            </div>
        </div>

Related