You are here: Templates Club arrow Blog arrow PHP conditional comments
PHP conditional comments
Thursday, 18 January 2007

As web developers we often have to use different CSS techniques for different browser types. Now days every browser has it's own hack but imagine if you have to use different CSS hack for each browser there is and load html conditional comments for each! That would be to much code in head tag . To keep it neat and load conditional comment only when specific browser is used here is a small PHP snippet that can help keep your code organized.

First we place the browser detection snippet in our index.php file :

$who = strtolower($_SERVER['HTTP_USER_AGENT']);

Than we load the conditional comment to be used only if visitor is using IE6 browser :

if(preg_match("/msie 6.0/", $who)) { echo'
<style type="text/css">
#mydiv{
padding:5px;
}
</style>';
}

To load different style for other browsers simply change the browser name in first line of code

if(preg_match("/chrome/", $who)) { 

Here is the list of other browsers you can use:

mozzila // Firefox

msie 7.0 // Internet Explorer 7.0

msie 8.0 // Internet Explorer 8.0

opera // Opera 

chrome // Chrome

safari// Safari

 

 

Add comment


Security code
Refresh