Absolute Horizontal And Vertical Centering In CSS
coding.smashingmagazine.com
coding.smashingmagazine.com
The best way to do real vertical-centering that works with variable heights, and doesn't use JavaScript, is to take advantage of the fact that table cells do this with "vertical-align: middle". Inexplicably, only table cells allow true vertical centering with variable heights.
So you can either convert the surrounding element to a big ole <table><tr><td></td></tr></table>, or you can use CSS to give it a property of display: table-cell. And in both cases, give it a "vertical-align: middle". Then you're good to go!
And of course, horizontal positioning in the table cell is easy.
See https://twitter.com/giuseppegurgone/status/38820349236545126...
But I can't figure out, for the life of me, why the bizarre ".container:before" declaration is needed -- it would seem to be entirely superfluous, but if you remove it, the whole thing breaks. Can you explain what's going on?
The pseudo-element (in red) is 100% tall and both .container:before and content are inline-block with vertical-align: middle;
Pragmatically: I suppose that we embrace CSS/HTML/JS as object code. And I mean this in a much more dramatic way than CoffeeScript or SASS. This is finally starting to happen, thanks to browser support for things like source-maps. This is one reason I contribute to ClojureScript!
Idealistically: We should be pushing for lower level platforms for code distribution and execution, as well as host platform integration. Portable Native Client and similar efforts shouldn't be so controversial.
There have been improvements in CSS2 and CSS3, like adding table layout so people could at least use the table layout features on div elements rather than abusing tables for layout, and CSS flexbox that work well for standard UI widget layouts.
But the big fixes to layout have been harder to specify and agree upon. They've been in the works for many years, but it's taken a while to figure out how they really should work, and which parts should be fundamental. It looks like it's getting a lot closer; the CSS Grid Module http://dev.w3.org/csswg/css-grid/ is being treated as the fundamental part of the stack on which others will build, it is being worked on by several browser vendors and is in highly active development, and several browsers have implementations in development. It seems to draw elements from IE's experimental CSS grid, as well as the templates of the earlier template layout proposal.
So, yeah, for many years CSS has had a big deficiency in specifying page layout. It's going to get better very soon, however, once the CSS Grid Module is finished and implemented. I'd imagine we'll start seeing implementations within a year; here's Webkit's tracking bug for it: https://bugs.webkit.org/showdependencytree.cgi?id=60731&hide... here's Chromium's: https://code.google.com/p/chromium/issues/detail?id=79180 . Mozilla doesn't seem to be working on it yet, though they have had some preliminary discussion: https://bugzilla.mozilla.org/show_bug.cgi?id=616605 . IE had implemented an experimental Grid Layout proposal that the current work is based on, so while the syntax won't be exactly the same, they have most of the underlying functionality working: http://msdn.microsoft.com/en-us/library/ie/hh673533(v=vs.85)...
Although lower-level and not as modular, flexbox can already be used with great success for a number of layout sections (media objects, vertical centering, input inserts) or full layouts ("holy grail", sticky footers and grids): http://philipwalton.github.io/solved-by-flexbox/
Flexbox is a CR, and it's starting to get pretty well supported: http://caniuse.com/#feat=flexbox
Whoopee.
However, they don't abstract over tasks like layout--you're still stuck with hacky CSS tricks and semantics.
You're correct, but that hasn't stopped people from trying.
I agree with your frustration about this article being newsworthy (it's 2013! How are we still figuring out vertical centering?!), though I would argue that CSS is great for its original use case and certainly has its place today. Why is it bad that CSS is entrenched in the Web?
Like any development technology, CSS has a learning curve and has several shortcomings (like vertical positioning), but I think it's a fine tool for the job and struggle to come up with something better.
A scrollable table with fixed headers is something that Visual Basic supported in 1997.
Of course it wasn't a use case of interest to the mighty browser vendor cabal so it has never been considered a priority. It does seem like it will be possible soon though with position:sticky https://air.mozilla.org/intern-presentation-ford/
We should probably prepare some sacrifice to the browser vendor gods to thank them for this small morsel they have thrown us.
CSS did borrow concepts from TeX at the time. I agree with you that if it had borrowed more from TeX maybe we would have avoided Javascript and also we would have benefited from an updated model for TeX and I said so at http://tex.stackexchange.com/a/6463/963.
Maybe is time to revisit and revamp both.
I have never seen this fully implemented except in the simplest of blog-engine layouts and toy examples. There is always some pollution of the markup, at the very least in the form of the ordering of elements.
A template-based approach would've provided the same semantics a lot sooner than a decade of half-baked CSS hacks.
Can you please elaborate how tables were the understanding of layout models 15 years ago?
The web was not thought to be interactive at that era, but to be printed, it's not the fault of the old specs that the web progressed significantly fast.
(1) The "H" in HTML stands for hypertext. (2) When has a web page ever printed well?
I said the web was tought to be printed, all the effort of CSS (circa 98) was about styling the web in a way that made sense for print, not for interaction.
In addition, if the web was truly meant to be printed — an assertion I have never heard before; it sounds blatantly false — then not adopting the massively successful TeX system sounds even more insane.
CSS makes life much, much easier.
That said, I use LESS. But while everything compiles down to CSS, it doesn't impact me much.
"Imagine it's 2013 and there is a website that has been online for years, where the greatest hackers and entrepreneurs exchange exciting ideas. When somebody posted an easy solution how to center something in HTML horizontal AND verical it was the favored post on that site for a day" ... ;)
"Other Techniques
Absolute Centering is a great solution for centering, but there are other methods that may fit more specific needs. The most commonly used or recommended methods are Negative Margins, Transforms, Table-Cell, Inline-Block, and now Flexbox. They are covered more in depth in other articles, so I’ll only cover the basics here."
Pray your support matrix allows you to just stick to Flexbox soon and all of this will start to feel like a bad dream from the bygone past.
First we abused tables, then we abused divs, now there are actual sane hooks for this, it just took 20 years is all.
Then the semantic web pedants came along and decreed that tables shall never be used for layout. Fine -- but nobody ever bothered to come up with a DIVs+CSS replacement for the grid-based layout that any serious graphic designer uses to build a page.
And do you know what happened to the web? Debate cause and effect all you want, but webpage designs are far more boring now than they were 10 years ago. Now it's all just blocks of large-print scrollable text, some images here and there (or maybe a wall of them), and a cute logo. It's like everything is a Geocities page with better fonts and color choices, minus the gifs. I feel like I never see anything really cutting edge or creative in the design space on the web.
The internet is a really young medium of course, and it will naturally go through phases, where certain types of design come into fashion while others go out. And I would love it if somebody would prove me wrong by posting some links to some really kick-ass-looking sites. But from where I'm sitting these days, from a design perspective the web is a big yawnfest. Maybe today's websites are more usable and easier to index and work well on a screen reader, but from a graphic design perspective... zzzzz....
Microsoft of course deviated from the nascent "standard" such that we've been burdened for years with "if IE ..." blocks to the point that the latest version of IE pretends not to be IE.
You reap what you sow.
But every time I come across the issue of "how to vertically center in CSS", an issue that has existed for years, I get angry. I get angry because the old way (vertical alignment on table cells) just works. As much as the CSS purists (some might call them "anti-table zealots"), it's hard to argue with something that works all the way back to IE4 and probably beyond (well valign="middle" probably does, CSS vertical-align: middle is probably IE5/6).
But beyond this there are so many things in HTML/CSS that should be simple but are hard. This is just one of them.
Consider spacing between table cells. There's (still) actually no way to do this... if you want no spacing between the leftmost and rightmost cells and the edge of the table, which I find is a pretty good use case. People end up doing things like:
td { margin-right: 0.3em; }
td:last { margin-right: 0; }
Any such solutions had varying backwards compatibility issues, a problem we're now largely getting behind us, but still...Why can't I specify (separately if need be) the vertical and horizontal spacing between cells separately to the edge of the table (which should really be a property of the table rather than the cells IMHO).
The problem here is that the CSS WG seems to have started with a model and then just run with it without running it through very common use cases.
Looking at the article, it looks fine. One thing I'll say is it starts off with the "margin: 0 auto" trick, which everyone who does anything with CSS needs to know. Why is centering that hard? Why can't I just say "put this in the center"? Why can't I then use margins to offset from center or something?
Also FWIW, the margin: 0 auto thing didn't work in older versions of IE if you didn't specify a DOCTYPE and force "standards compatibility mode" (rather than the euphemistic "backwards compatibility mode".
http://css-tricks.com/centering-in-the-unknown/
They're all much of a muchness, at least we'll be able to just use flexbox soon enough.
That kinda ruined all the excitement for me :( Good of the OP to include all of the alternative techniques that might be better for cross-browser or other specific situations.
Seems like transforms are better than the original main suggestion though... why not stick with those?
So using your example, if SASS inherently used grid that would be like CoffeeScript introducing MVC into your project.
I suppose the answer is because it can't do so in CSS alone and that emulating CSS Grid would require CSS, DOM, and Javascript to be emitted from the compiled SASS script. It would be neat if someone who knew more could confirm my suspicion.
I'm not sure I follow your example regarding CoffeeScript and MVC.
http://jsfiddle.net/mBBJM/2155/
Not that it's any less Stockholm-Syndrome-y than the linked article's solution, but still, I have no clue why this is on the front page of HN.
A lot of this is also because a frontend engineer's job is to lower the cognitive processing a user has to do, while a backend engineer's job is to come up with useful things to cognitively process. In other words, when a backend engineer comes up with a demo, they make the viewer work to understand it, while when a frontend engineer does the same they make the viewer go "Ooh! Aah!"
I completely agree with your point re: cognitive processing: good CSS communicates intent to the reader, so if this kind of stuff is unclear to people and the Smashing Mag post helps the concept click in readers' minds, then it's a win :)
http://philipwalton.github.io/solved-by-flexbox/demos/vertic...