Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make flex container give each children half the space. Why is flex-grow 1 not working?

I think I’m misunderstanding something here. I want to have two children in my flex-container and give them equal space by setting each to flex-grow: 1. But for some reason the one grows more than the other…

The code looks like this:

<body>
    <div class="container">
        <div id="chart">
            <svg>
            </svg>
        </div>
        <div id="description">description</div>
    </div>
</body>

</html>

<style>
    body,
    html {
        margin: 0
    }

    .container {
        border: 1px solid black;
        width: 80vw;
        margin: 0 auto;
        margin-top: 50vh;
        transform: translateY(-50%);
        display: flex;
        position: relative;
    }

    .container>div {
        border: 4px dashed gold;
        flex-grow: 1;
    }

    svg {
        width: 100%;
    }
</style>

The result looks like this, but I would like to be both equal width.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

Every time this has happened to me I fixed it with the property flex-basis.

    body,
    html {
        margin: 0
    }

    .container {
        border: 1px solid black;
        width: 80vw;
        margin: 0 auto;
        margin-top: 50vh;
        transform: translateY(-50%);
        display: flex;
        position: relative;
    }

    .container>div {
        flex-basis: 0;
        border: 4px dashed gold;
        flex-grow: 1;
    }

    svg {
        width: 100%;
    }
<body>
    <div class="container">
        <div id="chart">
            <svg>
            </svg>
        </div>
        <div id="description">description</div>
    </div>
</body>

</html>

Hope this helps!

EDIT:

The solution posted by Alex also works -> flex-basis: 50%;
And in this case it would be the quickest solution!

I just prefer using flex-grow since it usually works better than using percentages in other cases.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading