News for hackers. Stuff that matters.
Re:What No One Told You About Z-Index by moultano on Tuesday January 15, 2013 @06:45PM (#5063964)
Does anyone have any insight as to why it is this way? To me it seems insane, but I don't trust my own judgement in domains like this because I've never built anything comparable. Are there really good reasons for CSS to be the way it is?
Re:What No One Told You About Z-Index by 2468ben on Tuesday January 15, 2013 @07:13PM (#5064079)
(I don't think they're good reasons, just reasons)

1. Unlike print layout, the dimensions of a webpage can change, and so we slowly invented an evolving layout framework for something without fixed dimensions. I've done typesetting since it was exacto knives and hot wax, and if you told me then to imagine a stretchable paper for news I would have cried in the corner.

2. The layout framework was independently built by a bunch of separate companies, who by nature of competition weren't going to agree on everything (see every browser).

3. The agreements that WERE made were done by committee, including the reluctant inputs of competing companies. This method of design will guarantee that they end up with something weird.

4. Make it backwards compatible with all the previous ghosts of architects past.

Imagine if Ford, GM and Chrysler had to build a joint car every few years.

Also, try designing a page in Flash and using (or just reading about) their graphics/layout/text frameworks. Some things are much harder than HTML/CSS, some are much easier, but it offers a great comparison on a completely different way of solving layout. I've implemented a spec like CSS for customizing Flash app appearances, and compared to what existed before, it made everything better.

Re:What No One Told You About Z-Index by CapitalistCartr on Wednesday January 16, 2013 @01:48PM (#5068334)
Imagine if Ford, GM and Chrysler had to build cars that shared the same roads, used the same fuel, fit in the same overall dimensions for parking and driving, accepted the same basic controls: Steering, foot pedals, etc. It would be madness.
Re:What No One Told You About Z-Index by 2468ben on Wednesday January 16, 2013 @08:26PM (#5070329)
Those features are the madness of standards enforced under penalty of law. CSS is a recommendation, and the only penalty is a competitive disadvantage. Back when the Internet was a graphically simpler place with a browser monopoly, there was little disadvantage to implement CSS however they damn felt. If Ford sold 90% of cars and a consortium recommended they add 12 new safety features, imagine how they'd respond.
Re:What No One Told You About Z-Index by yuhong on Wednesday January 16, 2013 @10:59PM (#5070858)
In case of CSS, the important browser was IE.

This reminds me of DOCTYPE switching. From http://hsivonen.iki.fi/almost-precedent/: "Back when the Quirks Mode and the Almost Standards Mode were introduced ... committees created specs and that were enshrined as W3C Recommendations before there was solid implementation experience. Then the Web Standards Project lobbied browser-makers to implement the W3C specs as they were. The mindset was that the specs given from above couldn’t and shouldn’t change. People thought that vigorous upgrade evangelism would work and make Web authors change their existing sites."

Note that the Web Standards Project was also the one petitioning Netscape to cancel Mariner, which ended up contributing to the problem. Why did non-IE browsers have to implement document.all?

Re:What No One Told You About Z-Index by pornel on Wednesday January 16, 2013 @05:02AM (#5066143)
1. CSS1 started modestly by specifying very little over what browsers already did at the time, and from the start locked itself into HTML's inline/block/float/table model.

Back then the old Netscape engine was dominant and it couldn't get even those basics right (http://acid1.acidtests.org), so there was no hope of adoption of a more advanced layout mechanism.

2. CSS had deliberate limitations that did help early implementors: e.g. elements later in the document are not supposed to cause reflow of elements earlier (with tables being "legacy" exception). That's how Opera worked until v6. That what helped all browsers implement progressive rendering. The downside is abysmal vertical centering and limited selectors (flexbox and CSS4 selectors finally drop that limitation).

3. It was hard to get any progress on this for many, many years. First we had Netscape 4, then we had IE6 holding back use of CSS on the web, so specs from that time were mostly untested ivory tower science-fiction.

Things got better since, but of course now we have massive legacy and backward-compatibility constrains... and IE8/9 holding back flexbox.

Re:What No One Told You About Z-Index by mcgwiz on Tuesday January 15, 2013 @09:00PM (#5064654)
Let's clarify the question.

"Does anyone have any insight as to why vertical centering of dynamic-height content is the way it is?"

Yes. The original standards committee created a layout model (block, inline, and inline-block elements) that (they felt) adequately balanced language complexity with features. It's easy to idiomatically achieve vertical centering. http://jsfiddle.net/aMMxj/10/

"Does anyone have any insight as to why people that code CSS complain about it so much?"

This is a much deeper question, but one that I think is far more informative. Having done front-end engineering for 10 years, across industries, and in very pricey and exclusive consultancies, I have never found another engineer who had read the bulk of the CSS spec, or even a book about it, for that matter.

Developers, the ones with the mind and experience to bring technical discipline to CSS and create a set of idiomatic patterns (both low-level and high-level), treat CSS as a second-rate citizen. The bulk of the rest of the CSS userbase consists of visual design types (coming from print or graphic design), where the tools of the trade are physical pen and paper, or something from Adobe Creative Suite. The remaining portion of the userbase are equally non-technical hobbyists.

This is the real problem of CSS. Those that have the mindset to devise idiomatic conventions to the community don't think the language is worth it. At best, you have semi-experienced developers duct-taping various non-idiomatic ideas together and giving it a fancy name.

Re:What No One Told You About Z-Index by timb on Tuesday January 15, 2013 @11:12PM (#5065208)
"It's easy to idiomatically achieve vertical centering. http://jsfiddle.net/aMMxj/10/ "

that solution

1. only works for text, which is part of eranation's rant.

2. only works if you know the exact height of the container.

3. isn't even centered: http://i.imgur.com/lz8Mq.png

Re:What No One Told You About Z-Index by mcgwiz on Wednesday January 16, 2013 @10:44AM (#5067249)
1. Not true. Could have been an image, or even SVG element http://jsfiddle.net/aMMxj/55/

2. Fair enough. It would be nice to be able to set line-height to a percentage of height.

3. Bug, my bad. http://jsfiddle.net/aMMxj/51/

Re:What No One Told You About Z-Index by timb on Wednesday January 16, 2013 @02:01PM (#5068384)
It's still not precisely centered. Check out this version using a table.

http://jsfiddle.net/nJy8t/

I added a background image with a grid that extends 80px from top and bottom. There's also a small margin/padding/border reset and doctype just to make sure defaults and quirks aren't messing with anything.

Re:What No One Told You About Z-Index by mcgwiz on Wednesday January 16, 2013 @04:01PM (#5069001)
Yeah, I concede mine wasn't pixel-perfect.

To continue down this path of the "natural" way to vertically-center, font-size must also be explicitly set. This works http://jsfiddle.net/nJy8t/3/ . Though it's certainly not without it's flaws (we lose font-size cascading).

Re:What No One Told You About Z-Index by kevingadd on Wednesday January 16, 2013 @12:01AM (#5065384)
Your argument might hold more water if your example actually vertically centered anything.

It's 2013 and it's still in many ways easier to do pixel-perfect, scalable layouts using tables than it is with CSS. It's so bad a common CSS 'layout technique' is to force elements to be laid out like table cells! I find that incredibly depressing.

Many of those so-called 'idiomatic conventions' that you blame developers for not devising are avoided because they're needlessly complex, easy to get wrong, and hard to maintain. Nobody likes having to wrap every piece of content in 3-5 nested divs/spans with complex style rules applied to them.

Re:What No One Told You About Z-Index by mcgwiz on Wednesday January 16, 2013 @10:55AM (#5067320)
You're right, I had a bug: http://jsfiddle.net/aMMxj/51/

Idiomatic solutions, by definition, cut out the non-semantic document structure. Certainly you will find layouts that cannot be achieved idiomatically in CSS, just as certain software patterns are more idiomatic in one OO language than another (e.g. observer pattern in C# vs Java). But the more scenarios you attempt to address with the language, the more complex it becomes. It's quite possible that extensions to CSS would open it further to the criticism that it's too complex.

Unfortunately, browser vendors do not support any other stylesheet language. Perhaps if this were different, there would be more effort to understand stylesheets from an engineering perspective. Since it's not, we're stuck with poorly-thought conventions and a community that's been trained to despise the language.

CSS is not a happy language, that much I'll agree with. But I find it no more painful than other computer languages. I reassert the lack of rigorous analysis by CSS users and the collective community as the main solvable problem. I have worked at extremely pricey consultancies, and the only thing I learned about CSS from them is that the more senior of a developer you are, the less respect you have toward CSS as an engineerable technology.

Re:What No One Told You About Z-Index by sandGorgon on Tuesday January 15, 2013 @10:34PM (#5065085)
genuine question as a non front end engineer- do you recommend reading the CSS spec as a reference guide? im basically asking whether someone learning to be good in CSS should read the spec or pick up one of the recommended books (that appear from time to time on HN).

For example, i never had to read the spec for ruby to get jruby to work.

Re:What No One Told You About Z-Index by mcgwiz on Wednesday January 16, 2013 @10:29AM (#5067181)
There may or may not be Great CSS books out there, but in 10 years, I never found any. They invariably teach you recipes, but not how to to think in the terms the language was designed.

The ruby community is one of the most active and rigorously analyzed in contemporary programming. Blog articles are well vetted, and Google search can reliably point you to decent articles. Further, ruby belongs to a class of languages (interpreted, general purpose, imperative/OO) that is very well understood. Languages from this broader ecosystem include python, perl and even JavaScript, all contributing to a deeper analysis of each.

CSS is a stylesheet system. There are no other popular systems in use. The comparison could be made to Microsoft Word's style system (which has a very different conceptual model). Such a comparison could yield interesting ideas. But such a comparison is never made. Other stylesheet languages for structured documents include the unknown FOSI and DSSSL. When was the last time anyone cited those referenced in a blog post, tutorial, or book about CSS?

I'm not saying the specs are a good teaching device either. But teaching and precise description are different ends. Regardless, because there doesn't exist a good community (incl blogs, books, personalities) to fall back on, the specs are your best bet. Ultimately, they (along with other implementations) are what inform browser implementations.

Re:What No One Told You About Z-Index by michielvoo on Wednesday January 16, 2013 @03:16PM (#5068805)
Would you consider writing a post that explains the high-level (conceptual) differences between CSS, DSSL, FOSI and Word?
Re:What No One Told You About Z-Index by zevyoura on Wednesday January 16, 2013 @12:50AM (#5065544)
I'll admit I haven't read the CSS spec, but I have been reading the canvas element spec in detail, as there's little good, deep reference material for it. It's readable, or at least understandable, but it's challenging compared to more traditional reference books (like MDN's presentation, for example).
Re:What No One Told You About Z-Index by Arelius on Tuesday January 15, 2013 @11:24PM (#5065255)
Does ruby even have a proper spec?
Re:What No One Told You About Z-Index by sandGorgon on Wednesday January 16, 2013 @12:14AM (#5065426)
yes - http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_...

there are other community-driven sources - like RubySpec [1], etc. but this is the (expensive!!) ISO standard.

FYI - I had intended it as an example. I'm really interested to know if the most effective way to work with CSS is to understand its spec... which is not the way it works with most programming languages.

But perhaps CSS is more similar to a rulebook (declarative) than a language.

[1] http://en.wikipedia.org/wiki/RubySpec

Re:What No One Told You About Z-Index by Arelius on Saturday January 19, 2013 @04:43PM (#5084888)
I think generally speaking, reading the spec is useful when trying to figure out the details of more complex topics, I think this holds true for most languages. I used to pull out the C spec regularly, and even the c++ draft spec on occasion, it gets better for smaller languages, for Scheme for instance, I would pull out the spec rather quite often.

Since I imagine the CSS spec is relatively small (compared to say C++) I imagine it would be useful for solving more adavnced use cases.

Re:What No One Told You About Z-Index by Camillo on Tuesday January 15, 2013 @07:54PM (#5064335)
I don't have a reference for this, but I always got the impression that CSS was intentionally limited to help browsers implementers write efficient layout engines with as few pathological cases as possible. For instance, if you could specify the value of object attributes using arbitrary expressions with other objects' attributes as input, it would be very easy to have circular dependencies, unexpectedly high computational costs, contradictions, and so on. Just defining how to solve a contradiction would have made the standard much more complex, and error reporting would have been a nightmare.
Re:What No One Told You About Z-Index by Arelius on Tuesday January 15, 2013 @11:27PM (#5065265)
I think that while that may have originally been the case, I feel that we've long since passed the point of particularly efficient layout engines with as few pathological cases as possible. Particularly computational costs, since I think you'll find many sites doing some part of layout in javascript.
Re:What No One Told You About Z-Index by ajanuary on Wednesday January 16, 2013 @07:50AM (#5066571)
Hence new layout techniques in CSS3.
Re:What No One Told You About Z-Index by spyder on Tuesday January 15, 2013 @10:26PM (#5065053)
Because we used to do these things with tables but tables aren't cool enough since the "War on tables" (because tables aren't the right way to do layouts) and nothing better has been available. Hopefully CSS3 will fix these things.
Re:What No One Told You About Z-Index by sopooneo on Wednesday January 16, 2013 @06:30AM (#5066340)
The crazy thing about that is that all these css grid system seem to me to just be reimplementations of tables, with just as much markup.