Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show three columns per row?

Tags:

html

css

flexbox

I have been trying to show three columns per row. Is it possible using flexbox?

My current CSS is something like this:

.mainDiv {     display: flex;     margin-left: 221px;     margin-top: 43px; } 

This code puts all content in a single row. I want to add a constraint to just shows three records per row.

like image 585
maxspan Avatar asked Jun 13 '14 02:06

maxspan


People also ask

How do you display 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 3 columns in HTML?

Simply create a <div> element inside your HTML document, and by using this syntax, you are going to divide the content into three columns.

How do you make 3 divs in one row?

Three or more different div can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side.


2 Answers

This may be what you are looking for:

http://jsfiddle.net/L4L67/

body>div {    background: #aaa;    display: flex;    flex-wrap: wrap;  }    body>div>div {    flex-grow: 1;    width: 33%;    height: 100px;  }    body>div>div:nth-child(even) {    background: #23a;  }    body>div>div:nth-child(odd) {    background: #49b;  }
<div>    <div></div>    <div></div>    <div></div>    <div></div>    <div></div>    <div></div>  </div>
like image 152
Barun Avatar answered Sep 20 '22 14:09

Barun


Even though the above answer appears to be correct, I wanted to add a (hopefully) more readable example that also stays in 3 columns form at different widths:

.flex-row-container {      background: #aaa;      display: flex;      flex-wrap: wrap;      align-items: center;      justify-content: center;  }  .flex-row-container > .flex-row-item {      flex: 1 1 30%; /*grow | shrink | basis */      height: 100px;  }    .flex-row-item {    background-color: #fff4e6;    border: 1px solid #f76707;  }
<div class="flex-row-container">    <div class="flex-row-item">1</div>    <div class="flex-row-item">2</div>    <div class="flex-row-item">3</div>    <div class="flex-row-item">4</div>    <div class="flex-row-item">5</div>    <div class="flex-row-item">6</div>  </div>

Hope this helps someone else.

like image 33
RoboBear Avatar answered Sep 20 '22 14:09

RoboBear