Flexbox's justify-content: space-around
makes my list items horizontally evenly spaced. Is there a way to have exactly the same thing with the only difference that the first item on the left has no space on its left? (that is, the list "starts" from the left edge of the container)
Using the code posted in the question, we could create a new flex container that wraps the current flex container ( ul ), which would allow us to center the ul with justify-content: center . Then the flex items of the ul could be left-aligned with justify-content: flex-start .
In fact, all major browsers consider pseudo-elements on a flex container to be flex items. Knowing that, add ::before and ::after to your container. With justify-content: space-between and zero-width pseudo-elements, the visible flex items will appear evenly spaced.
If you do want to align one item, or split a group on the main axis, use auto margins to do so; The align-items property sets all of the align-self values as a group. Use align-self on the flex child to set the value for an individual item.
space-evenly is a value that can be assigned to the justify-content property to distribute flex items in such a way that the items have equal space around them.
Instead of using justify-content: space-around
, use auto
margins on the items.
By giving each flex item margin-right: auto
, container space will be distributed evenly between items (like justify-content
), but the first item will remain at the left border edge.
flex-container[one] {
display: flex;
justify-content: space-around;
border: 1px dashed green;
}
flex-container[one]>flex-item {
background-color: lightgreen;
}
flex-container[two] {
display: flex;
border: 1px dashed red;
}
flex-container[two]>flex-item {
margin-right: auto;
background-color: orangered;
}
flex-item {
width: 50px;
height: 50px;
}
<code>justify-content: space-around</code>
<flex-container one>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
</flex-container>
<br>
<code>margin-right: auto</code>
<flex-container two>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
</flex-container>
Learn more about flex auto
margins here: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
You can use justify-content: space-between
, but the last content will have also no space on the right.
A good documentation.
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