I have set up a flexbox with two items which should be on the left and right on a normal screen.
If the screen is not big enough to show both next to each other, it should wrap but then they should be centered and not aligned on the left.
I tried different solutions (like using margin: auto
on the child items) but that aligned them more to the center even when in the same row.
Here is a simplified example:
.container {
display: flex;
align-items: flex-end;
flex-wrap: wrap;
justify-content: space-between;
border: 1px solid black;
margin-bottom: 25px;
}
.large {
width: 500px;
}
.small {
width: 175px;
}
.item {
width: 100px;
height: 100px;
background-color: blue;
margin: 25px 0px;
}
<div class="container large">
<div class="item"></div>
<div class="item"></div>
</div>
<div class="container small">
<div class="item"></div>
<div class="item"></div>
</div>
https://jsfiddle.net/SoWhy/zfx4ub8x/
The first container is the intended positioning, the second container emulates the same container on a smaller screen, note the align to the left.
Is there a way to define that the flexbox is to align items differently when wrapped or can I only do it using the "@media screen" method (which requires me to set a certain size and is thus not flexible if the size of the items changes)?
You need to use the justify-content property to center flex items along the main axis. Its default value is flex-start which aligns all the items inside the flex container to the beginning of the main axis. If you want to center the items you need to use it with the center value.
The flex-wrap CSS property sets whether flex items are forced onto one line or can wrap onto multiple lines. If wrapping is allowed, it sets the direction that lines are stacked.
Making things wrap If you want to cause them to wrap once they become too wide you must add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap in the container.
This may not work in your case, but it's worth mentioning this lesser-known difference between justify-content: space-between
and justify-content: space-around
.
From the flexbox specification:
8.2. Axis Alignment: the
justify-content
propertyThe
justify-content
property aligns flex items along the main axis of the current line of the flex container.
There are five values that apply to justify-content
. Here are two of them:
space-between
Flex items are evenly distributed in the line.
If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to
flex-start
.
This explains why your items align left on wrap with space-between
.
Now look at space-around
:
space-around
Flex items are evenly distributed in the line, with half-size spaces on either end.
If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to
center
.
Hence, to center-align flex items on wrap, consider using space-around
.
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-around; /* ADJUSTMENT */
border: 1px solid black;
}
.item {
width: 200px;
height: 100px;
background-color: blue;
margin: 25px;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
</div>
Revised Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With