Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Code for Tomorrow’s Web Today Using Modernizr

Web developers have been watching the developement of the HTML 5 and CSS 3 specifications for some time. Between the two specs, many of today’s complex web design headaches — like creating rounded corners or animating page elements — can be solved with a single line of code.

Want your text to have a drop shadow? No problem. How about boxes with rounded corners using a single line of code? Also easy to do. But this is still all fantasy. The problem holding us back is the same one web developers have had since the birth of the web — inconsistent browser support means your pixel perfect designs won’t work in every web browser.

Of course, many of the styles in CSS3 degrade gracefully — that is, browsers that don’t support rounded corners will simply render the box as a rectangle. In some cases that’s fine. Firefox, Safari, Opera and Chrome users will see one design and Internet Explorer users will see another.

However, there are some cases where that’s not good enough — for example in detecting HTML 5 support like the new <:canvas> capabilities — and that’s where the wonderful new tool Modernizr comes in handy.

Modernizr is a compact (6kb compressed) JavaScript library that can detect which CSS 3 properties the current browser supports.

Modernizr is a very slick and lightweight way to start using some of the very cool emerging standards of HTML 5 and CSS 3, while maintaining support for older browsers.

If the browser in question supports the feature you’re targeting, then Modernizr adds a class to the body tag so that you can specifically target just the current browser. For example you could have two CSS rules in your stylesheet like this:


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

In this case, Modernizer would check to see if the current browser supports multiple backgrounds (another nice new CSS 3 feature) and then apply the class multiplebgs so you can write specific rules for that browser.

Currently, Modernizer can detect the following elements:

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

However, Faruk Ate��, the creator of Modernizr, cautions that Canvas and CSS columns support is still experimental and may not work correctly in every browser.

In addition to the CSS 3 elements, Modernizr will allow you to use elements from the HTML 5 spec and style them, even in Internet Explorer.

For more details on how to use Modernizr on your site, check out the detailed tutorial.

One day HTML 5 and CSS 3 will be taken for granted and work in every browser, but until then solutions like Modernizer offer a way for developers to start working with new features today.

See Also:

Post Comment Comments Permalink Print
Reddit Digg

 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year