Skip to content

Instantly share code, notes, and snippets.

@yukulele yukulele/ie-classes.php
Last active Oct 5, 2017

Embed
What would you like to do?
<?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
You can’t perform that action at this time.