Posts by Category
Recent Articles
Make every website display perfectly in every browser! A PHP script that provides a simple way to accomplish this feat without spending countless hours on one style sheet. The best way to do this is to deliver the user a style sheet that perfectly matches their browser.
If you are like me, you hate having to make one CSS stylesheet work for every browser. Of course its much easier to adjust the stylesheet to work for Opera, Firefox and Safari than it is for Internet Explorer, especially Internet Explorer 6.
The reason this is so difficult is because different browsers render web pages differently. Microsoft, the makers of the notorious Internet Explorer series, do not believe they have to comply with with the W3C. The W3C sets the standards for the web so that their is uniformity among web designers and developers.
Alright, so if you want to deliver a different CSS file for different users, their are two options. You can use Javascript or Serverscripting. Javascript works ok, as long as the user does not have javascript disabled. Most user do not, however some users do disable javascript for various reasons. With javascript disabled, the correct CSS file would not be delivered. Server scripting on the other hand, works every time. The user is not able to turn it off.
The server detects what browser the user is using. It looks in the css.php file and sends the correct version of the CSS file to the user’s browser, changing the header of the file to ‘css.css’.
I use a series of includes inside of the file to include the proper CSS file. You can however, just keep all of the CSS in the file without the includes if you don’t like switching back and forth between files, its just a personal preference.
In order to use the php file simply link to it like you would a CSS file in the head section:
-
-
<link rel="stylesheet" href="css.php" type="text/css" />
-
I did not write this script, I found it open source somewhere on the web, and I have not been able to locate it again. If you know where the original is, please let me know so I can link to it.
-
-
<?php
-
-
-
-
-
-
-
"MSIE", // parent
-
-
-
"OPERA",
-
-
-
"MOZILLA", // parent
-
-
-
"NETSCAPE",
-
-
-
"FIREFOX",
-
-
-
"SAFARI"
-
-
-
);
-
-
-
$info[browser] = "OTHER";
-
-
-
foreach ($browser as $parent) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
$info[browser] = $parent;
-
-
-
$info[version] = $version;
-
-
-
}
-
-
-
}
-
-
-
?>
-
-
-
-
<?php
-
-
-
if (($info[browser] == "MSIE") && ($info[version] == 7)) {
-
-
-
-
-
} else if (($info[browser] == "MSIE") && ($info[version] < 7)) {
-
-
-
-
-
} else if ($info[browser] == "FIREFOX") {
-
-
-
-
} else if ($info[browser] == "SAFARI") {
-
-
-
-
} // you can add the other browsers in the same manner if you like
-
else {
-
-
-
-
-
}
-
-
-
?>
-
-
-
}
-
Give it a try, if you have any problems or questions, let me know and I will do my best to answer them ![]()