How to wrap last row of flexbox with "space-around"? [duplicate] - html

This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Closed 3 years ago.
I am trying to create a Bootstrap-like grid of cards using CSS flex properties. I have 10 cards (4 each row). However, when using justify-content: space-around (this is what I need), the last row is not aligning with the others (naturally). What is the workaround?
.card-gallery {
width: 100%;
height: auto;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
align-items: center;
flex-direction: row;
}
.card-box {
width: 270px;
height: 400px;
background: red;
}
<div class="container">
<div class="card-gallery">
<div class="card-box"></div>
<div class="card-box"></div>
<div class="card-box"></div>
<div class="card-box"></div>
<div class="card-box"></div>
<div class="card-box"></div>
<div class="card-box"></div>
</div>
</div>

You could try CSS :last-child as in the example below to use different style in your last div.
.flexbox {
display: flex;
flex-flow: column;
}
.flex-child {
background: red;
align-content: center;
align-self: center;
}
.flex-child:last-child {
align-content: space-around;
align-self: flex-start;
}
<div class="flexbox">
<div class="flex-child">
one
</div>
<div class="flex-child">
two
</div>
<div class="flex-child">
three
</div>
</div>
By the way space-between and applying relative width to your card div works better.
.card-gallery {
width: calc(100% -2rem);
height: auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
flex-direction: row;
padding: 1rem;
}
.card-box {
width: 45%;
height: 40px;
background: #ff0;
align-self: center;
}
div.card-box:last-child {
align-self: flex-start;
}
<div class="container">
<div class="card-gallery">
<div class="card-box">1</div>
<div class="card-box">2</div>
<div class="card-box">3</div>
<div class="card-box">4</div>
<div class="card-box">5</div>
<div class="card-box">6</div>
<div class="card-box">7</div>
</div>
</div>

You can't achieve this easily with flexbox.
Instead, set .card-gallery to display:grid to make sure the cards always line up in a grid formation when multiple rows exist. Then give it this style rule:
grid-template-columns: repeat(auto-fill, 120px)
The auto-fill keyword will make sure the items will wrap when the viewport shrinks:
.container{max-width:479px;background:lightblue}
.card-gallery {
width: 100%;
height: auto;
display: grid;
grid-template-columns: repeat(auto-fill, 120px);
justify-content: space-around;
}
.card-box {
height: 100px;
background: #fff;
margin-bottom:30px;
border:solid 2px dodgerblue
}
<div class="container">
<div class="card-gallery">
<div class="card-box">cb</div>
<div class="card-box">cb</div>
<div class="card-box">cb</div>
<div class="card-box">cb</div>
<div class="card-box">cb</div>
<div class="card-box">cb</div>
<div class="card-box">cb</div>
</div>
</div>

Related

Is there a way to combine flex-start with center in css [duplicate]

This question already has answers here:
How to center a flex container but left-align flex items
(9 answers)
Closed 6 months ago.
If I do justify-content: center; cards are centered, but it looks quite rough if there aren't an even number of cards. So, I did justify-content: flex-start; and now I need to center them while are aligned left, I don't want the negative space, is it possible to do so with flexbox?
<section>
<div class="wrap">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</section>
section {
width: 100%;
display: flex;
justify-content: center;
}
.wrap {
display: flex;
flex-wrap: wrap;
width: 80%;
justify-content: flex-start;
}
.card {
width: 300px;
height: 200px;
}
Want it like this:
Instead of this:
Consider grid:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
section {
width: 100%;
display: flex;
justify-content: center;
}
.wrap {
display: flex;
flex-wrap: wrap;
width: 80%;
justify-content: center;
}
.consider_grid {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 20px;
row-gap: 10px;
}
.card {
background-color: pink;
width: 300px;
height: 200px;
}
<section>
<div class="wrap">
<div class="consider_grid">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>
</section>

Shrink flexbox to the size of its children

I need to arrange a bunch of blocks of fixed size into a grid. I am using flex-wrap: wrap to get as many in each row as will fit. But, I would like this whole wrapped column to be centered in the page. Ideally, I would like it to look something like this:
But, in the snippet I have below, the green flexbox fills to fit any space it can, so all the blue boxes are pushed all the way to the left. I don't want to set the flexbox to a fixed width because I want it to be able to flow to fit as many boxes in a row as possible, but I just don't want it to take up any more space beyond that.
Obviously, I could use justify-content: center to center the boxes within the container, but the incomplete row at the bottom gets out of alignment, which I don't want.
Is there any way to achieve the effect I am looking for with CSS?
I saw this question which suggested using display: inline-flex. That does seem to work when you are not wrapping, but as soon as you put on flex-wrap: wrap and add enough items to make it wrap, it jumps back to filling the full width.
.page {
width: 100%;
background-color: pink;
padding: 10px;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
}
.flex-column {
display: flex;
flex-direction: row;
flex-wrap: wrap;
background-color: green;
justify-content: flex-start;
gap: 10px;
}
.flex-child {
display: block;
width: 200px;
height: 100px;
background-color: blue;
}
.button {
display: inline-block;
padding: 20px;
background-color: red;
}
.
<div class='page'>
<div class="flex-column">
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
</div>
<div class="button">Load more</div>
</div>
Since the width is always the same, CSS grid can help you here:
.page {
background-color: pink;
display: grid;
grid-template-columns:repeat(auto-fit,200px); /* same width as child */
gap:10px; /* the same gap here */
justify-content:center; /* center everything */
}
.flex-column {
display: flex;
flex-direction: row;
flex-wrap: wrap;
background-color: green;
grid-column:1/-1; /* take all the columns */
gap: 10px;
}
.flex-child {
width: 200px;
height: 100px;
background-color: blue;
}
.button {
padding: 20px;
background-color: red;
grid-column:1/-1; /* take all the columns */
margin:auto; /* center the button */
}
<div class='page'>
<div class="flex-column">
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
</div>
<div class="button">Load more</div>
</div>
In mine it is working like this you can see:
.page {
width: 100%;
background-color: pink;
padding: 10px;
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
}
.flex-column {
display: flex;
flex-direction: row;
flex-wrap: wrap;
background-color: green;
justify-content: flex-start;
gap: 10px;
display: flex;
padding: 20px;
}
.flex-child {
display: block;
width: 200px;
height: 100px;
background-color: blue;
flex: 0 1 auto;
}
.button {
display: inline-block;
padding: 20px;
background-color: red;
}
.
<div class='page'>
<div class="flex-column">
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
<div class="flex-child"></div>
</div>
<div class="button">Load more</div>
</div>

Custom gallery layout with flexbox [duplicate]

This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 1 year ago.
I'm trying to create this style of gallery layout with flexbox, but running into some trouble.
So far my code is putting two small ones and the large one in the top row, and then the second small ones on the bottom row.
Something like this.
.container {
display: flex;
justify-content: space-between;
flex-direction: row;
}
.item {
width: 100%;
margin: 1em;
}
.content {
border: 1px solid black;
margin-bottom: 1em;
height: 100%;
}
<div class="container">
<div class="item">
<div class="content">half</div>
<div class="content">half</div>
</div>
<div class="item content">full</div>
<div class="item">
<div class="content">half</div>
<div class="content">half</div>
</div>
</div>
Html
<div class"parent">
<div class="leftContent"></div>
<div class="centerContent"></div>
<div class="rightContent"></div>
</div>
Css
.parent{
width: 100%;
display: flex;
justify-content: space-between;
align-items: stretch;
}
.leftContent{
display: flex;
justify-content: space-between;
height: 100%;
flex-direction: column;
align-items: stretch;
}
.rightContent{
display: flex;
justify-content: space-between;
height: 100%;
flex-direction: column;
align-items: stretch;
}
add now the contents in the divs. i think this should work

How to align the flex items in three different ways?

So, I want to align items inside flex-box like in the above picture.
I have html as follows:
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>
css as
.flex-row {
min-height: 400px;
display: flex;
align-items: flex-start;
}
.flex-item:nth-child(2) {
align-items: center;
}
.flex-item:last-child {
align-items: flex-end;
}
Use align-self not align-items
.flex-row {
min-height: 300px;
display: flex;
width: 90%;
margin: 1em auto;
border: 1px solid red;
justify-content: space-between;
}
.flex-item {
height: 50px;
background: lightblue;
width: 100px;
}
.flex-item:nth-child(2) {
align-self: center;
}
.flex-item:last-child {
align-self: flex-end;
}
<div class="container flex-row">
<div class="flex-item"></div>
<div class="flex-item"></div>
<div class="flex-item"></div>
</div>
Like so?
You need flex-wrap: wrap; In order to wrap the items need to be wide enough (100%). Since 100% flex item is always justified to the full width, you need text-align, not align-items which is the wrong property in the first place.
align-items is the alignment at the cross axis, not the main axis which would be justify-content
.flex-row {
min-height: 400px;
display: flex;
align-items: flex-start;
flex-wrap: wrap;
}
.flex-item {
flex-basis: 100%;
}
.flex-item:nth-child(2) {
text-align: center;
}
.flex-item:last-child {
text-align: right;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
<div class="flex-item">
<img src="/default.png" width="100" height="50">
</div>
</div>
Look at this code
display:flex;
flex-direction: row;
justify-content: flex-start;
align-items: centre;
With flex-direction you specify le principal alignment (horizontal with row , vertical with column). Depending on what you choose here the following properties will have a different effect.
Then with justify-content you specify how your items inside your elements should align on the principal axis you just defined (vertical or horizontal).
In this case flex-start means at the beginning of my axis.
And with align-items you specify the alignment for the secondary axe.The vertical one because we choose flex-direction: row; and not flex-direction: column;. So in the vertical axis items will align at centre
And don't forget to take look at the available properties like space-between or flex-end
NOTE!
to set height to column change the px here:flex: 0 1 50px; or remove it!
Using margin-right/left:auto as:
.flex-row {
min-height: 400px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.flex-item {
flex: 0 1 50px;
}
.flex-item:nth-child(2) {
margin-right: auto;
margin-left: auto;
}
.flex-item:last-child {
margin-left: auto;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>
2. using align-self
.flex-row {
min-height: 400px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.flex-item {
flex: 0 1 50px;
align-self: flex-start;
}
.flex-item:nth-child(2) {
align-self: center;
}
.flex-item:last-child {
align-self: flex-end;
}
<div class="flex-row">
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
<div class="flex-item">
<img src="/default.png">
</div>
</div>

Flexbox centeres items with same height

I ceated a flexbox grid and tried to make each item the same hight which is not working. Basically all those blue containers shall have the same height.
HTML:
<div class="outer">
<div class="item">
<h1>Contact</h1>
</div>
<div class="item">
<h3>Mail:</h3>
<a>john#doe.com</a>
<br>
<a>PGP</a>
</div>
<div class="item">
<h3>Telegram:</h3>
<a>www.t.me/doe</a>
</div>
</div>
CSS:
.outer {
display: flex;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
height: 100vh;
width: 100vw;
}
.item {
background-color: aqua;
flex: 1;
}
Photo:
I would add another element that is the only flex child of .outer, centered vertically, with the background applied to it, then make that element the flex parent that holds your 3 sections and aligns them.
.outer, .inner {
display: flex;
align-items: center;
}
.outer {
height: 100vh;
width: 100vw;
}
.inner {
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
flex: 1 0 0;
background-color: aqua;
}
.item {
flex: 1;
}
<div class="outer">
<div class="inner">
<div class="item">
<h1>Contact</h1>
</div>
<div class="item">
<h3>Mail:</h3>
<a>john#doe.com</a>
<br>
<a>PGP</a>
</div>
<div class="item">
<h3>Telegram:</h3>
<a>www.t.me/doe</a>
</div>
</div>
</div>