Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange 2 items per row using flexbox

Tags:

html

css

flexbox

Imagine I have following markup

<div class="container">     <div class="item"></div>     <div class="item"></div>     <div class="item"></div>     <div class="item"></div> </div> 

and style

.item {   width: 100% } 

and due to certain reasons I can't change the width of the .item Can I arrange 2 items in each row by styling parent container .container, using flexbox or any other way?

like image 433
PranavPinarayi Avatar asked Jul 11 '17 14:07

PranavPinarayi


People also ask

How do you get 3 items per row on flexbox?

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 you display 4 items per row in flexbox?

Here's another way without using calc() . // 4 PER ROW // 100 divided by 4 is 25. Let's use 21% for width, and the remainder 4% for left & right margins... . child { margin: 0 2% 0 2%; width: 21%; } // 3 PER ROW // 100 divided by 3 is 33.3333...

How do I arrange items in flexbox?

Justify Content Flex Start positions element at the start of the page. Flex End sets the element to the end of the page. Space Around arranges the items evenly but with spaces between them. The spaces will be equal among all the elements inside a flex-box container, but not outside them.


2 Answers

You can give flex: 50% to children divs without touching .item

.item {   width: 100% }  .container {   display: flex;   flex-wrap: wrap; }  .container > div {   flex: 50%; /* or - flex: 0 50% - or - flex-basis: 50% - */   /*demo*/   box-shadow: 0 0 0 1px black;   margin-bottom: 10px; }
<div class="container">   <div class="item">1</div>   <div class="item">2</div>   <div class="item">3</div>   <div class="item">4</div> </div>
like image 58
dippas Avatar answered Sep 22 '22 07:09

dippas


The below solution worked for me

.parent{   display: flex;   flex-wrap: wrap; }  .child{   width: 25%;   box-sizing: border-box; } 

Sample: https://codepen.io/capynet/pen/WOPBBm

like image 40
Deepak Terse Avatar answered Sep 22 '22 07:09

Deepak Terse