Testing for an HTML 5 and CSS 3 Web Browser with Modernizr

By Don Burnett at July 08, 2009 00:39
Filed Under: HTML 5

From http://www.modernizr.com/ 

“What is Modernizr?

Have you ever wanted to do if-statements in your CSS for the availability of cool features like border-radius? Well, with Modernizr you can accomplish just that! The syntax is very intuitive, too:

body.multiplebgs div p {
  /* properties for browsers that
     support multiple backgrounds */
}
body.no-multiplebgs div p {
  /* optional fallback properties
     for browsers that don't */
}

Modernizr is a small and simple JavaScript library that helps you take advantage of emerging web technologies (CSS3, HTML 5) while still maintaining a fine level of control over older browsers that may not yet support these new technologies.

Modernizr uses feature detection to test the current browser against upcoming features like rgba(), border-radius, CSS Transitions and many more. These are currently being implemented across browsers and with Modernizr you can start using them right now, with an easy way to control the fallbacks for browsers that don’t yet support them.

Additionally, Modernizr creates a self-titled global JavaScript object which contains properties for each feature; if a browser supports it, the property will evaluate TRUE and if not, it will be FALSE.

Lastly, Modernizr also adds support for styling HTML 5 elements. This allows you to use more semantic, forward-looking elements such as <section>, <header> and <dialog> without having to worry about them not working in Internet Explorer.

Modernizr does not add missing functionality to browsers; instead, it detects native availability of features and offers you a way to maintain a fine level of control over your site regardless of a browser’s capabilities….

How to use Modernizr

First, you need to include Modernizr directly after the <body> element in your page . Modernizr will then add classes to the body that indicate which features are and aren't supported in the current browser, allowing your CSS rules to trigger off of them. In essence, you can then write CSS rules like the example mentioned above.

Similarly, you can use the JavaScript object that gets created to do if-conditionals and fork your JS code to leverage native implementations where available.

For more information on how to use Modernizr, please look at the detailed tutorial.

  1. Canvas and CSS Columns support is still very rudimentary and should be used with caution.
  2. The reason it is included in that exact spot is because first of all, the <body> element must be present, but even more importantly, waiting for DOM readiness might result in a partial page rendering, which is undesirable since Modernizr will trigger style changes depending on the browser’s capabilities…

Modernizr detects support for:

  • Canvas
  • rgba()
  • hsla()
  • border-image:
  • border-radius:
  • box-shadow:
  • Multiple backgrounds
  • opacity:
  • CSS Animations
  • CSS Columns
  • CSS Gradients
  • CSS Reflections
  • CSS Transforms
  • CSS Transitions

Additionally, Modernizr will allow you to use elements from the HTML 5 spec and style them, even in Internet Explorer. Learn more about styling HTML5 elements and see below on how to use Modernizr in your website.

  • Version: 0.9
  • Compressed size: 6kb

Download 16kb uncompressed version 

or go to http://modernizr.com

Modernizr.com in all browsers:
  • Safari 4
  • Firefox 3.5
  • Opera 10 beta 1
  • IE8
follow @Modernizr on Twitter
Comments are closed

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

About Don Burnett

Name of authorDon is a user experience designer and developer who enjoys spending his time creating Rich Internet Applications and Multimedia creations in XAML, with Expression Blend and Expression Studio.

He has worked in the field of Windows Development and Multimedia design for over 20 years. This includes work for such companies as Media Station, Disney Software, Universal, MediaOne, and many others. Don specializes in training and support, design and development, in WPF and Silverlight.. 

Don currently offers design and development, consulting, and training services to both individuals and businesses. Don is also a Silverlight and Blend and WPF Insider.

Don was awarded the Microsoft MVP award for Expression Blend for 2009-2010. And formerly the Expression Studio award winner for 2008-2009 and was the former co-moderator and content editor for Microsoft's original Phizzpop.com site that Introduced Expression Studio Products and WPF and Silverlight to the world.

Don is also a co-founder/partner in Calder Entertainment and Media Corporation involved with Real-time Interactive Social Business Design and Social Branding consulting with http://calderentertainment.com.

 

Interactive Realtime Social Business Design and Social Branding

 

Lijit Search