Skip to main content
Inspiring
May 16, 2023
Question

(CSS) Beginner question about Flex cell width control

  • May 16, 2023
  • 4 replies
  • 915 views

Previous thread got locked, so I'll ask my follow-up here.

 

I've been using Grid for quite some time, was told I'd be better off with Flex, and started fiddling with it to see how quickly I can pick it up. Could someone tell me why the following code isn't displaying both cells as equal width unless there's enough content to fill them?

 

.flex {
display: flex;
width: 100%;
gap: 1.5em;
flex-direction: row
}
.flex .cell {
flex-grow: 1
}

<div class="flex">
    <div class="cell">
        Hi
    </div>
    <div class="cell">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    </div>
</div>

 

I'm trying to keep them each at 50% regardless of content, and couldn't find an equivalent to grid-template-columns: 50% 50%. Thought maybe flex-grow: 1 would do it, but that seems to require that both cells have enough content to fill.

 

(Currently, the single-word cell is only as wide as that word, with the fuller one occupying everything else.)

 

Could someone correct my primitive n00b syntax to force each of the 2 cells to specific percentages regardless of contents, like grid-template-columns lets us do?

 

Thanks in advance!

    This topic has been closed for replies.

    4 replies

    Liam Dilley
    Inspiring
    May 17, 2023

    There are a few mistakes in the comments with regards to Flex.
    Firstly - Everyone should start moving away from percentage widths. Still used, still useful for sure but when you what proper scaling and dynmaic scaling there are more options.
    Viewport width and height - vw, vh
    Rem - Root EM. This is where in html you have a font size you can have direct scaling of elements based on that value.
    Calc is used in an example here and this is a great way to also do values based on the conditions of the display.

    In terms of flex spacing, just as CSS Grid you do not do this with margin, you use the gap value.

     gap: 0.5em;

    For example.

    With regard to Grid you can avoid using media quries. Again you have values to define the current browser conditions now to manage this sort of thing.
    You can repeat your column rules and you can also define the content specifc values (if you want) that it conforms too.

    grid-template-columns: repeat(3, fit-content(410px));
    But one of the cool newer toys with have in our CSS arsenal is minmax.
    You can do columns like this now for example:
    grid-template-columns: minmax(0, 300px) 1fr;
    And the grid gap syntax is prefered now to be:
    grid-gap: 20px;
    So it is not confused with the gap of flexbox.

    The last thing I would add is that the justify-content spacing you have to be mindful as people use it a lot but then it does not fit varied content at different times etc. Here you want to observe using of the flex-basis.
    You can ensure a content fit for example with:
    flex-basis: fit-content;
     
    BenPleysier
    Community Expert
    Community Expert
    May 17, 2023

    So, to translate what you are saying

     

    .row {
        display: flex;
        gap: 1.5rem;
    }
    
    .col {
        flex: 1;
        background: yellow;
        padding: 1rem;
    }

     

     

     

     

    This is a huge improvement, thank you!

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Liam Dilley
    Inspiring
    May 18, 2023

    I been posting for a long time and mainly for the other platforms Adobe had. My thing is I do not just spit out the code answers. I try to provide all the right information and tools needed for the understanding for the code. I know this annoys some people but I wont change my view on that. People who turn to language models to get their code now for example wont learn what it is doing or how to fix it if something is not right or expand on it. The educated will.

    I am glad though you have taken that and got a nice clean CSS output.

    flex: 1;

    Is the only bit you will need to re-visit for responsiveness if you need to.

    BenPleysier
    Community Expert
    Community Expert
    May 17, 2023

    This is a good reference for Flexbox:

    https://developer.mozilla.org/en-US/docs/Web/CSS/flex

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    BenPleysier
    Community Expert
    Community Expert
    May 17, 2023

    This is what I would use:

     

    .flex {
        display: flex;
        justify-content: space-between;
    }
    
    .flex .cell {
        flex-basis: calc(50% - 1.5em); /* width minus gap */
    }

     

     

    A few remarks:

    .flex {

    display: flex;
    width: 100%; = default
    gap: 1.5em;
    flex-direction: row  = default
    }
    .flex .cell {
    flex-grow: 1 = takes up all available space
    }

     

     

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
    Legend
    May 16, 2023

    Just use percentages for widths when youre constructing your equal width columns in Flexbox. You could also use flex-basis to set the column width (see code below). Flex doesnt really play that nicely with gap like Grid does as it tends to add the gap to the right of each container if you set the gap using px, whereas Grid just adds the space between columns, assuming you set the column widths using the grid fr (fraction) unit. You can of course use css calc to overcome the gap issues in Flexbox if you do require the gap to remain at a static px width - you would set the column width as width: calc(50%-15px); lf I use Flex then I use justify-content: space-between; and set the column width to 48% or 49% which creates a gap between the columns.

     

    I only use flex: 1; (the short version of flex-grow: 1; ) if I want a column or row to consume the remaining space. Example I might set the 1st column to 30% or in some cases give it a specific width of 400px - I can then set flex: 1; on the 2nd column to force it to take up the rest of its parent container space. flex: 1; is useful at times, particularly when you want buttons to line up at the foot of each column, other than that I dont find much use for it but Im sure it has other possibilties, maybe.

     

    You may find the below link useful, its a visual guide about Flexbox properties:

    https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 

     

    FLEXBOX:

    .
    flex {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    }
    .flex .cell {
    width: 48%;
    / * flex-basis: 48%; You can also use this instead of width */
    background-color: #f2f2f2;
    }
    @media screen and (max-width: 761px) {
    .flex .cell {
    width: 100%;
    /* flex-basis: 100%; You can also use this instead of width */
    }
    }

    GRID:
    .flex {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    }
    .flex .cell {
    background-color: #f2f2f2;
    }
    @media screen and (max-width: 761px) {
    .flex {
    grid-template-columns: 1fr;
    }
    }