Arrange 2 items per row using flexbox
You can give flex: 50%
to children div
s 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>
How to display 2 columns per row using flexbox
The best way to achieve this layout would be with Grid CSS:
.flex { display: grid; grid-auto-flow: column; grid-gap: 20px; grid-template-rows: 100px 100px; grid-template-columns: 100px 100px; padding: 10px;}
.box { padding: 20px; border: 1px solid black;}
.red { background-color: red;}
.yellow { background-color: yellow;}
.blue { background-color: blue;}
.green { background-color: green;}
* { box-sizing: border-box;}
<div class="flex"> <div class="box green">positive 1</div> <div class="box yellow">positive 2</div> <div class="box blue">positive 3</div> <div class="box red">negative 1</div></div>
Flexbox - Cater for 2,3 or 1 items per row
you can use flex-basis
along with flex-grow: 1
for that:
.grid3 { display: flex; flex-wrap: wrap;}
.grid3-item { flex: 1 0 33%; text-align: center; border: 1px solid red; box-sizing: border-box}
<div class="grid3"> <div class="grid3-item">Some Content</div> <div class="grid3-item">Some Content</div> <div class="grid3-item">Some Content</div> <div class="grid3-item">Some Content</div> <div class="grid3-item">Some Content</div></div>
How to display a decreasing number of images per row
You can use flexbox and flex like below:
.gallery {
display: flex;
flex-wrap: wrap;
}
.gallery > img {
flex: calc(100%/3); /* 3 images per row */
min-width: 0;
outline:1px solid red;
}
/* until the 9th 4 images per row */
.gallery > :nth-child(-n + 9) {
flex: calc(100%/4);
}
/* until the 5th 5 images per row */
.gallery > :nth-child(-n + 5) {
flex: calc(100%/5);
}
<div class="gallery">
<img src="https://picsum.photos/id/1058/400/200">
<img src="https://picsum.photos/id/1018/400/200">
<img src="https://picsum.photos/id/1016/400/200">
<img src="https://picsum.photos/id/1058/400/200">
<img src="https://picsum.photos/id/1018/400/200">
<img src="https://picsum.photos/id/1016/400/200">
<img src="https://picsum.photos/id/1058/400/200">
<img src="https://picsum.photos/id/1018/400/200">
<img src="https://picsum.photos/id/1016/400/200">
<img src="https://picsum.photos/id/1058/400/200">
<img src="https://picsum.photos/id/1018/400/200">
<img src="https://picsum.photos/id/1016/400/200">
</div>
How to display 3 items per row in flexbox?
Flex container:
- You probably want to use
display: flex
notinline-flex
. - Add
flex-wrap: wrap
to allow wrapping onto multiple lines. - Remove
width: 33%
if you wish it to take entire space avaiable.
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 meansflex-basis: 33.333333%
.
.serv ul { display: flex; flex-wrap: wrap; padding-left: 0;}
.serv ul li { list-style: none; flex: 0 0 33.333333%;}
<div class="serv"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul></div>
Related Topics
How to Put Text Over an Image Without Absolute Positioning or Setting The Image as Backbround
Grow Height of Parent Div That Contains Floating Nested Divs
How to Get a Div to Resize Its Height to Fit Container
Text-Overflow: Ellipsis in Table-Cell Without Width
Opening a New Tab to Read a PDF File
How to Detect Click Outside of an Element in Angular
How to Make Rectangular Image Appear Circular with CSS
What's The Maximum Number of Simultaneous Connections a Browser Will Make
How to Style a Horizontal List with Bullets Between Items Using CSS
To "User-Scalable=No" or Not to "User-Scalable=No"
The Ajax Response: Data (JSON, Xml) or HTML Snippet
Why Are 3-Digit Hex Color Code Values Interpreted Differently in Internet Explorer
Delete HTML Tags in Sed or Similar
HTML: Should I Encode Greater Than or Not? ( > > )
Onserverclick Event Handler Not Called If Using Onclick
Angular 2 Use a "Template" for Ng-Content to Use Inside Component Loop