Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to justify a single flexbox item (override justify-content) [duplicate]

Tags:

css

flexbox

You can override align-items with align-self for a flex item. I am looking for a way to override justify-content for a flex item.

If you had a flexbox container with justify-content:flex-end, but you want the first item to be justify-content: flex-start, how could that be done?

like image 337
Mahks Avatar asked May 13 '14 01:05

Mahks


People also ask

Why is justify-self ignored in flexbox layouts?

To add to this answer, justify-self is simply not supported in flexbox because it justifies all its items as a group.

Can I use justify content space evenly?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.

What can I use instead of justify content?

You may use margin instead align-items and/or justify-content .


2 Answers

There doesn't seem to be justify-self, but you can achieve similar result setting appropriate margin to auto¹. E. g. for flex-direction: row (default) you should set margin-right: auto to align the child to the left.

.container {    height: 100px;    border: solid 10px skyblue;        display: flex;    justify-content: flex-end;  }  .block {    width: 50px;    background: tomato;  }  .justify-start {    margin-right: auto;  }
<div class="container">    <div class="block justify-start"></div>    <div class="block"></div>  </div>

¹ This behaviour is defined by the Flexbox spec.

like image 151
Pavlo Avatar answered Sep 21 '22 03:09

Pavlo


AFAIK there is no property for that in the specs, but here is a trick I’ve been using: set the container element ( the one with display:flex ) to justify-content:space-around Then add an extra element between the first and second item and set it to flex-grow:10 (or some other value that works with your setup)

Edit: if the items are tightly aligned it's a good idea to add flex-shrink: 10; to the extra element as well, so the layout will be properly responsive on smaller devices.

like image 40
eeglbalazs Avatar answered Sep 20 '22 03:09

eeglbalazs