Skip to content

Instantly share code, notes, and snippets.

@yukulele
Last active October 5, 2017 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukulele/11209552 to your computer and use it in GitHub Desktop.
Save yukulele/11209552 to your computer and use it in GitHub Desktop.
<?php
function ieClasses( $min = 6, $max = 11 ) {
$classes = array();
preg_match("/msie (\d+)/i", $_SERVER['HTTP_USER_AGENT'], $match);
if( count( $match ) > 0 ){
$version = +$match[1];
$classes[] = 'ie';
$classes[] = 'ie' . $version;
for( $i = $min ; $i <= $max ; $i++ ){
if( $version < $i )
$classes[] = 'lt-ie' . $i;
else
$classes[] = 'gte-ie' . $i;
if( $version > $i )
$classes[] = 'gt-ie' . $i;
else
$classes[] = 'lte-ie' . $i;
}
}else{
$classes[] = 'no-ie';
}
return implode( ' ', $classes );
};

IE classes PHP

ie-classes.php generate CSS classes like IE conditional comments to add to <html> class attribute.

There is also a client side version (javascript) : https://gist.github.com/yukulele/11185298

Usage

PHP

<?php
require "ie-classes.php";
?><!doctype html>
<html class="no-js <?=ieClasses()?>">

CSS

html.ie h1 {
	color: red;
}

html.ie8 h1 {
	color: orange;
}

html.gte-ie9 h1 {
	color: blue;
}

html.no-ie h1 {
	color: green;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment