Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Dynamic CSS with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
MISCELLANEOUS

Dynamic CSS with PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    2004-06-23

    Table of Contents:
  • Dynamic CSS with PHP
  • Grouping Browser Style Sheets
  • Environment Aware Style Sheets
  • Conclusion

  • 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


    Dynamic CSS with PHP - Environment Aware Style Sheets


    (Page 3 of 4 )

    More than once I've run into situations where I wish style sheets supported simple arithmetic. Suppose I want my style sheet to set the height of a text div 100 pixels less than the viewport's height because of some graphical banner on the page. If I know I have a window of a specific size, I could do the math and hardcode the numbers by hand. However, that isn't always the case.

    Previously I would have scrounged the web searching for some JavaScript code to detect the viewport's height and then made my modifications to readjust the element's properties once the page has loaded:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
      "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <html>
      <head>
        <title>Sample HTML</title>
        <link rel="stylesheet" href="styles.css" type="text/css" />    
        <script language="JavaScript" type="text/javascript">
        <!--
          function sizeAdjustment() {
            var myHeight = 0;

            if (typeof(window.innerHeight) == "number")
              /* Non-IE */
              myHeight = window.innerHeight;
            else if (document.documentElement &&
            document.documentElement.clientHeight)
              /* IE */
              myHeight =
              document.documentElement.clientHeight;

            document.getElementById('body').style.height =
            (myHeight - 100 + "px");
          }
        //-->
        </script>
      </head>
      <body onload="sizeAdjustment(); return true;">
      ...
      </body>
    </html>

    The resize of the body object degrades nicely in some browsers but causes serious display issues in others.

    Instead, I could detect the browser's viewing area and append the values to the URL when calling the style sheet.

    <html>
      <head>
        <title>Sample HTML</title>
        <link rel="stylesheet" href="styles.css" type="text/css" />    
        <script language="JavaScript" type="text/javascript">
        <!--
          var myHeight = 0;

          if (typeof(window.innerHeight) == 'number')
            /* Non-IE */
            myHeight = window.innerHeight;
          else if (document.documentElement &&
          document.documentElement.clientHeight)
            /* IE */
            myHeight =
            document.documentElement.clientHeight;

          myHeight = myHeight - 100;

          document.write('<link rel="stylesheet" ',
          'type="text/css" href="styles-css.php?height=',
          myHeight, '" />');
        //-->
        </script>
      </head>
      <body>
       ...
      </body>
    </html>

    The JavaScript calculates the height and constructs a link to the style sheet that passes along the value. The style sheet can read the incoming values and adjust itself before being sent to the browser.

    <?php
    header
    ("Content-Type: text/css");
    echo 
    "body {height: $_GET[height]; }";
    ?>

    Ingenious... but definitely an ugly hack. Still though it illustrates how JavaScript can communicate back to PHP on the server and how PHP can tailor a unique style sheet to fit a given situation.

    More Miscellaneous Articles
    More By bluephoenix


       · Then to hide the fact that your using dynamic styles you could use .htaccess and...
       · Thank you very much for this very usefull tutorial.The only problem I had was the...
       · I created a free CSS library that extends this idea. It allows you to:1) Use...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek