Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex items evenly spaced but first item aligned left

Tags:

css

flexbox

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)

like image 806
drake035 Avatar asked Mar 23 '17 12:03

drake035


People also ask

How do you center a Flex container but left align Flex items?

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 .

How do you evenly space flex items?

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.

How do you align Flex items individually?

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.

What is space evenly in Flex?

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.


2 Answers

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>

jsFiddle demo

Learn more about flex auto margins here: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

like image 89
Michael Benjamin Avatar answered Oct 27 '22 00:10

Michael Benjamin


You can use justify-content: space-between, but the last content will have also no space on the right.

A good documentation.

like image 42
Shiva127 Avatar answered Oct 27 '22 00:10

Shiva127