views:

147

answers:

5

In every design tool or art principle I've heard of, relationships are a central theme. By relationships I mean the thing you can do in Adobe Illustrator to specify that the height of one shape is equal to half the height of another. You cannot express this information in CSS. CSS hard-codes all values. Using a language like LESS that allows variables and arithmetic you can get closer to relationships but it's still a CSS variant.

This inability in my mind is the biggest problem with CSS. CSS is supposed to be a language that describes the visual component of a Web page but it ignores relationships and contraints, ideas that are at the core of art.

How possible is it to imagine a new Web design language that can express relationships and contraints that can be implemented in JavaScript using the current CSS properties?

A: 

Are you looking for something like CSS Scripting Layout Specification or Constraint Cascading Style Sheets for the Web? Both are still in the research/prototype stage though.

The CSS Scripting Layout Specification has been implemented as a Google Chrome plugin, it seems.

Sebastian
Yes these are very detailed descriptions of what I had in mind. The paper on Constraint Cascading Style Sheets is brilliant. It was written in 1999 and unfortunately I can't find a record of any progress since. Is it technically feasible to implement a similar system in JavaScript in the current browsers? Or at the least is W3C considering such features for a future version of CSS?
hekevintran
Sorry but I can't answer any of your questions. I stumbled across the 1999 paper during my own work, but apparently nothing much has come out of it.
Sebastian
A: 

There was also a JavaScript Style Sheets spec from Netscape back in 1996, http://en.wikipedia.org/wiki/JavaScript_Style_Sheets.

The CSS Scripting Layout Specification is not a Chrome plugin. What is provided is a proof of concept. Many people are not convinced that JavaScript can perform well enough for CSS layout due to Microsoft's CSS Expressions implementation that had severe performance issues.

It is limited to layout as this seems to be the biggest complaint with CSS. It's aim is to give power users the ability to do nearly anything they want, but at the same time make it such that layouts can be encapsulated, referenced, and reused by novice users.

Darrel Karisch
A: 

Directly, this isn't something you can do in pure CSS without causing more trouble than helping due to varying amounts of support from different browsers.

Indirectly,frameworks like Less or by running your CSS through a server-side script before sending it to the client are your best bet but, like you said, not ideal.

In Javascript, using jQuery to set one element's height property based on another's outerHeight is probably the start of a decent solution, but I can't find any code examples people have written to solve your specific problem. I'd imagine it might be something like this, though:

var totalHeight = 1000;
$("#div2").height(totalHeight - $("#div1").outerHeight());

That would set the height of one based on the height + border + padding of the other. In order to be more robust, there's more that needs to go into it than that, but it's the beginning of a solution.

JoshMock
I like this idea. It looks like a good start. The first thing about this that I see as problematic is that it uses imperative code to express a visual appearance. This is like using imperative code to draw a UI in a desktop application. A declarative language that gets converted into jQuery function calls like this is probably step 2.
hekevintran
Maybe write a jQuery module which uses the [rel] attribute to get style data for elements? It wouldn't exactly be pretty and would destroy the idea of separating content from presentation, but it would work and would still be declarative...
ehdv
Yes, as I said, it's the start of a solution, but hardly a complete one. Perhaps a jQuery plugin would be a good full solution, where it can look at an element's rel or class attribute for some code-like syntax that expresses how it relates to elements around it in height, width, color, etc. That way no Javascript need be written by the implementing developer. Maybe it's an over-engineered solution, but it could have many uses if fully fleshed out.
JoshMock
Just to continue the discussion, here's an example of how to use a simple syntax in the "class" attribute that jQuery can read into: http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/ It's for form validation, not positioning, but the idea stands. Have you had any luck finding/building a solution?
JoshMock
A: 

You may want to take a look at Clever CSS, a pythonic approach to writing CSS. It includes variables, arithmetic, you can even do operations on colors.

I'm also looking after one such approach to writing styles, something that is a higher level over CSS. But one of the big problems, I believe, will be the fact that CSS style sheets are often written by designers and not programmers, using design tools that probably allow them to work at a higher level and generate the CSS afterwards. What for us programmers could be a good flexible approach may not work because it is too geekish for designers.

Carles Barrobés
BTW for elastic layouts I've used a jQuery-based approach as JoshMock suggests. I still don't think it's agood solution because it is difficult to explicitly and readably express the relationships / constraints between elements
Carles Barrobés
Clever CSS is very similar to LESS or Sass. These languages definitely make CSS easier but they do not truly encode relationships the same way that Adobe Illustrator does. I like the jQuery-based approach suggested by JoshMock, but I agree with you that it is not an ideal solution not only because it is not designer friendly but also because describing visual things in imperative code is just hard. I think an interesting open source project would be to design a declarative language that can encode relationship information but can also be parsed and applied by JavaScript in current browsers.
hekevintran
In other words I am thinking about writing a JavaScript library that will be a replacement for CSS engines of browsers. So instead of relying on the browser to apply CSS properties using the traditional cascade, use a declarative language that can properly express relationships and constraints and convert that to CSS properties using JavaScript code. Performance and cross browser compatibility will probably be insanely difficult problems to solve.
hekevintran
Declarative info parseable by JS sounds like a good idea. If you make it e.g. jQuery-based you may have some of the browser compatibility issues solved. Let me know if you start developing something like this as an open source project, I'd lend you a hand.
Carles Barrobés
+1  A: 

If you set the size attributes using a percentage, and place the element as a child of the one you're sizing, you will be able to size an element relatively to another. Then, use positioning to move the child outside the parent physically.

Delan Azabani