Style Sheets
  Home arrow Style Sheets arrow CSS Constants
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

CSS Constants
By: Chris Heilmann
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2005-05-09

    Table of Contents:
  • CSS Constants
  • The CSS standard compliant approach
  • Using ID Selectors and Descendent Selectors
  • Moving server side
  • Using Server Side Scripting Languages
  • More examples
  • Parsing CSS with PHP

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    CSS Constants


    (Page 1 of 7 )

    One feature designers often wished they had with style sheets are constants –- the chance to define something once and reuse it over and over in the style sheet document. This article shows some techniques for how to achieve that and discusses their pros and cons.

    A constant nagging

    When reading CSS related forums or mailing lists, you’ll sooner or later find someone asking how they can define a “constant” in CSS –- something to define once in the CSS document and reuse throughout it.

    There are several reasons why there are no constants in CSS:

    • The CSS specifications didn’t plan for any constants, variables, loops or conditions simply because CSS is there to describe the visual presentation of a document, not programming logic. The same applies to HTML –- there are no variables, constants, loops and conditions. Its job is to describe what a certain text is, within this document, regardless of what variables were sent to it.
    • CSS is “executed” on the client -– the browser. Browsers on slower computers already have problems rendering CSS properly. Fixed positioning and big background graphics can cause delays and flickering. What would happen if they had to process the “CSS logic” on top of that?
    • Any scripting on the client side is, however low, a security threat and allows for exploits. Microsoft Internet Explorer’s ability to embed Javascript in CSS files proved that.

    However, CSS constants could be something very handy indeed.

    Possible Uses

    CSS constants would make it easier to turn style guides into CSS and allow for smaller style sheet documents without much repetition. You define the colors once and add the constant name every time you want to use it.

    /* Company Colours */ 
    $blue=’#369’;
    $green=’#363’;
    $lgreen=’#cfc’;
     
    […]
     
    ul#navigation{
            background:$blue;
            color:#fff;
    }
    h1{
            border-bottom:1px solid $green;
    }

    White-labelling and redesign would be a breeze; all you need to change is the constants, and all the styles comply with the new colors. No more search and replace sessions, and the inevitable setting that escaped them.

    Another option would be to define often used styles and keep the document shorter.

    $boxstyles=’background:#ccc;border-top:1px solid #eee; 
    border-left:1px solid #eee; border-right:1px solid #aaa;
    border-bottom:1px solid #aaa;padding:.5em;margin:.5em 0’;
     
    h1{
            $boxstyles
            color:#000;
    }
    div#extras div {
            $boxstyles
            font-size:80%;
    }

    Since CSS does not allow for that, we need to find a way around the problem. One is to use what CSS does provide us with.

    More Style Sheets Articles
    More By Chris Heilmann


     

    STYLE SHEETS ARTICLES

    - Creating Gradients for Individual Containers...
    - Creating Gradients for Web Page Headers with...
    - SEO Scrolling Frames Problem Solved
    - Building Cross-Browser Background Effects wi...
    - CSS: Pseudo
    - Using PNG Images to Build Background Effects
    - CSS: Continuing the Clarification of CSS Cla...
    - CSS: Top Secret Classification
    - CSS: Dimensions
    - CSS: Margins and Padding
    - CSS: Crossing the Border
    - CSS: Text, Fonts, and Tables
    - CSS: Working with Text
    - CSS: Backgrounds
    - CSS for the Newbie


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway