Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:34
Show Gist options
  • Save AndersDJohnson/4667784 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/4667784 to your computer and use it in GitHub Desktop.
IE classes for conditional HTML tags
<?php
function get_ie_classes($ie) {
$classes = array('ie');
if ($ie >= 7 && $ie <= 9) {
$classes[] = 'ie-' . $ie;
}
for ($i = 6; $i < $ie; ++$i) {
$classes[] = 'ie-gt-' . $i;
$classes[] = 'ie-gte-' . ($i + 1);
}
for ($i = 10; $i > $ie; --$i) {
$classes[] = 'ie-lt-' . $i;
$classes[] = 'ie-lte-' . ($i - 1);
}
return implode(' ', $classes);
}
<!DOCTYPE html>
<!--[if lt IE 7]><html class="ie ie-lt-10 ie-lte-9 ie-lt-9 ie-lte-8 ie-lt-8 ie-lte-7 ie-lt-7 ie-lte-6 no-js"><![endif]-->
<!--[if IE 7]><html class="ie ie-7 ie-gt-6 ie-gte-7 ie-lt-10 ie-lte-9 ie-lt-9 ie-lte-8 ie-lt-8 ie-lte-7 no-js"><![endif]-->
<!--[if IE 8]><html class="ie ie-8 ie-gt-6 ie-gte-7 ie-gt-7 ie-gte-8 ie-lt-10 ie-lte-9 ie-lt-9 ie-lte-8 no-js"><![endif]-->
<!--[if IE 9]><html class="ie ie-9 ie-gt-6 ie-gte-7 ie-gt-7 ie-gte-8 ie-gt-8 ie-gte-9 ie-lt-10 ie-lte-9 no-js"><![endif]-->
<!--[if gt IE 9]><html class="ie ie-gt-6 ie-gte-7 ie-gt-7 ie-gte-8 ie-gt-8 ie-gte-9 ie-gt-9 ie-gte-10 no-js"><![endif]-->
<!--[if !IE]><!--><html class="not-ie no-js"><!--<![endif]-->
<!--
...
-->
</html>
<!DOCTYPE html>
<!--[if lt IE 7]><html class="<?php echo get_ie_classes(6); ?> no-js"><![endif]-->
<!--[if IE 7]><html class="<?php echo get_ie_classes(7); ?> no-js"><![endif]-->
<!--[if IE 8]><html class="<?php echo get_ie_classes(8); ?> no-js"><![endif]-->
<!--[if IE 9]><html class="<?php echo get_ie_classes(9); ?> no-js"><![endif]-->
<!--[if gt IE 9]><html class="<?php echo get_ie_classes(10); ?> no-js"><![endif]-->
<!--[if !IE]><!--><html class="not-ie no-js"><!--<![endif]-->
<!--
...
-->
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment