Brandon Dixon
Brandon Dixon

Reputation: 1134

Flex Wrap and Evenly Distribute Between Rows

Demonstration of question

Hello. The image presented above is a menu of buttons (text has been intentionally obfuscated). The menu functionally moves items to the second row as intended, but it does not evenly space the items between the rows. I have looked up solutions online, but they either have not worked (both for me, and some even in its own fiddle), or they have been too restricting. Can anyone recommend a way of making the items split evenly between the two (or more) rows?

Here is the associated CSS:

#tabs {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    background-color: rgb(50,50,50);
    justify-content: space-around;
}

#tabs button {
    background-color: <?php echo $style['buttonColor']; ?>;
    color: white;
    margin-left: 5px;
    margin-right: 5px;
    margin-top: 10px;
    margin-bottom: 10px;
    padding: 10px;
    font-size: 18px;
    border: 1px solid black;
    -webkit-transition-duration: 0.4s; /* Safari */
    transition-duration: 0.4s;
    cursor: pointer;
}

Upvotes: 6

Views: 4312

Answers (1)

Dylan
Dylan

Reputation: 1714

You can add flex-grow: 1 to your children elements to make them fill the empty space.

https://jsfiddle.net/sabvyfow/

If you want an even number in each column that breaks dynamically, you will probably have to resort to media queries at certain break points: https://www.w3schools.com/css/css3_mediaqueries.asp

Upvotes: 3

Related Questions