News for hackers. Stuff that matters.
Re:CSS Absolute Centering by crazygringo on Saturday August 10, 2013 @12:35PM (#6191651)
As someone who's done an insane amount of front-end programming, you're entirely right.

Sometimes I wish I could get back all the brain cells that I've wasted on the hundreds of "little tricks" you need to know to get things working -- and then working cross-browser.

Short answer: CSS is just a horribly, horribly, terribly badly designed language. It's the absolute worst. Don't let anybody tell you otherwise. It's that, it's what we've got, and it is awfully powerful in many ways. But it's complete rubbish in others, which include a lot of incredibly basic layout things.

Re:CSS Absolute Centering by gcv on Saturday August 10, 2013 @01:39PM (#6191929)
crazygringo is absolutely right.

When the committee responsible for CSS started working in the late 1990s, it seems to have lacked any members who knew anything about layout. Certainly, the browser was a new challenge, but automatic layout engines had existed for decades by that point. TeX, for example. While it is a beast, I have usually found it far easier to get its glue-and-boxes model to do what I want than wrestle with CSS.

To make matters worse, the CSS people considered it a badge of pride that CSS lacks programming features. Allow variables to represent colors so we don't have type #f0c450 all over the place? Nesting constructs? Perish the thought!! That would make it look like a programming language, with scopes! As a result, we now have LESS and Sass which plug up the worst holes, but do nothing about the fundamentally brain-damaged layout model.

Re:CSS Absolute Centering by steveax on Saturday August 10, 2013 @02:56PM (#6192221)
You're comparing a layout engine for print where the creator has control over all aspects of the presentation (font size, page size, etc.) with CSS where the content is displayed in an environment where the user is in control of many of those? Apples to Oranges. Pretty much all the things that are difficult to do in CSS have to do with this uncertainty and lack of control.

For example, centering in CSS is trivial if you're only doing it for one viewport size, font, font size.

Re:CSS Absolute Centering by lobster_johnson on Sunday August 11, 2013 @01:43AM (#6194006)
It's true that it is harder to design a model based on different viewports and so on. However, this does not excuse the ridiculous shortcomings of CSS, especially in this case.

CSS doesn't really have vertical positioning within arbitrary boxes, which is insane if you think about it. This shortcoming has nothing to do with viewports or fonts or whether it's screen or paper. After all, the rule "box B is to be vertically centered within box A" is not dependent on a particular medium such as print or screen. The rule "box A is to be 100 pixels tall" adds medium-specificity, but that's different.

Rather than providing rich high-level rules to create simple, responsive layouts, CSS makes one jump through hoops to accomplish trivial things like vertical positioning, and gets mired in finicky micro-details and a cumbersome, inflexible layout model. It breaks down quickly; for example, only recently, with the introduction of "box-sizing: border-box", has it been possible apply percentages to box sizes that have padding. (Before, if you had two boxes with "width: 50%", they would wrap if you applied padding.)

As an aside, I am personally not convinced that the CSS promise of separating presentation from content has paid off. In any serious web app, the DOM is visual, and separating CSS from the DOM makes no sense. It's useful as a way to organize code and to share style rules, to be sure, but those benefits don't come from content/presentation separation. I wish we had an inverted model — a visually oriented language concerned only with presentation (think SVG, only not limited to vector graphics), with a separate data language concerned exclusively with data.

Re:CSS Absolute Centering by gcv on Saturday August 10, 2013 @04:34PM (#6192577)
As I said, the browser is a new challenge, and I was referring exactly to the problem of presentation flexibility. However, I'm pretty sure that the boxes-and-glue model can handle flexible viewport and font sizes as inputs from the browser, rather than specified by the user in terms of page size.

CSS has boxes, more or less. It does not have glue in any meaningful way, i.e., it does not give a direct way to specify what happens in between the boxes. Pretty much all the things that are difficult to do in CSS have to do with the difficulty of describing what happens in the negative space.

Re:CSS Absolute Centering by ianstormtaylor on Saturday August 10, 2013 @01:53PM (#6191978)
You should distinguish between whether the syntax and general principles of the language are horribly designed or whether the current language features are lacking. They're very different critiques. The former results in people saying we need to completely scrap CSS and find a new way to layout pages. The latter results in people putting more pressure on and getting more involved in the CSS working groups and browser implementations to get new features pushed to consumers.
Re:CSS Absolute Centering by crazygringo on Saturday August 10, 2013 @02:03PM (#6192014)
Well, there's nothing inherently wrong with the syntax or structure of CSS (and things like LESS make it even nicer).

I'm talking about things like the way "floats" transformed from a way of wrapping text around pictures (good) to a general-purpose layout mechanism (disaster).

About how there's no way to put a footer at the bottom of your page that stays at the bottom if your content is short, but gets pushed down if your content is long (the normal desired use case).

About how the only way to get two columns of equal height, but that expand to fit the text, is to use a <table>, but that this is supposedly disallowed because it's "unsemantic".

About the complete disaster that float-clearing is, yet how it's central to almost all modern CSS layouts, and fixed only by bizarre byzantine hacks like "clearfix", overflow:hidden (which breaks other things), or the classic "<div style="clear:both;"></div>" which needs to be peppered throughout HTML, negating the whole supposed distinction between HTML and CSS.

And finally, the fact that the width of an element doesn't include its border and padding, making for nesting div's that include border and padding much more difficult than it should be. (Although this is, of course, fixed in newer versions of CSS.)

In the end, it's not really "current language features", but mainly the disastrous decision to adopt floats as a basic layout concept, instead of grids/columns/etc. (Which is, of course, why so many people wind up using <table> in the end, which has its own problems...)

Re:CSS Absolute Centering by ianstormtaylor on Saturday August 10, 2013 @04:51PM (#6192633)
No one ever really decided that floats were the way to layout columns as far as I know. It's completely about "current language features". The standards group was just too slow to implement a decent alternative so we had to use floats. But flexbox[1] is the solution to lots of the problems you are mentioning, and it's almost support in all modern browsers, so very soon we can start abandoning floats and absolute positioning in our layouts.

Meanwhile, CSS is still a well-designed language in terms of syntax and principles. We just need a few more features that weren't in the original spec as new uses for the web emerge.

[1]: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexi...

Re:CSS Absolute Centering by chestnut-tree on Saturday August 10, 2013 @03:33PM (#6192348)
I actually wish that CSS didn't have so many different ways to write a rule. Have just one way to write a rule and enforce it strictly.

In other words, instead of being able to write any of these

  padding: 0 0 0 0;
OR

  padding-top: 0;
OR

  padding: 0;
Just settle on one (the first one in my opinion for this rule) and get rid of all the other alternatives.
Re:CSS Absolute Centering by dragonwriter on Saturday August 10, 2013 @03:39PM (#6192371)
The first one (and the third one, which is closely related) doesn't provide any mechanism to override a single of the padding settings while inheriting the rest.

If you could only have one mechanism, and you weren't going to lose significant functionality, it would have to be the longhand padding-top, padding-bottom, etc., set.

But I think having the shorthand rule is worthwhile.

Re:CSS Absolute Centering by chestnut-tree on Saturday August 10, 2013 @04:02PM (#6192443)
I didn't think padding was inherited?

The reason I chose the first example over the second is that, if you only ever specify padding-bottom for example, what are the default values for the other unspecified padding properties? This is why we have the CSS reset technique and normalize.css (more hackery).

I would make it mandatory that you declare all four values for the padding rule to remove any ambiguity. That would also mean eliminating shortcuts like

  padding: 5px 10px 15px;

  padding: 5px 10px;
I think having shorthand rules just adds to the ambiguity and complexity of CSS, because, as you rightly point out, each of the variations has a subtly different meaning from each other. This is not a good thing (in my opinion :-) especially when you consider how lengthy and complicated stylesheets are nowadays for many websites.
Re:CSS Absolute Centering by ianstormtaylor on Saturday August 10, 2013 @04:48PM (#6192627)
It's not about inheritance, it's about specificity. If you have a generic module that specifies

  padding: 5px;
And you actually the bottom of your element to have more padding, you need to be able to do adjust the bottom padding independently

  padding-bottom: 10px;
Otherwise, you'd be required to tightly couple your our padding requirements with the defaults of the original module

  padding: 5px 5px 10px 5px;
Re:CSS Absolute Centering by lmm on Saturday August 10, 2013 @05:28PM (#6192738)
CSS requires you to do that in plenty of other places (e.g. if you make your border 1px wider you have to make the element it's on 2px narrower.)
Re:CSS Absolute Centering by callmevlad on Saturday August 10, 2013 @06:16PM (#6192879)
If you set box-sizing: border-box [1], you don't need to worry about the latter.
Re:CSS Absolute Centering by emn13 on Sunday August 11, 2013 @04:38AM (#6194351)
Even without border box you can achieve that quite easily (since the conceptual border-box 100% is the default for blocks); you simply need more wrapper elements.
Re:CSS Absolute Centering by lmm on Sunday August 11, 2013 @04:12AM (#6194291)
If only that worked on all the browsers I was required to support...
Re:CSS Absolute Centering by fractaled on Saturday August 10, 2013 @04:40PM (#6192602)
How about if you want to override just one part?

It doesn't make much sense with "padding", but for something like "border: red solid 2px;", how do you override just the color if you don't have the individual components broken out?

Re:CSS Absolute Centering by chestnut-tree on Saturday August 10, 2013 @05:50PM (#6192810)
Good point. I should have qualified what I wrote previously by saying some CSS properties (like padding) could be simplified with just one way of writing the syntax. I say simplified (but not necessarily shortened) because it means you no longer need to explain that property x can be written this way. Or it can be written that way. Or it can also be written yet another way.
Re:CSS Absolute Centering by dragonwriter on Saturday August 10, 2013 @04:21PM (#6192527)
> I didn't think padding was inherited?

I forgot that padding required explicit "inherit".

So, bottom line, yes, it would seem to make sense to just have the single-line complete version:

  padding: *w* *x* *y* *z*
Re:CSS Absolute Centering by PavlovsCat on Saturday August 10, 2013 @04:48PM (#6192626)
That doesn't matter, you had it right the first time and there is a still a reason and a need for being able to specify single elements of a property. Say, you set a specific font size and font family for the body, but want some element to have a slightly smaller font size... having to explicitly repeat all the stuff would make CSS unworkable. (look up the "font" short hand, there is quite a lot in there, and that's just one of so many, e.g. backgrounds and background images)
Re:CSS Absolute Centering by dragonwriter on Sunday August 11, 2013 @11:24AM (#6195153)
I'm referring specifically to padding; whether reduction of options is appropriate in other cases I didn't address. The balance if brevity, specificity, and distinct function (e.g., inheritance) is not the same for each of the cases where CSS has multiple forms with overlapping function.
Re:CSS Absolute Centering by joshuacc on Saturday August 10, 2013 @03:27PM (#6192326)
"About how the only way to get two columns of equal height, but that expand to fit the text, is to use a <table>, but that this is supposedly disallowed because it's "unsemantic"."

This is incorrect. Using display: table and display: table-cell allows you to achieve equal height columns.

Re:CSS Absolute Centering by barrkel on Saturday August 10, 2013 @06:51PM (#6192970)
Well yes, if you make a div work like a table and a nested div work like a table cell, you can indeed technically get away without using tables and cells, but you're fooling nobody.
Re:CSS Absolute Centering by jonahx on Saturday August 10, 2013 @09:23PM (#6193365)
+1. In what world, other than the current one fraught with our bizarre legacy, would you link the concept of vertical centering with the concept of "tables" and "cells"? How about: "display:non-idiotic-mode" and "display:layout-support-any-sane-person-would-expect", and now you can use "vertical-align"? A bit verbose but more accurate!
Re:CSS Absolute Centering by unclebucknasty on Saturday August 10, 2013 @09:26PM (#6193376)
Tables (or grids) are a very natural way of thinking about layout. Just about every webapp/site uses some sort of grid scheme as its layout model.

This is probably why the woefully layout-inadequate CSS had to include these hacks to mimic table behavior.

So, it's more like CSS is fooling nobody.

Re:CSS Absolute Centering by yesimahuman on Saturday August 10, 2013 @12:55PM (#6191733)
I've never really understood people who say CSS is horribly designed. In some ways, we are trying to apply desktop application design paradigms to something that started document or content oriented.

I can tell you that I'd much rather use CSS than Android XML layouts, or WPF, or adding layout containers a la Swing or AWT. It's simple, easily understood across disciplines, and very powerful. Elements of a pretty well designed system.

Re:CSS Absolute Centering by orangecat on Saturday August 10, 2013 @03:08PM (#6192259)
It's simple, easily understood across disciplines, and very powerful. Elements of a pretty well designed system.

On the other hand, it's 2013 and we're still trying to figure out how to center one element inside another.

In some ways, we are trying to apply desktop application design paradigms to something that started document or content oriented.

Which is exactly the problem. Web development today is a Turing tar pit, because we're using tools that were originally created for very different purposes. HTML is great for static documents, and lousy for apps. Javascript is great for attaching dynamic behavior to widgets, and lousy for large-scale software projects. CSS is great for consolidating style information, and lousy for layout. Yes, after over a decade of hacking by many smart people we've built up elaborate frameworks that sort of do what we want most of the time, but I wish we could have had more appropriate tools in the first place and been able to spend those resources elsewhere.

Re:CSS Absolute Centering by hamburglar on Saturday August 10, 2013 @06:20PM (#6192890)
> On the other hand, it's 2013 and we're still trying to figure out how to center one element inside another.

100% this. CSS is a very powerful tool if you know its quirks very well, and there are a lot of quirks. I know a lot of incredibly talented developers who can pick up any programming language or technology in no time at all, but still occasionally waste half a day circle-jerking with CSS trying to make it do something incredibly simple like center one box in another or baseline-align a couple of pieces of text.

Re:CSS Absolute Centering by peterhunt on Saturday August 10, 2013 @05:50PM (#6192805)
Agreed entirely that this is the problem today.

However we did have that (Java applets), and they didn't take off. I think the web did hit a bit of a sweet spot at the time, but it's time for us to build new technology on top of it. Personally, it's looking like asmjs, inline styles and translate3d divs to me :)

Re:CSS Absolute Centering by DougWebb on Saturday August 10, 2013 @02:41PM (#6192167)
Documents, in the real world, are full of layout that has been difficult to replicate in CSS. And to counter your point more directly CGI and the concept of web applications existed before CSS 1.0, so there was already need for application-style layouts when CSS was being designed.

Flexbox isn't the answer either. I had high hopes for it and I'm in the middle of redesigning a web application UI using Flexbox for layout, and I still can't do some basic things like have an overflow:auto content area that expands to fill the space between a fixed left-hand nav panel, a variable-height fixed header, and a fixed footer. I'm still stuck with a bunch of javascript and a window.resize event handler to measure some heights and adjust the sizes of my containers. Maybe the upcoming CSS grids will work, but it'll be another couple of years before support is wide enough for me to use them.

EDIT: I also wanted to say that I've learned xaml in the past few years, and that's absolutely not the direction I'd want CSS to go.

Re:CSS Absolute Centering by anonyfox on Saturday August 10, 2013 @02:37PM (#6192144)
i second this. CSS may be crap, but switching from frontend-development to swing-stuff makes me wanna hang myself after a few minutes. (And i worked with awt/swing/swt long before i touched CSS.)
Re:CSS Absolute Centering by lmm on Saturday August 10, 2013 @05:30PM (#6192745)
Try qt/smoke if you can. An API that actually makes sense and just does what you expect - or you can use QML, which is what HTML for applications ought to look like.
Re:CSS Absolute Centering by grandpoobah on Saturday August 10, 2013 @05:42PM (#6192782)
Wpf is fantastic, it is everything html could ever wish to be.
Re:CSS Absolute Centering by Skalman on Saturday August 10, 2013 @12:45PM (#6191687)
I agree that it's a disaster, but: Is there any better language? I feel that very few of the "native" variants have APIs that are simpler to deal with. (Of course Sass is much better than vanilla CSS, but it's basically the same if you strip away the sugar)
Re:CSS Absolute Centering by sker on Saturday August 10, 2013 @01:15PM (#6191826)
XAML is very well designed, not for the web though. It was designed from the ground up to be consistent with only one implementation, as opposed to CSS, which evolved over the years on multiple implementations. It can get quite verbose since it's XML-based, but at least what you code is what you get.
Re:CSS Absolute Centering by Skalman on Saturday August 10, 2013 @02:00PM (#6192005)
I feel that CSS is pretty well designed. The problem is that it has so many ways to do the same thing, and the fact that vertical and horizontal layout don't work the same way.
Re:CSS Absolute Centering by platz on Saturday August 10, 2013 @01:30PM (#6191895)
I've not built apps in xaml but I've heard the that its a PITA once your app gets to a certain size and complexity.
Re:CSS Absolute Centering by sker on Saturday August 10, 2013 @01:39PM (#6191928)
I agree, I just think it's well designed and consistent. Plus, CSS can also be a royal PITA, that's why I take refuge in back-end development when possible.
Re:CSS Absolute Centering by platz on Saturday August 10, 2013 @01:28PM (#6191885)
Show me a presentation language that is as flexible and powerful as CSS. Try styling something in a native gui language, or something like xaml, and I guarantee there will be many more curse words.

A lot of people who are shocked with the complexity of CSS aren't saying this after switching from another better GUI environment, they're getting into visual apps after doing backed work.

CSS is not perfect but I'll take it over previous presentation idioms any day.

Re:CSS Absolute Centering by lmm on Saturday August 10, 2013 @05:32PM (#6192757)
> Show me a presentation language that is as flexible and powerful as CSS. Try styling something in a native gui language, or something like xaml, and I guarantee there will be many more curse words.

You're on. I pick QML.

Re:CSS Absolute Centering by platz on Saturday August 10, 2013 @08:08PM (#6193152)
I must admit I'm unfamiliar, but at a quick glance it does look quite nice.
Re:CSS Absolute Centering by jonnytran on Saturday August 10, 2013 @03:40PM (#6192373)
What are some examples of previous presentation idioms you're thinking of? I think I can relate to what you're saying, but I'm trying to think of concrete examples.
Re:CSS Absolute Centering by saraid216 on Saturday August 10, 2013 @04:13PM (#6192487)
Cascading text styles are pretty well done, I think. The issue is entirely with box models, floating elements, scrollbars, positioning, z-index... you know. Everything to do with layout.
Re:CSS Absolute Centering by dminor on Saturday August 10, 2013 @03:38PM (#6192369)
> Short answer: CSS is just a horribly, horribly, terribly badly designed language.

On the contrary, it's the box model that's horribly designed. CSS is quite excellent.

We're long past the days of web pages being simple hyperlinked documents, but the browser's layout options haven't evolved quickly enough.

Re:CSS Absolute Centering by Keyframe on Saturday August 10, 2013 @08:49PM (#6193263)
Short answer: CSS is just a horribly, horribly, terribly badly designed language. It's the absolute worst.

16. When your language is nowhere near Turing-complete, syntactic sugar can be your friend.