If you're anything like me, vertically and horizontally centering an HTML element has been the bane of your existence at some point in your career.
Luckily, this solution works in every browser, even IE 11:
display: flex;
justify-content: center;
align-items: center;
justify-content
is for horizontal alignment by default. Removing it or setting justify-content: flex-start;
places the child element on the left. Setting justify-content: flex-end;
places the child element on the right.
align-items
is for vertical alignment by default. Removing it or setting align-items: flex-start;
places the child element at the top. Setting align-items: flex-end;
places the child element at the bottom.
Here's an example.
You can probably stop using padding
, absolute
left/right and top/bottom positioning, float
, line-height
, transform
, and any other tricks :)
Funny story, I didn't realize someone else also wrote a short article about flexbox earlier today 😂