Home arrow PHP arrow Page 4 - PHP Browser Detection and Appropriate CSS Generation
PHP

PHP Browser Detection and Appropriate CSS Generation


In this article, Tim explains his browser detection utility and how you can use it to generate the right CSS for each user.

Author Info:
By: Tim Perdue
Rating: 4 stars4 stars4 stars4 stars4 stars / 45
February 16, 2003
TABLE OF CONTENTS:
  1. · PHP Browser Detection and Appropriate CSS Generation
  2. · CSS Tips
  3. · Browser Detection Library
  4. · Font Size
  5. · Conclusion

print this article
SEARCH DEVARTICLES

PHP Browser Detection and Appropriate CSS Generation - Font Size
(Page 4 of 5 )

There, now we can start using that library to determine font sizes.

Internet Explorer on Wintel has the biggest default font by a long shot. If you don't specify a font size, Wintel users will get huge fonts and they'll yell to no end, even though it's caused by their own browser settings.

Beyond IE on Windows, other browsers like Netscape also have big fonts. Macs for some reason buck this trend and render fonts very small, so you need to set the font up a couple steps or they'll be angry.

The "other" platforms, like linux, unix, etc, come last. I generally set the fonts about the same as I do for Netscape on Windows and I haven't seen too many complaints.

Here's the library. Notice that the CSS sheet itself is generic, but the font-size: on that sheet is a variable which is set at the top of the code.

<?php

function css_site() {

  //determine font for this platform
  if (browser_is_windows() && browser_is_ie()) {

    //ie needs smaller fonts than anyone else
    $font_size='x-small';
    $font_smaller='xx-small';
    $font_smallest='7pt';

  } else if (browser_is_windows()) {

    //netscape or "other" on wintel
    $font_size='small';
       $font_smaller='x-small';
    $font_smallest='x-small';

  } else if (browser_is_mac()){

    //mac users need bigger fonts
    $font_size='medium';
    $font_smaller='small';
    $font_smallest='x-small';

  } else {

    //linux and other users
    $font_size='small';
    $font_smaller='x-small';
    $font_smallest='x-small';

  }

  $site_fonts='verdana, arial, helvetica, sans-serif';

  ?>
  <STYLE TYPE="text/css">
  <!--
  BODY, TH, TD, OL, UL, LI { font-family: verdana, arial, helvetica, sans-serif; font-size: <?php echo $font_size; ?>; }
  H1 { font-size: 175%; font-family: <?php echo $site_fonts; ?>; }
  H2 { font-size: 150%; font-family: <?php echo $site_fonts; ?>; }
  H3 { font-size: 125%; font-family: <?php echo $site_fonts; ?>; }
  H4 { font-size: 100%; font-family: <?php echo $site_fonts; ?>; }
  H5 { font-size: 75%; font-family: <?php echo $site_fonts; ?>; }
  H6 { font-size: 50%; font-family: <?php echo $site_fonts; ?>; }
  PRE, TT, CODE { font-family: courier, sans-serif; font-size: <?php echo $font_size; ?>; }
  A:hover { text-decoration: none; color: #FF6666; font-size: <?php echo $font_size; ?>; }
  A.menus { color: #FF6666; text-decoration: none; font-size: <?php echo $font_smaller; ?>; }
  A.menus:visited { color: #FF6666; text-decoration: none; font-size: <?php echo $font_smaller; ?>; }
  A.menus:hover { text-decoration: none; color: #FF6666; background: #ffa; font-size: <?php echo $font_smaller; ?>; }
  A.menussel { color: #FF6666; text-decoration: none; background: #ffa; font-size: <?php echo $font_smaller; ?>; }
  A.menussel:visited { color: #FF6666; text-decoration: none; background: #ffa; font-size: <?php echo $font_smaller; ?>; }
  A.menussel:hover { text-decoration: none; color: #FF6666; background: #ffa; font-size: <?php echo $font_smaller; ?>; }
  A.menusxxs { color: #FF6666; text-decoration: none; font-size: <?php echo $font_smallest; ?>; }
  A.menusxxs:visited { color: #FF6666; text-decoration: none; font-size: <?php echo $font_smallest; ?>; }
  A.menusxxs:hover { text-decoration: none; color: #FF6666; background: #ffa; font-size: <?php echo $font_smallest; ?>; }
  -->
  </STYLE>
  <?php
}

?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials