sanjar-notes / web_dev_fundamentals

The basics - HTML, CSS, Javascript and more
https://sanjar-notes.github.io/web_dev_fundamentals/
3 stars 0 forks source link

How to Align Last Flex Item to the Right #83

Open sanjarcode opened 1 year ago

sanjarcode commented 1 year ago

Situation: Suppose there's a horizontal flexbox with some items. We wish to align all items to the left, except the last element which should be aligned to the right.

It's known that justify-self doesn't work in a Flexbox. But there's still a way to achieve this effect. Add the following styles to the last item:

#last-item {
  margin-left: auto; 
}

/* OR equivalently - do this from the flex container itself */
#my-flex-container:last-child {
  margin-left: auto;
}

Soruce: https://medium.com/@iamryanyu/how-to-align-last-flex-item-to-right-73512e4e5912