How do I right align div elements?
To right-align divs, leverage the power of flexbox. Apply display: flex; and justify-content: flex-end; to the parent container:
Now, place your div in the container and let the magic happen:
This nudges the div to the far-right edge in a snappy, responsive manner.
Beyond the basics
Shifting unwanted space with flex-grow
Within a flex container, the flex-grow property comes in handy. It swallows additional space and pushes div elements to the right
Handling the sibling rivalry (stacking)
For multiple divs, use flex-wrap: wrap; to prevent sibling divs from invading each other's space:
Responsive solution with flexbox
Flexbox also does a remarkable job when it comes to dynamic width and responsive design.
Right-align divs without flex
Flexbox not your cup of tea? Worry not, there's more than one way to skin a cat.
Inline-block with text-align
Auto margin magic
Overflow handling for the float fans
For those who love floats, handle overflow issues using a clear-fix:
Other alignment techniques
Positioning to the rescue
Absolutely positioned elements can be placed right:
Evenly spacing items
For an evenly distributed layout, justify-content: space-around; is your friend:
Handling IE blues
Cater for IE-related issues with these quick tips:
- Be aware of the IE Double Margin Bug
- Use
display: table-cell;andvertical-align: middle;for vertical centering when flexbox isn't acceptable - Substitute
flexwithdisplay: inline-block;andtext-align: right;to align divs on older IE versions
Was this article helpful?