4

I am currently working on a site that shows some info in boxes. My goal right now is to have 2 div boxes fit on each line. I made it so that more than one div can be on the same line with the code below, but I realized that if I had more than 2 they would all still stack on the same line.

I am not totally sure how I would limit my divs to 2 per line. Some help would be greatly appreciated!

.flex-container {
    display: flex;
}

.flex-child {
    flex: 1;
    /*border: 2px solid yellow;*/
    display: flex;
    justify-content: center;
    margin: 5px;
    margin-top: 20px;
    padding: 100px;
    background: #7d7d7d;
    border-radius: 30px;
    margin-left: 10px;
    margin-right: 10px;

    font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
    font-size: 50px;
    color: white;
}

.flex-child:first-child {
    display: flex;
    justify-content: center;
    margin: 5px;
    margin-top: 20px;
    padding: 100px;
    background: #7d7d7d;
    border-radius: 30px;
    margin-right: 10px;
    margin-left: 10px;

    font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
    font-size: 50px;
    color: white;
}
<div class="flex-container">
  <div class="flex-child"></div>
  <div class="flex-child"></div>
  <div class="flex-child"></div>
  <div class="flex-child"></div>
</div>
ROOT
  • 11,363
  • 5
  • 30
  • 45

1 Answers1

2

You should use flex-wrap CSS attribute, using it on flex display will force the elements to go new line when ever there is no space for more elements in one line:

.flex-container {
    display: flex;
    flex-wrap: wrap;
}

.flex-child {
    border: 2px solid yellow;
    justify-content: center;
    margin: 5px;
    margin-top: 20px;
    background: #7d7d7d;
    border-radius: 30px;
    margin-left: 10px;
    margin-right: 10px;
    font-family: 'Ubuntu', Helvetica, Arial, sans-serif;
    color: white;
    width: calc(50% - 24px);
    height: 200px;
}
<div class="flex-container">
  <div class="flex-child"></div>
  <div class="flex-child"></div>
  <div class="flex-child"></div>
  <div class="flex-child"></div>
</div>
ROOT
  • 11,363
  • 5
  • 30
  • 45