I have a div that is vertically centered using flex box. But is there now any way to align it 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.
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.
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>
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