PHP CSS Browser Selector

Update

You can find a more feature rich version of the browser detector in my little PHP toolkit, called Kirby

Rafael Lima's CSS Browser Selector in PHP

CSS Browser Selector is a very small javascript with just one line php function which empower CSS selectors. It gives you the ability to write specific CSS code for each operating system and each browser. Quoted from Rafael's website

Example

The background of this page will change on different os/browsers:

yellow
Internet Explorer
orange
Internet Explorer 7
red
Gecko Engine on Windows (Firefox, Mozilla, Camino)
pink
Gecko Engine on Linux (Firefox, Mozilla, Camino)
gray
Gecko Engine on Other OS (Firefox, Mozilla, Camino)
green
Opera
blue
Konqueror
black
Safari
cyan
Chrome

CSS of this example

.ie body {
  background-color: yellow
}
.ie7 body {
  background-color: orange
}
.gecko body {
  background-color: gray
}
.win.gecko body {
  background-color: red
}
.linux.gecko body {
  background-color: pink
}
.opera body {
  background-color: green
}
.konqueror body {
  background-color: blue
}
.webkit body {
  background-color: black
}
.chrome body {
	background-color: cyan
}

Download

css_browser_selector.zip (4 kb)

Usage

Include css_browser_selector.php in your app <?php require("css_browser_selector.php"); ?> …and add the following to your html template. <html class="<?php echo css_browser_selector() ?>"> HTML-Output (for your browser): <html class="">

Available OS Codes

Available Browser Codes

PHP vs. JS

The advantage of this PHP version is that there's no additional request needed to include the javascript file and the selectors are available immediately.

The javascript version includes a client javascript detection with the js selector. This is not available in the PHP version.

Inspiration

As mentioned above this is just a port to PHP. The original javascript file has been created by Rafel Lima who's got the idea from a blog post by 37signals

License

Rafel has released his code under the following license and so do I: http://creativecommons.org/licenses/by/2.5/

Author

Bastian Allgeier bastian-allgeier.de

Follow me on Twitter twitter.com/bastianallgeier