When searching the web for ways to center a DIV I found a ton of suggestions. Many kind of solved one problem but at the same time introduced others, some made the code unreadable and others straight up didn’t work at all.
Eventually I got frustrated and decided to tinker around for a couple hours myself on a problem that seems like there should be a native solution for it - but there isn’t.
My Goals
intuitive code - no complex conditions I would understand 6 months down the road
Unintrusive markup - no weird structures
Usable on top level DIVs as well as on nested ones
The Code
|
|
I’m leveraging the powers of Flexbox for this task. For this to work, each parent of the ‘.center’ed element needs to have the ‘position’ attribute set (or else the ‘absolute’ value will use the body as reference). Adding a margin of 40px to top and bottom of each centered descendant makes for better visuals, you may remove that if you like.
The CSS-rule regarding ‘.center::before’ and ‘.center::after’ is the heart of this setup. It’s the best I could find for making sure vertical alignment works without flaws.
To better understand how it’s used, let’s look at an example markup:
|
|
See? Very not-complex implementation, easy on the coder’s eyes!
Horizontally only
As a bonus, here’s another class centering only horizontally:
|
|
There’s really no magic to this one, as it’s mostly based on basic Flexbox but it’s a convenient class to have nonetheless!
Conclusion
The search is over! Here are your classes to handle centering of DIVs easily.