A feature request for CSS3
One of the niggles I have with CSS 2 is that I frequently have to define colours multiple times. Consider this blog: I use orange in several places (as a background to the header, a border around the sidebar and a background to the sidebar h3 elements). Should I decide to change the shade of orange, or change it to another colour, I would have to alter my stylesheet in several places.
A nice addition for CSS 3 would be the ability to define “constants” for colours (and maybe for other types as well, although I can’t think of any that need them off the top of my head). It would be extremely convenient to define a bunch of colour constants at the top of a stylesheet and then refer to them elsewhere. For example:
@define {
colour_1: orange;
colour_2: navy;
colour_3: #f00;
}
/* later in the stylesheet ... */
h3 {
background-color: &colour_1;
}
That way, changing the @define block at the top of the sheet would change the colour of any element referring to a constant. I’m sure there are much better alternatives for the syntax, but the above should at least make the concept clear.
Update: It has been pointed out in the comments that this has been discussed before on the www-style mailing list but rejected for a number of reasons (see this thread). Aah well.
jonner - 22nd July 2003 18:27 - #
Great thinking - why stop there?
Let's have palette arrays, so that I can define actual color schemes rather than single colours.
@palette desert { #FFFFFF; #B4A183; #D8CB9D; #3DAAD7; }/* referring to it later as: */h3 { color: &desert(1); background-color: &desert(2); }The advantage being the ability to define consistent colour schemes, and have more than one per .css file.
(pardon the code ugliness - how in the world do you force code formatting with no <pre>?
Dave S. - 22nd July 2003 18:28 - #
Yeah, plenty of people are clamouring for that. I doubt we'll see it before CSS 4 though, it's a fairly big change in processing to go into CSS 3 at this stage.
Jim Dabell - 22nd July 2003 18:29 - #
kevin c smith - 22nd July 2003 18:38 - #
It *is* a good idea. Something that we all could make good use of. But to take Dave's comment further, why stop at color? I mean, color *is* probably the most repeated property. But there are other properties which I find myself repeating quite a bit too. Anything from borders to backgrounds to font sizes. The whole idea of setting variables or constants in a style sheet, then being able to reference them later is something I've wanted for a long time.
I've always wished I could somehow combine CSS with something like PHP. The ability to embed a server-side language directly into a style sheet, or be able to link to a .php file (instead of a .css file) and still have it parsed by the server. Then we'd have access to
forloops,ifstatements, and everything else. I know the same thing could be done if the styles were embedded in the header of a normal PHP document, but that's obviously not efficient for styling a whole site, even if the styles came from one include.Douglas Bowman - 22nd July 2003 18:50 - #
You can serve the file produced by PHP as text/css using
header():header("Content-type: text/css");kevin c smith - 22nd July 2003 19:04 - #
I've always wished I could somehow combine CSS with something like PHP.
You can do this, but it takes some server config and/or planning.
One way is to include style blocks in the page <head> rather than linking; all manner of scripting is then exposed, and there's the attendant benefit of self-contained pages. You'd lose alternate media attributes, but the @media construct can be applied within the style block. I don't know how well supported this is.
Another method is to have your server process, say, .css files through the pHp interpreter. They can then contain pHp script and conditionally render declarations based on lots of variables.
I've done this with ASP. Since I'm new to pHp (and don't code much anyhow) I've not given this a thorough shakedown — but I don't see why it wouldn't work. The browser sees what you send it. How you build what you send it is nobody's business, and there should be no bias toward static, linked stylesheets.
Lou Quillio - 22nd July 2003 19:09 - #
Simon Willison - 22nd July 2003 19:11 - #
Addendum: You can see why W3C avoids these proposals. This is scripting. A document contains or incorporates CSS via links, but the document is the unit. Ranging into global variables turns the browser into a programmatic interpreter in ways that it today is not, ways that begin to choke it off, that turns documents into scripts. A document is a document. It should render legibly in the simplest of clients.
Lou Quillio - 22nd July 2003 19:24 - #
Actually, it can be done - if you serve your page as xml - with character entities. Works in Mozilla and Opera. Example: http://home.hccnet.nl/m.wargers/test/charent.xml
I've seen it mentioned before I think in the w3c css mailing list.
martijnw@dolfijn.nl - 22nd July 2003 19:35 - #
OK, I've read through the thread on www-style and there are a number of argumetns against aliases. One of these is that this functionality can already be had by using a preprocessor. I don't buy that at all, for one simple reason: using a preprocessor requires developers to switch from editing a single document to working with two - the "source" document and the "compiled" preprocessed one that must be uploaded. This is a pretty big change in workflow practises for the average web developer (it's less difficult for programmers used to working with source and compiled versions of programs).
By far the best argument against aliases is that they break backwards compatibility with CSS 2. Imagine a document which uses aliases to define some colours - a CSS 2 browser would not know how to resolve the aliases, and would end up using default colours. At best this would make a site look unattractive, and at worst is could lead to unreadable colour combinations such as black text on a black background.
The final argument is that introducing macros to CSS would add a great deal of complexity and change the fundamental nature of style sheets (as Lou said, "This is scripting"). I find this argument less compelling than the backwards compatibility one, but it is still a consideration.
To summarise then: colour aliasing is unlikely to be added to CSS primarily because it breaks backwards compatibility with the current range of browsers.
Simon Willison - 22nd July 2003 19:36 - #
To summarise then: colour aliasing is unlikely to be added to CSS primarily because it breaks backwards compatibility with the current range of browsers.
Of course I defer to Simon's greater understanding of these issues. Yes, backward compatibility is critical to the advance of this medium we're consuming right here, right now.
Still, arresting the programmatic demands placed on user agents is critical to keeping clients thin. That may be a larger concern.
Lou Quillio - 22nd July 2003 19:53 - #
pardon the code ugliness - how in the world do you force code formatting with no <pre>?
This way:
<p style="font-family: monospace; white-space: pre;">code</p>
Add "color: #xxx;", "margin-left: x", anything else. Be kind and use <br />s to keep things from getting too wide. Preview first.
Lou Quillio - 22nd July 2003 20:28 - #
Unless you use a certain Hypertext Pre-processor. The caching problem could be solved by conditional GET or something similar, couldn't it?
anode - 22nd July 2003 20:40 - #
Kristian Hogsberg - 23rd July 2003 00:03 - #
Gary F - 23rd July 2003 16:17 - #
eric scheid - 24th July 2003 05:38 - #
joe - 24th July 2003 17:09 - #
Daniel Glazman - 27th July 2003 13:17 - #
Andrew Brown - 27th July 2003 14:01 - #
Joseph - 6th March 2004 06:26 - #