Detecting advanced CSS features

Published:

I decided to do some fancy typography in my résumé, but I wanted to have a sane fallback for browsers that don't support the latest features. I have my name rotated 90 degrees using CSS3 transforms, but simply applying the transform: rotate(-90deg) CSS property isn't enough to get the effect I want. To make it look right I also need to measure the text (since its dimensions will change depending on the viewer's fonts) and absolutely position it based on that. Standard CSS fallback (where unrecognized rules are ignored) doesn't give a good result. At all.

After a bunch of fiddling and cross-browser testing I found that testing the existence (!==undefined) of properties on element.style worked well. So I put all of my rotation rules in a CSS class that's applied only if the style properties exist.

<style type="text/css">
  h1.rotated {
    -webkit-transform-origin: left bottom;
    -moz-transform-origin: left bottom;
    -o-transform-origin: left bottom;
    transform-origin: left bottom;

-webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); -o-transform: rotate(-90deg); transform: rotate(-90deg);

position: absolute; left: 0px; top: 0px; margin-top: 16px; } </style> <script type="text/javascript"> // rotate the title, if that's even possible var h1 = document.getElementsByTagName('h1')[0]; var s = document.body.style;

// test for the presence of CSS3 transform properties if (s.transform !== undefined || s.WebkitTransform !== undefined || s.MozTransform !== undefined || s.OTransform !== undefined) { // move h1.setAttribute('style', 'top:' + ( h1.offsetWidth - h1.offsetHeight) + 'px'); // and rotate h1.setAttribute('class', 'rotated'); } </script>

Of course, if I could just check for transform: support rather than -webkit-transform:, -moz-transform: and -o-transform: but we're not there yet. Anyway, based on browsershots.org it seems to do the right thing on every known browser. It shows rotated text in very recent Webkit and Gecko browsers, and in Opera nightlies, and unrotated, correctly text everywhere else.