# css – How to justify a single flexbox item (override justify-content)

## The Question :

324 people think this question is useful

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?

This was best answered by this post: https://stackoverflow.com/a/33856609/269396

606 people think this answer is useful

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.

20 people think this answer is useful

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.

13 people think this answer is useful

If you aren’t actually restricted to keeping all of these elements as sibling nodes you can wrap the ones that go together in another default flex box, and have the container of both use space-between.

.space-between {
border: 1px solid red;
display: flex;
justify-content: space-between;
}
.default-flex {
border: 1px solid blue;
display: flex;
}
.child {
width: 100px;
height: 100px;
border: 1px solid;
}

<div class="space-between">
<div class="child">1</div>
<div class="default-flex">
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
</div>
</div>


Or if you were doing the same thing with flex-start and flex-end reversed you just swap the order of the default-flex container and lone child.

1 people think this answer is useful

For those situations where width of the items you do want to flex-end is known, you can set their flex to “0 0 ##px” and set the item you want to flex-start with flex:1

This will cause the pseudo flex-start item to fill the container, just format it to text-align:left or whatever.

0 people think this answer is useful

I solved a similar case by setting the inner item’s style to margin: 0 auto.
Situation: My menu usually contains three buttons, in which case they need to be justify-content: space-between. But when there’s only one button, it will now be center aligned instead of to the left.

0 people think this answer is useful

To expand on Pavlo’s answer https://stackoverflow.com/a/34063808/1069914, you can have multiple child items justify-content: flex-start in their behavior but have the last item justify-content: flex-end

.container {
height: 100px;
border: solid 10px skyblue;
display: flex;
justify-content: flex-end;
}

.container > *:not(:last-child) {
margin-right: 0;
margin-left: 0;
}

/* set the second to last-child */
.container > :nth-last-child(2) {
margin-right: auto;
margin-left: 0;
}

.block {
width: 50px;
background: tomato;
border: 1px solid black;
}

<div class="container">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block" style="width:150px">I should be at the end of the flex container (i.e. justify-content: flex-end)</div>
</div>

Tags:,