zzz6519003 / blog

My blog about coding
4 stars 1 forks source link

How to center an element horizontally and vertically? #81

Open zzz6519003 opened 7 years ago

zzz6519003 commented 7 years ago

How to center an element horizontally and vertically? Ask Question

http://stackoverflow.com/questions/19461521/how-to-center-an-element-horizontally-and-vertically

Approach 1 - transform translateX/translateY:

Approach 2 - Flexbox method:

Approach 3 - table-cell/vertical-align: middle:

Approach 4 - Absolutely positioned 50% from the top with displacement:

Approach 5 - The line-height method (Least flexible - not suggested):

Horizontally Is it inline or inline-* elements (like text or links)? Is it a block level element? Is there more than one block level element?

Vertically Vertical centering is a bit trickier in CSS. Is it inline or inline-* elements (like text or links)? Is it a block-level element?

Both Horizontally and Vertically You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps: Is the element of fixed width and height? Is the element of unknown width and height? Can you use flexbox?

Conclusion You can totally center things in CSS.

See www.medium.com/@Snowmanzzz/how-to-center-an-element-horizontally-and-vertically-54129e20ebe7