26

CSS style sheets have a habit of growing big and chaotic over time.

There are a lot of rules, hints, and schools of thought that help achieving cleaner CSS. (For example here) However, all those require constant alertness, activity and a lot of discipline on the maintainer's end, with mixed real-world success. As Nicole Sullivan so nicely puts it:

In fact, in most cases, the things we considered best practices were leading to the bad outcomes we sought to avoid. I realized (unpopular though it might be), that we couldn’t make it work out well by trying harder. Each time we start a new project, we think “this time, I’m going to keep the code clean. This time the project will be a shining example of what can be done with CSS.” And without fail, over time, as more content and features are added to the site, the code becomes a spaghetti tangle of duplication and unpredictability.

Are there any efforts to create a language of some sort, with strict structural rules and a merciless compiler, that enforces strict rules that prevent style sheets from becoming spaghetti? The compiled end result would be CSS.

I have no idea what such a language would look like and whether, given the vast amount of possibilities and combinations, this is a solvable problem at all.

Is there any research in this field? Anything to try out?

One very interesting related tool is CSS Lint, but what I'm asking about goes even farther than that.

Edit: LESS and SASS absolutely go into the right direction, but they are not what I am looking for. They introduce some very nice features and are a godsend for the CSS developer, but what I'm, asking about goes even further and more into defined, enforced structures.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • "The compiled end result would be CSS." There should be some you've already heard of... – BoltClock Jun 21 '11 at 13:11
  • @BoltClock LESS and SASS go into the right direction, but they are not strict enough for what I am looking for. I'll add that to my question. – Pekka Jun 21 '11 at 13:14
  • 2
    So, what do you want that LESS and SASS don't do? – Marcin Jun 21 '11 at 13:46
  • @Marcin neither LESS nor SASS really address `Are there any efforts to create a language of some sort, with strict structural rules and a merciless compiler, that enforces strict rules that prevent style sheets from becoming spaghetti?`. They add some great features, but not in the field I'm asking about. Nested statements are a step towards that, but not more than that – Pekka Jun 21 '11 at 13:52
  • 3
    @Pekka: No such language exists for any target language. Beyond constructs that allow you to write well, and heuristics as in Lint, nothing will strictly prevent you from writing spaghetti code. – Marcin Jun 21 '11 at 14:23
  • @Marcin A tool or language that dictates *some* of the structure, and checks some heuristics in one go, is probably what I am looking for. Like programming languages do: To run, you need a main function or class; that needs to have an interface defined; and so on. – Pekka Jun 21 '11 at 15:14
  • 1
    @Pekka: That's completely different from what you're asking for, on every level. – Marcin Jun 22 '11 at 00:14
  • in the name of completeness stylus is another project like LESS/SASS http://learnboost.github.com/stylus/ – generalhenry Jun 22 '11 at 05:11
  • Just because it's missing: [LessCSS](http://lesscss.org/) is a nice alternative to SASS. – Sean Patrick Floyd Jun 21 '11 at 13:09
  • You'll have to be your own "merciless compiler". Things like css lint and less/sass can help you with that but in the end there is always a human, prone to produce a messy css(like) file even with the most strict compiler. Self-disciple, FTW:) – Litek Jun 21 '11 at 13:58
  • I am looking for ways to solve exactly this problem :) That why I'm thinking in terms of a high-level compiled language - there are still plenty of ways to mess things up, but certain structural idiocies get rejected at compile time. But this may not be possible for CSS. – Pekka Jun 21 '11 at 14:00
  • [SASS](http://sass-lang.com/ "SASS") should be the first one to check. – Eifion Jun 21 '11 at 13:07
  • True - both LESS and SASS (SASS more) have elements that go in that direction. They're not *entirely* what I'm looking for but absolutely worth a mention – Pekka Jun 21 '11 at 13:10
  • Sass also has nesting, and functions, and you can extend classes. ZOMG, it's awesome. – Dan Gayle Jun 21 '11 at 17:14

5 Answers5

20

You could very easily write spaghetti code in any interpreted language, so really what you are looking for is a CSS compiler. This is sort of what Google GWT does for JavaScript by generating it from Java code.

However, CSS doesn't have flow control, functions, variables etcetera, which means it doesn't make sense to have a higher-level language around CSS. At the root of it, it's just name-value pairs with cascading logic.

If you want to reign in your CSS code, you need to reign in your DOM as well. If you are defining columns with IDs left and right then you are semantically restricting the designer from moving them anywhere else. On the flip side, if you are smart about things you can do a lot more.

Lastly, the cascading part is what most new designers have the highest tendency to get wrong. If you pay close attention to the DOM and cascading logic, make use some good resets or grid-based frameworks and incorporate tools like LESS (Ruby) or LessPHP, you'll end up CSS that's far more pleasing to work with. I like LESS because It empowers you with quasi-functions, variables and nested properties which really helps clean up your CSS.

aleemb
  • 31,265
  • 19
  • 98
  • 114
  • 1
    If I could upvote you more, I would. Everyone seems to try to think of CSS as a "programming language," when it's not. Having tools like LESS can help by making developing CSS more like programming languages (thus not requiring the paradigm shift), but in the end, the CSS is still just name-value pairs. – Shauna Jun 21 '11 at 14:14
  • 2
    +1 This is probably the long and short of it - a solution enforcing discipline would have to control (and structure) what gets put into the DOM as well as on the CSS rules. In the end, you would end up with something like GWT - which many people wouldn't like because it's too restricted. @Shauna I understand CSS is not a programming language, but there still are rules, and a structure, to what it describes. – Pekka Jun 21 '11 at 14:29
  • @Pekka - Yes, there are, but not in the way as most people that want a compiler like what you want, like or accept. Following the rules takes more thought and legwork in interpreted languages than compiled, since there's not compiler to yell at you. – Shauna Jun 21 '11 at 15:14
  • @Shauna that is *exactly* what my question is about - whether there are approaches to describe design rules in a checkable and compile-able form, with one possible end result of creating CSS. – Pekka Jun 21 '11 at 15:15
  • @Pekka, thinking about a solution that ties in with DOM+CSS and that compiles is a good start--that's what I am hinting at though I am not sure it would get you where you want to go. With DOM+CSS, you now have two problems: structure and style. You may even end up with something like ASP.NET server side controls that generate the HTML+CSS. – aleemb Jun 22 '11 at 06:36
10

Are there any efforts to create a language of some sort ... The compiled end result would be CSS.

There are two efforts that I know of to do exactly this:

Both aim to provide an improved version of CSS which allows you to write with more structure.

In addition, Google have demonstrated a development version of Chrome which incorporates a number of additions to CSS along similar lines. This is interesting because it indicates the future direction of CSS, but in the short to medium term you will need to use Less or Sass, as even the work in Chrome is very much experimental and will not be released for some time to come (and even when it is, you'll need to wait for the other browsers to follow suit).

[EDIT] Here's a link which details Google's experimental CSS features in Chrome: http://johanbrook.com/design/css/webkit-css-variables-mixins-nesting/

[UPDATE 18 April 2012] There has been some progress in this area since I wrote this answer. Slow progress, but progress nonetheless. The good news is that CSS Variables has just been published as a First Draft specification by the W3C. See http://lists.w3.org/Archives/Public/www-style/2012Apr/0228.html for the official announcement of the specification.

So that's goodnews. Of course, how long it takes to get into the browsers and the end user base is anyone's guess, but at least there are signs of progress.

Spudley
  • 166,037
  • 39
  • 233
  • 307
4

There is Stylus, something like LESS and SASS, but with transparent mixins.

That means that you can hide away complexity by bundling it into new key/value pairs. Example from the site:

border-radius()
  -webkit-border-radius arguments
  -moz-border-radius arguments
  border-radius arguments

form input
  padding 5px
  border 1px solid
  border-radius 5px

Maybe this kind of syntax is what you want?

PS: you can still type braces and semicolons, if you prefer ;)

flying sheep
  • 8,475
  • 5
  • 56
  • 73
3

Have you checked out Compass? Compass is a framework built around Sass. I think it mostly adds features and boilerplate code to add a library of predefined mixins and whatnot. However, Compass might be even closer to what you want. Compass is based around Blueprint which is a straight up CSS framework (which may also be worth looking in to).

To me the best approach would be to take Compass and Sass (or CSS and Blueprint/LESS and something comparable) and to write a very concise and detailed style guidelines which you could develop overtime as you figure out what works best: your first thoughts on the matter, regardless of experience, may be wrong. Once you have these guidelines down you could talk to the people at Sass, Compass, LESS, Blueprint, whatever, about working in some of your well thought out and "researched" guidelines. OR you could start contributing to these wonderful projects which I believe are all under an open source license (Sass is under MIT, LESS is under Apache and I think Compass and Blueprint also under some form of open source license). Fork it and have fun :D

Volker E.
  • 5,911
  • 11
  • 47
  • 64
cwoodall
  • 49
  • 2
  • 4
    Just for completeness there also seems to be this thing called [HSS](http://ncannasse.fr/projects/hss)... I don't know much about it, but it might also be worth taking a look at – cwoodall Jun 21 '11 at 16:25
3

Well you seem to know about Nicole Sullivan, so if you know about OOCSS already, I apologize, but no one seems to have brought it up. If you don't, it's her own project to do this very thing, I believe. It's only in Alpha testing right now, but you asked about efforts and not finished languages, so maybe this will be just what you're looking for. It definitely claims to be be for solving the problems you describe, but I haven't used it myself.

https://github.com/stubbornella/oocss/wiki

C. Warren Dale
  • 184
  • 2
  • 11