This is a pointless post. Use the TABLE tag. Yes, it will violate the purity of CSS, but you can trust it, and it will make your day better because it will free you angst and from extensive cross-browser testing. Simple is better than complex. Straightforward is better than arcane.
As igvadaimon wrote below, there's no need for a tag. Instead of: my div you can do my div The inline styling is just for illustrative purposes, of course.
The complete guide to centering a div
21–30 of 170 posts
Re: The complete guide to centering a div
#22What about cases in which developer wants to vertically center a div, but he or she doesn't want to specify a height property?
Then unfortunately you have to calculate it with JS if it varies. Vertically centering divs has always been a gigantic pain-in-the-ass. If anyone has a better idea for doing this without JS, please let me know. Once CSS implements variable-based elements (ie #div.height) then we'll have a lot more control.
Flexbox, with optional fallback to JS for IE 8 and 9. That said, vertical centering is almost always a nice-to-have rather than a must-have so personally I wouldn't even use a fallback.
http://philipwalton.github.io/solved-by-flexbox/demos/vertic...
Re: The complete guide to centering a div
#23I don't know anything about the history of CSS's design, but I'm perpetually amazed that positioning (and specifically centering) requires so much CSS magic. I would think CSS positioning would be a one-liner, but instead it's a collection of context-dependent, browser-dependent recipes. I wonder if there's a good reason for the madness, or if it's just poor design. (FWIW, I'm a backend engineer and have only dabbled…
Well the reason is that CSS originally wasn't intended for layout….
But since there was no other tool for layout, once the web design community decided to remove tables from layout it got drafted despite being severely insufficient. Layout modules have just started being developed, fought upon and implemented in the last few years. Here's how vertical centering looks like when using flexbox (http://philipwalton.github.io/solved-by-flexbox/demos/vertic...):
.parent {
display: flex;
align-items: center;
justify-content: center;
}
.centered {
max-width: 50%;
}
(note: this ignores prefixing, specs differences and browser-specific issues, here's the component https://github.com/philipwalton/solved-by-flexbox/blob/maste... which is itself expanded to suit browsers)Re: The complete guide to centering a div
#24I was wondering why the demo is not working for me. it really took me a while to realize that it is actually a button.
Re: The complete guide to centering a div
#25Earlier quoted context omitted.
As igvadaimon wrote below, there's no need for a tag. Instead of: my div you can do my div The inline styling is just for illustrative purposes, of course.
I'll admit that the table-cell approach is nice -- thank you. But how curious that the article, which purports to be an in-depth examination, doesn't mention it. And how curious that the table-cell approach has not turned up in the numerous times I've gone looking on-line for a robust, pure CSS solution. I've long harbored suspicions that many CSS gurus are too quick to adopt complex solutions.
On the other hand support for centering vertically with auto isn't/wasn't widely supported either.
Re: The complete guide to centering a div
#26Earlier quoted context omitted.
As igvadaimon wrote below, there's no need for a tag. Instead of: my div you can do my div The inline styling is just for illustrative purposes, of course.
I'll admit that the table-cell approach is nice -- thank you. But how curious that the article, which purports to be an in-depth examination, doesn't mention it. And how curious that the table-cell approach has not turned up in the numerous times I've gone looking on-line for a robust, pure CSS solution. I've long harbored suspicions that many CSS gurus are too quick to adopt complex solutions.
[0] http://philipwalton.github.io/solved-by-flexbox/demos/vertic...
[1] http://css-tricks.com/centering-in-the-unknown/
[2] http://gtwebdev.com/workshop/vcenter/vcenter-inline-css.php
Re: The complete guide to centering a div
#27> Centering a div Within a div, Horizontally and Vertically The code in this section is incorrect. "margin: auto" does nothing in the vertical direction. If you set the height of the outer div to something more than 160 px, you will see that the inner div is not centered.
Here's the Codepen that shows it with a larger outer div,
Re: The complete guide to centering a div
#28I don't know anything about the history of CSS's design, but I'm perpetually amazed that positioning (and specifically centering) requires so much CSS magic. I would think CSS positioning would be a one-liner, but instead it's a collection of context-dependent, browser-dependent recipes. I wonder if there's a good reason for the madness, or if it's just poor design. (FWIW, I'm a backend engineer and have only dabbled…
Re: The complete guide to centering a div
#29I was wondering why the demo is not working for me. it really took me a while to realize that it is actually a button.
Re: The complete guide to centering a div
#30What about cases in which developer wants to vertically center a div, but he or she doesn't want to specify a height property?
Then unfortunately you have to calculate it with JS if it varies. Vertically centering divs has always been a gigantic pain-in-the-ass. If anyone has a better idea for doing this without JS, please let me know. Once CSS implements variable-based elements (ie #div.height) then we'll have a lot more control.