Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 33.33% width flex divs in multiple rows?

Tags:

css

flexbox

I am trying to get flex divs stacked in rows of 3. Sometimes I will have 3 divs, sometimes 6 or 21, etc. I got the widths and paddings figured out, but I can't figure out how to get them to start a new row after each set of 3.

How can I achieve that? Right now it's putting them all in 1 line.

The HTML code is like this:

<div id="container">

  <div class="sections__container">

        <div class="sections__container__a">A-1</div>
        <div class="sections__container__b">A-2</div>
        <div class="sections__container__c">A-3</div>

        <div class="sections__container__a">B-1</div>
        <div class="sections__container__b">B-2</div>
        <div class="sections__container__c">B-3</div>

  </div> 

</div>

Each flex div is using CSS like:

width: 33.33%;
height: 100%;
float: left;
flex-grow: 1;

And here's a jsFiddle to make things easier: https://jsfiddle.net/xr746syj/

Thank you so much for any advice :)

like image 877
moo moo Avatar asked Mar 05 '19 12:03

moo moo


People also ask

How do you flex 3 items per row?

For 3 items per row, add on the flex items: flex-basis: 33.333333% You can also use the flex 's shorthand like the following: flex: 0 0 33.333333% => which also means flex-basis: 33.333333% .

How do I display flex items in a row?

There is no method in flexbox to tell items in one row to line up with items in the row above — each flex line acts like a new flex container. It deals with space distribution across the main axis.

How do you equally divide flex into two columns?

Use a container with display: flex and assign the width to 50% to the two parts you want to divide.


1 Answers

First, you need to use flex-wrap: wrap so that your rows break at the proper point.

Second, you're using margins and need to account for them in your width calculations.

So, if you're saying a block is 33.33% but has margin-left: 5px, you need to use width: calc(33.33% - 5px).

Last, I removed the floats from your code, since they are useless properties when applied to flex children. Your code could be massively simplified by creating a common class that contained all the shared values between each section.

Here's the full, working code:

* {
  box-sizing: border-box;
}

#container {
  width: 600px;
  margin: 0 auto;
  background-color: #ececec;
  padding: 10px;
  margin-bottom: 30px;
}

.sections__container {
  margin: 0 auto;
  max-width: 600px;
  display: flex;
  flex-wrap: wrap;
}

.sections__container__a {
  margin-right: 5px;
  width: calc(33.33% - 5px);
  height: 100%;
  border-radius: 4px;
  flex-grow: 1;
  background-color: #FFFFFF;
  font-size: 0.80rem;
  padding: 15px 0px 10px 0px;
  font-weight: 600;
  margin-bottom: 10px;
  text-align: center;
}

.sections__container__b {
  margin: 0 5px;
  width: calc(33.33% - 10px);
  height: 100%;
  border-radius: 4px;
  flex-grow: 1;
  background-color: #FFFFFF;
  font-size: 0.80rem;
  padding: 15px 0px 10px 0px;
  font-weight: 600;
  margin-bottom: 10px;
  text-align: center;
}

.sections__container__c {
  margin-left: 5px;
  width: calc(33.33% - 5px);
  height: 100%;
  border-radius: 4px;
  flex-grow: 1;
  background-color: #FFFFFF;
  font-size: 0.80rem;
  padding: 15px 0px 10px 0px;
  font-weight: 600;
  margin-bottom: 10px;
  text-align: center;
}
<div id="container">
  <div class="sections__container">
    <div class="sections__container__a">A-1</div>
    <div class="sections__container__b">A-2</div>
    <div class="sections__container__c">A-3</div>

    <div class="sections__container__a">B-1</div>
    <div class="sections__container__b">B-2</div>
    <div class="sections__container__c">B-3</div>
  </div>
</div>

jsFiddle

like image 139
Andy Hoffman Avatar answered Oct 26 '22 23:10

Andy Hoffman