1036 points · by plurby · 11y ago · 289 comments
foo {
align: center;
valign: center;
} foo {
display: flex; /* opt into the new box model */
justify-content: center; /* "align" */
align-items: center; /* "valign" */
}
Vendor prefixes may or may not ruin your day (for now), but the spec is there and is exactly what you want. CSS is no longer "badly broken" if that's your metric.I still think there's scope for a language that compiles to CSS but allows totally intuitive positioning, provides error messages if you make a mistake and generally isn't as infuriating as CSS is. The amount of times I've done something which seemed like it really ought to do what I want and the layout doesn't change is unbelievable. Even assuming vertical alignment is totally solved by flexbox it's still an agonisingly painful thing to work with.
That said, I think it's important for people to understand that many of the problems that people have canonically associated with the web's incompetence have been "solved" in standards for a number of years. There's a real difference between problems that remain unsolved in all browsers and problems that do not remain in the current versions of all browsers.
That difference may seem moot until the features are universally rolled out, but understanding that a solution to centering content rolled out in all major browsers (including Internet Explorer) three years ago may help us get past a sense of learned helplessness about the web.
Yes, improving the platform takes time, but by making browser vendors aware of the problems we face as web developers, things do get better.
Nowadays people are not only using desktop computers. Many of the computer form factors used nowadays almost never update, yet one needs to support them.
EDIT: Typo version => browser.
foo<br>baz
renders correctly asfoo bar
but <span>foo</span><br><span>bar</span> renders as foobar.
And AFAICT it doesn't work at all in Safari even with vendor prefixes.
In QML I can center like this:
anchors.centerIn: parent
(replace parent with id of item you want to center in if not the parent item). You can also do just vertical or horizontal centering, or you can fill another item or you can anchor eg the left of your item the the right of another item and the right of your item someplace else and it resizes/repositions your item as required.Flexbox still often requires lots of nesting to achieve what should be (and is in QML) relatively simple layouts. I've also hit a few things that I couldn't do at all with Flexbox (although that could just be lack of CSS/flexbox skill on my part - but to that I say that CSS gets very hard quite fast, while complex layouts in QML are pretty easy).
<table height=100% width=100%><td valign=center align=center>
And what the hell does "margin: auto" mean? I'd like to hear some print designers using that in normal conversation. "oh and also i'd like these margin's auto'ed".
At this rate the table tag is going live longer than the copyright on mickey mouse.
It means "calculate the margins automatically according to available space". Nothing complicated about that.
Print is quite different from web. The most noticeable difference is that paper cannot be resized, nor does it adapt it's content when you rotate it.
position: absolute; top: 50%; left: 50%;
Is that so hard? Having poor CSS skills is not the same as CSS being broken.I'm not a fan of CSS due to issues like this. It falls in the same line as issues where, for example, paddings and margins (and borders) are added to the size of your div rather than subtracted. There's a solution for that that makes sense nowadays but by the time that came about, I'd already stopped liking CSS.
.. But then we've ended up with bootstrap and it's grid layout, where we're doing exactly that, and for some reason it's perfectly fine.
Regardless of how you feel about CSS 2.1, it's indisputably better than the table-based layout we had before. Speaking as someone who has implemented both table-based layout and CSS 2.1, it's really unfortunate to see people want to return to the bad old world.
The primary problem with layout tables was that they involved tightly coupled inlining of your layout rules.
The whole web is fad-driven, so I'm glad at some point, pendulum will swing back.
Meanwhile, I'm just happy parallax fad is dying out.
Grid based layouts are obviously useful but the table tag isn't the right tool.
<div style="display:list-type; type-of-list:unordered"> <div style="display:list-item;">
?
Author email in straight text? Markdown is what you're writing already.
Author it in WYSYWIG/GUI clients? You only care about presentation.
That said, centering (and typography) in CSS and elsewhere is that you mix passionate people with a really hard problem and you don't get answers, you get a lot of debate. I used to read the alistapart mailing list and it was intense at times. Suffice it to say that when the content of part A and part B are different by 1 screen pixel, and you're centering, do you assume +1 pixel or -1 pixel? (You don't get 1/2 pixel resolution) a typically large debate
The fact that you say there is no easy vertical center, of which I disagree, doesn't make it broken. It means it's missing a feature you wish existed.
indeed, an extremely basic and fundamental feature than any site that implements popups could use.
There's this misconception that CSS is a display language; it's actually designed as a typesetting language that must degrade nicely.
Vertical alignment is the real killer.
I think the secret is that maybe 10% of CSS writers actually have read more than the bare minimum to getting their layouts to work ( I am not among them, mind you).
“The easiest way to vertically center something in CSS is to close your laptop and go to the bar.” - @jakeboxer
“How to choose between rem and em”
1. question css
2. question your life
3. throw away laptop
4. scamper into forest
https://twitter.com/iamdevloper/status/587651586350002176Browser support: http://caniuse.com/#feat=flexbox
http://dev.w3.org/csswg/css-grid/
Of course, it might be a while until there is broad browser support!
So: is it not degrading gracefully a property of the site, or a property of Flexbox?
For people that are use to hacks + grid systems, it seems weird at first, but over time you will become familiar with a rich layout system and stop thinking left-to-right, it will become more natural. The language of the spec and the declarations (align/justify distinctions, etc) can be weird at first, but you get used to it.
That's what 'layout' means after all, isn't it? Aligning one thing with another, to show they're related. I'm not a classically trained graphic designer, but doesn't any explication of visual fundamentals include 'alignment' somewhere up near its top?
Yet CSS really, really sucks at lining things up.
Anyone designing a better system might do well to start with the notion of alignment as a basic primitive/operator. To be able to say, item A here ought to line up with item B there (and if not possible, predictable behaviour C results).
My gut feeling is that one could then derive all other types of layouts from there. I admit that might be naive. Also, I realise that I've probably just described constraint-based layout, but as yet I haven't tested one of its implementations such as GSS in a real project.
If one were to consider these problems alone, CSS works quite well for what it was designed to do. Most designers I've discussed these problems with have problems more in their thinking than with the tool itself. They mostly try to apply print design thinking to web design, which simply does not work.
Example: <div class="item"><div><p>Your text here</p></div></div>
.item {display:table;} .item > div {display:table-cell;vertical-align:middle;}
I use this method on justindocanto.com to vertically center an unknown amount of text/elements inside a div that changes its size based on the size of the image that's inside it. Really nice technique when used correctly.
Wish there was another standard that could co-exist side by side on the web, something simple enough to be implemented reliably by the browser vendors.
That standard exists. It's called flexbox. It was designed to solve exactly this problem (among many others), and does.
That said, as I never tire of mentioning, vertical centering was a part of the design of CSS 2.1. The technique is called "absolute centering". See CSS 2.1 10.6.4 [1]: "If both 'margin-top' and 'margin-bottom' are 'auto', solve the equation under the extra constraint that the two margins get equal values."
[1]: http://www.w3.org/TR/CSS21/visudet.html#abs-non-replaced-hei...
wtf?
do...do they know...how many manhours could've been saved...?
Personally, I don't find css to be that bad at all, but I work in it at least a little everyday so it might just be stockholm syndrome.
It is inefficient. It is awkward to set up in code, so many people write their own syntactic sugar around it. It crashes hard when it isn't 100% sure what to do with its constraints. Working with multi-line text is still a pain, so you often run into clipped text on iOS if you dare to increase the font size. etc.
A few years later and I don't think anything but the occasional third party api with undocumented behavior has been nearly as hard.
>Just use flexbox!
Which is great, except it isn't supported at all by IE8 or 9 and is only partially supported in IE10 (by prefix only). Which means a lot of people simply can't use it. I'm hesitant to use it myself, and I've got a very lax policy on supporting old IE.
I have run into a few IE10 gotchas, but they're all solvable. IE10 just uses an older version of the spec.
and IE itself will be cut loose from MSFT in a few months.
"A lot of people" = 4% on IE 8 & 9 (http://www.w3counter.com/globalstats.php)
1. margin: auto (wrong)
2. top: 50% (mostly wrong)
3. negative margin-top (correct if you know the size)
4. table-cell or translateY (correct)
5. a correct answer plus a discussion about why it has to be a hack
FWIW, when you set everything to "Unknown", this site tells you to use display: table-cell. If you don't have to support IE8, I generally prefer "top: 50%; translate-Y: -50%"[Edited for spacing.]
"margin: auto" seems to be the way that the designers of CSS 2.1 intended to center things. 90% of the time, absolute centering is the easiest way to center in CSS; you should prefer it when possible.
In my experience, most of the time when I do want to center something vertically, I have a defined height (which can be a percentage!), so absolute centering is the solution of choice.
Firefox uses pcwaltons method for images (resource://gre/res/TopLevelImageDocument.css). Do you want the image to be in the center/vertical of the div (with the div having a specified height)? If so, then pcwaltons method should work, as long as you have position:relative; on the div.
<HBox height="100%" width="100%" horizontalAlign="center" verticalAlign="middle">
<VBox>Col 1</VBox>
<VBox>Col 2</VBox>
<VBox>Col 3</VBox>
</HBox>The only approximation I could find was this:
.mydiv {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
This, however, looks horrible on most browsers as they either use hardware acceleration (resulting in blurry output) or they drop pixels (e.g. single-pixel lines) falling between the grid.I'm failing to understand your expectations.
Works for dynamically sized content (and statically sized content as well, obviously).
That will never stop being perplexing, no matter how many new models they throw on top to try to fix it.
Center a bunch boxes in a row and have overflowing boxes show up on the next row to the left or right. Doesn't work, it will center the next row as well.
I haven't found a non JS solution to this yet.
<div style="display:flex; align-items:center; justify-content:center; width:100%; height:100%">dynamic both v and h aligned center</div>
it's just sad to think that a browser can do so much stuff, yet it's unable to provide a good layout system.
Of course, when I actually implement it in my design, I prefer to separate my CSS from my HTML.
I wish the solution was presented in both flavors, side by side.
By this, I mean generating the following individually: 1) the HTML with a class applied to a sample element to be centered 2) the separate CSS for that particular class selector
This is just a little nitpick based on my preference for avoiding inline styles; overall, nice work!
<center>Disrupt</center>I meant an explicit feature with it's own attribute.
I find it hard to fault the designers of CSS 2.1 for not being more complex than it was at the time. CSS 2 was a huge advance over CSS 1, so much so that a common feeling at the time was that CSS 2 had so many features no browser vendor would ever implement it all. The authors of CSS 2.1 had to maintain a delicate balance between giving authors the tools they needed and exploding the complexity of the spec, and making vertical alignment fall out of "margin: auto" was one of the ways they did just that.
The way absolute centering vertical alignment works in CSS makes sense once you understand containing blocks: you specify "position: absolute" to disable margin collapse and establish a containing block, you specify the offsets from the edges of the containing block that you want, and then you invoke the vertical-centering feature in CSS by saying "margin: auto".