By jawngee
via interfacelab.com
Published: Jun 18 2008 / 09:37
Back in April of 2008, I came across a proposal by Daniel Glazman and David Hyatt for using variables in CSS stylesheets. I thought the proposal was absolutely brilliant, filling a much needed void for sites using complicated stylesheets across a variety of different pages. Another part of their proposal was being able to include/import other stylesheets. I don’t know anyone that couldn’t find this useful.
I put together a quick class for implementing most of their proposal using PHP. Nothing fancy going on here, most of it is some simple regexes.
Comments
amphi replied ago:
As I already said elsewhere... that proposal (this implementation is based on) lacks math ops. I really hate having to write stuff like this:
padding-right:239px; /* RC fullwidth + CC padding */
jawngee replied ago:
How would you see that happening?
.left { width:245px; }
.right { width:185px; }
.middle { width:expr(.left.width + .right.width) }
?
amphi replied ago:
Something like that would be great.
jawngee replied ago:
That would be pretty easy, might have something for you tomorrow (assuming you use PHP).
Have to throw an exception if you were adding units of differing types, eg. 2em + 20px + 10%
I'd also probably stick with eval(expression) syntax to keep it consistent with var(), eg:
jawngee replied ago:
It's up there. You can now do:
You can only do this with px/em/% measurement units and the units must match. For instance you can add a variable that is 2em with another that is 120px. Doing so will throw an exception. Also, if you use a variable reference that doesn't exist, it will also throw an Exception.
jawngee replied ago:
Err *can't* add variables of different measurement types.
amphi replied ago:
You can also display error messages by injecting em into the CSS like this:
body:before{content:'line one\A line two';
border:2px solid #f00;
background:#fff;
text-align:left;
font-size:14px;
font-family:'courier new',courier,monospace;
margin:0;
padding:5px;
display:block;
white-space:pre;
}
Well, looks great so far. Even if it's a tad verbose. Thanks for your hard work. :)
Voters For This Link (6)
Voters Against This Link (2)