Link Details

Link 88183 thumbnail
User 247828 avatar

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.
  • 7
  • 2
  • 848
  • 287

Comments

Add your comment
User 239201 avatar

amphi replied ago:

0 votes Vote down Vote up Reply

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 */

User 247828 avatar

jawngee replied ago:

0 votes Vote down Vote up Reply

How would you see that happening?

.left { width:245px; }
.right { width:185px; }
.middle { width:expr(.left.width + .right.width) }

?

User 239201 avatar

amphi replied ago:

0 votes Vote down Vote up Reply

//comments like this get stripped from the output

/*comments like this remain*/

//assignments outside selectors=variables
//unit aware variables that is
LC_width:180px;
LC_hpadding:10px;
CC_hpadding:20px;
RC_width:130px
RC_hpadding:10px;
LC_fullwidth:LC_width+LC_hpadding;
RC_fullwidth:RC_width+RC_hpadding;

body {
//the assigned stuff gets split into tokens with space as delimiter
//since there is no space this whole thing is evaulated to 630px
min-width: 2*(LC_fullwidth+CC_hpadding)+RC_fullwidth;
}

#container {
padding-left: LC_fullwidth;
padding-right: RC_fullwidth+CC_hpadding;
}

#container .column {
position: relative;
float: left;
}

#center {
padding: 10px CC_hpadding;
width: 100%;
}

#left {
width: LC_width;
padding: 0 LC_hpadding;
right: LC_fullwidth+CC_hpadding;
margin-left: -100%;
}

#right {
width: RC_width;
padding: 0 RC_hpadding;
margin-right: -100%;
}

#footer {
clear: both;
}


Something like that would be great.

User 247828 avatar

jawngee replied ago:

0 votes Vote down Vote up Reply

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:


#right { width: eval(RC_width * 2); }

User 247828 avatar

jawngee replied ago:

1 votes Vote down Vote up Reply

It's up there. You can now do:


@variables {
lc_width: 240px;
rc_width: 180px;
all_padding: 20px;
}

#some_div {
width: eval((lc_width+rc_width)-(all_padding*2));
padding: eval(all_padding/2);
}


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.

User 247828 avatar

jawngee replied ago:

0 votes Vote down Vote up Reply

Err *can't* add variables of different measurement types.

User 239201 avatar

amphi replied ago:

0 votes Vote down Vote up Reply

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. :)

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (7)



Voters Against This Link (2)