Style Sheets
  Home arrow Style Sheets arrow Page 5 - CSS Constants
IBM Rational Software Development Conference
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  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Developerworks
 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
     
     
     
    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 - Using Server Side Scripting Languages
    (Page 5 of 7 )

    Server side languages, such as PHP, JSP, ASP, ASPX, Cold Fusion, ModPerl, Perl, JSP and so forth, come with the ability to send the header information of the current document. Furthermore, they feature everything we need –- Variables, Constants, Loops, Conditions and Objects.

    To tell, for example, a PHP script to send its output as CSS to the browser, we use the following two lines of code:

    header('content-type:text/css');
    header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 

    The first line defines the content as CSS, a style sheet. The second sets the expiry date of the document to the current time and a bit; that way, the output of the script will still be cached by the browser. Without this line, the CSS would never be cached, which can be a performance problem depending on the size of the resulting style sheet document.

    You can create anything with the right header information: Javascript, images, PDFs, Excel Sheets, Flash files, you name it. Header can also help us to trigger the download of files rather than the display of them in the browser.

    The only important thing is that the header output has to be the first output of the script. Any output before that –- including whitespace -– will cause an error.

    Content types can only be set once; you cannot put out Javascript and later on CSS (unless you write them out inline) in one script.

    Harnessing the power of the header function, we now have the whole specification of our server side language at our disposal. We can define and reuse variables, change them according to parameters sent to the page, automatically set them according to the location on the server and so on.

    Our above example in PHP would be:

    <?php
    header('content-type:text/css');
    header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 
    /* Company Colours */ 
    $blue=’#369’;
    $green=’#363’;
    $lgreen=’#cfc’;
    ?>
     
    ul#navigation{
            background:
    <?php echo $blue;?>;
            color:#fff;
    }
    h1{
            border-bottom:1px solid 
    <?php echo $green;?>;
    }

    If the PHP setup allows for shortcut notation we could also use:

     
    <?php
    header('content-type:text/css');
    header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 
    /* Company Colours */ 
    $blue=’#369’;
    $green=’#363’;
    $lgreen=’#cfc’;
    ?>
     
    ul#navigation{
            background:
    <?=$blue;?>;
            color:#fff;
    }
    h1{
            border-bottom:1px solid 
    <?=$green;?>;
    }

    Another way is to use the print command to output until a certain label is reached; this one does mess with the color coding of some editors and might be –- depending on the size of the CSS –- straining the processor:

    <?php
    header('content-type:text/css');
    header("Expires: ".gmdate("D, d M Y H:i:s", (time()+900)) . " GMT"); 
    /* Company Colours */ 
    $blue='#369';
    $green='#363';
    $lgreen='#cfc';
     
    print <<<ENDCSS
    ul#navigation{
            background:
    $blue;
            color:#fff;
    }
    h1{
            border-bottom:1px solid 
    $green;
    }
    ENDCSS;
    ?>

    More Style Sheets Articles
    More By Chris Heilmann


     

    STYLE SHEETS ARTICLES

    - 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
    - Styling Web Page Headers with Transparent Ba...
    - Creating Angled Corners with Transparent Bac...
    - Style Sheets for a Useful Links Page
    - Modifying the Look and Feel of Individual El...
    - Using Persistent Styles with Multiple Style ...






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