About Our New Blog Theme

by studioJMC on June 23, 2010

Once again we’re experimenting with the look and feel of our site. I don’t like “under construction” pages so I hope you’ll bear with me as we move and repurpose some of our content and finalize our new look and feel. It’s the kind of design experience I usually like to rant about rather than create.

{ 0 comments }

Using variables in CSS style sheets

by studioJMC on October 18, 2007

Tutorial Difficulty: Moderate – requires basic CSS and PHP skills, requires PHP enabled webserver

CSS style sheets are wonderful things. They offer greater design control than traditional HTML and make it possible to change the look and feel of an entire website by changing only one file. Of course that power comes at a cost. CSS has a steep learning curve and editing a style sheet, especially one you didn’t create, can be a difficult process.

The good news is there is a solution to those problems that requires only a limited knowledge of PHP scripting to implement: using variables in your style sheets. The bad news is, of course, that CSS doesn’t allow for variables. This post will show you how to get around that limitation.

Using PHP to generate a CSS file.

One advantage of CSS is that your styles can be kept in a separate file from the pages that use them. This is simply a text file that ends with the .css suffix such as style.css. The suffix tells the server to treat the contents of the file as CSS. Unfortunately, the CSS standard doesn’t allow for variables within a .css file. The solution is to use a PHP file, such as style.php and include code to let the server know that the contents of the file should be treated as CSS. This can be accomplished by including the following line of PHP at the top of the style sheet;

<?php header(“Content-type: text/css”); ?>

Your style definitions can then follow so that the top of your style.php document looks something like this:

<?php header(“Content-type: text/css”); ?>

a:link { text-decoration : none; color : # #FF0000; }
a:active { text-decoration : underline; color : # #FF0000; }
a:visited { text-decoration : none; color : # #FF0000; }
a:hover { text-decoration : underline; color : #660000; }

Your .php style sheet will now function exactly like a .css one. We’ll get to the variables in a minute but first…

Linking to your .php style sheet

You can link to your .php style sheet exactly as you would a regular style sheet. Here’s the code I placed in the <head> section of my webpage:

<link href=”style.php” rel=”stylesheet” type=”text/css” />

Using variables in your style.php file

The advantage of a PHP style sheet is that it allows you to define variables and then use those variables throughout your style sheet. So, for the example I gave above where the a:link, a:active and a:visited styles all use the same color (#3366cc), we could define a “$linkColor” variable (The “$” before linkColor simply indicates it is a variable.) and use it in all three styles. The variable definition would look like this:

<?php header(“Content-type: text/css”);
$linkColor = ‘# #FF0000;
?>

Remember all PHP code must be contained within a php tag which opens with “<?php” or simply “<?” and closes with “?>”

We can now insert the variable into our css code like this:

<?=$linkColor?>

Notice the opening and closing php tags.

So the beginning of our CSS file now looks like this:

<?php header(“Content-type: text/css”);
$ linkColor = ‘# #FF0000;
?>

a:link { text-decoration : none; color : <?=$ linkColor?>; }
a:active { text-decoration : underline; color : <?=$ linkColor?>; }
a:visited { text-decoration : none; color : <?=$ linkColor?>; }
a:hover { text-decoration : underline; color : #000021; }

Now to change the color of the a:link, a:active and a:visited links from red (#FF0000) to blue (#0000FF) we need only change the definition of $linkColor:

<?php header(“Content-type: text/css”);
$ linkColor = ‘# #0000FF;
?>

A word about variables and variable names

It should be easy to see how this trick can make editing your style sheets a lot easier. What’s less obvious (at least initially) is that, if you create a variable for every possible style, your variable list will soon become nearly as complicated and confusing as your original style sheet. Here are a couple tips to keep things simple:

  1. Build your color palette carefully so that you can color as many pieces of the design as possible using the same variable. For instance, if your header, footer and sidebar will be the same color, you can use the same variable (perhaps $secondColor) to define them.
  2. Don’t be too specific with your variable names. You don’t want to use the variable name $red, if you might be changing it to blue later on. Similarly, you probably don’t want to use the variable name $headerColor if the variable is also used for the footer and sidebar. I prefer names such as $themeColor, $hiliteColor, etc.
  3. Plan carefully so that you don’t force yourself into a situation where your type and background have insufficient contrast. In spite of #1 above, you might sometimes need to define more than one variable with the same color to provide future flexibility.
  4. Use PNG graphics for images that are partially transparent. Since PNGs offer true transparency, you can create a drop shadow that will work over any background color. That’s important if you’ll be changing those background colors. Just remember, you need to use a png javascript fix for PNGs to display properly in Explorer 6.

That should get you started. In the next post, I’ll show how to take this one step further by using style variables in WordPress blog themes.

{ 2 comments }

Monitor Resolutions Revisited

October 2, 2007

I recently had occasion to take another look at the screen resolutions of visitors to the websites I monitor. I used Google Analytics to check the percentage of user with 800 x 600 pixel monitors who visited 9 sites during September. While the results weren’t surprising, they did represent a milestone. For the first time [...]

Read the full article →

Adobe CS3: One man’s bloat is another man’s feature

September 24, 2007

I recently became involved in an interesting thread in the AIGA Design Education online discussion group. Basically the topic was the increasing complexity and bloating of design software. Here’s a few relevant comments from the discussion:

Read the full article →

800 by 600 pixel monitors: An endangered species

August 17, 2007

Back in June I mentioned being surprised by the low number of visitors to this site using 800 x 600 pixel monitors. At the time the exact number was zero. Well it’s increased a bit since then. Now a whopping 0.67% of visitors here have the mini-monitors.

Read the full article →

Coding IS Part of Web Design

July 6, 2007

My recent post, 50 Common Web Design Mistakes elicited this comment from Wild over at Digg: These are programming mistakes. Not DESIGN mistakes. Maybe I sound a bit arrogant, but their [sic]is a difference between a web designer and a web programmer. Its [sic] one the industry needs to understand as it leads to a [...]

Read the full article →

Design Consideration: Monitor Resolutions

July 5, 2007

While putting this site together, my son and I engaged in an argument about the ideal width of web pages. Based upon the standard advice I give my web design students I argued for a 760 pixel width which, even with scroll bars, can easily be accommodated on an 800 by 600 pixel monitor. He, [...]

Read the full article →

50 Common Web Design Mistakes

June 26, 2007

This started out as a “Top Ten Newbie Web Mistakes” for my beginning web design students but it quickly became obvious that I couldn’t limit it to only ten. I was finally able to edit it down to 50. But I suspect, as soon as the comments start, it will begin growing again. And, yes, [...]

Read the full article →

My favorite Google Search

June 8, 2007

“Untitled Document” is the default page title for web pages created in Dreamweaver and a variety of other web page editors. According to Google “about 32,900,000″ with that title now populate the web. And, if you’re interested (which apparently few people are), according to Yahoo! the number is “about 44,700,000.”

Read the full article →

WTF is Experience Design?

June 7, 2007

I’m a bit behind in my design jargon, so I have to admit that this “experience design” thing kind of crept up on me. I probably would have blissfully ignored the whole phenomenon if I hadn’t discovered that the local AIGA design awards offered eight categories for ink on paper and grouped everything else – [...]

Read the full article →