Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align a vertical centered flex element to the right

Tags:

css

flexbox

I have a div that is vertically centered using flex box. But is there now any way to align it to the right?

like image 262
Jonathan Avatar asked Apr 11 '15 15:04

Jonathan


People also ask

How do you align the flex element to the right?

In simple words, flex-items now expand from right to left as shown in the given figure. When justify-content is set to “flex-end”, it instantly shifts all the flex-items to the end of the flex-container along the main-axis, i.e flex items get right aligned.

Does vertical align work with Flex?

With Flexbox, you can stop worrying. You can align anything (vertically or horizontally) quite painlessly with the align-items , align-self , and justify-content properties.


1 Answers

One option would be to add justify-content: flex-end to the flexbox container: (example)

.parent {
    display: flex;
    width: 200px;
    height: 200px;
    border: 1px solid;
    align-items: center;
    justify-content: flex-end;
}
.parent > .child {
    width: 50%;
    height: 50%;
    border: 2px solid #f00;
}
<div class="parent">
    <div class="child"></div>
</div>

Alternatively, you could also add margin-left: auto to the flexbox item: (example)

.parent {
    display: flex;
    width: 200px;
    height: 200px;
    border: 1px solid;
    align-items: center;
}
.parent > .child {
    width: 50%;
    height: 50%;
    border: 2px solid #f00;
    margin-left: auto;
}
<div class="parent">
    <div class="child"></div>
</div>
like image 194
Josh Crozier Avatar answered Oct 21 '22 10:10

Josh Crozier