The Modernizr Library
If you have used HTML5 at all, you have undoubtedly heard of the Modernizr JavaScript library (http://modernizr.com). The Modernizr library allows you to test, on a feature-by-feature basis, what is supported by a visitor’s browser. You can use the dynamically generated classes for styling hooks in CSS, you can query a global Modernizr object that it creates, or you can do both, and then you can customize your pages to fall back when certain features are not supported.
Modernizr also includes a key feature called html5shiv, which solves a problem that exists in Internet Explorer version 8 and earlier. In Internet Explorer 8 and earlier, if you try to style an element that Internet Explorer does not recognize—for example, some of the new HTML5 elements, such as section or article—the element will not be styled. This problem prevented many designers and developers from using the new HTML5 semantic elements. In response, the web development community quickly came up with a solution. In January 2009, Remy Sharp created the HTML5 enabling script, which uses a technique first discovered by Sjoerd Visscher to get around the problem with Internet Explorer by simply creating the unknown elements programmatically via JavaScript. Modernizr leverages this same technique in its html5shiv to ensure that older browsers that don’t recognize the new elements are still able to apply styles to them.
But the real power of Modernizr is its ability to check for the presence of specific features. When you download Modernizr, you must specify which features you want to check for, as you can see in Figure 1.3.
Figure 1.3. Customizing a Modernizr build
As you read this book, you need a copy of Modernizr that can check for the following features:
- CSS animations (also known as keyframe animations)
- CSS 2D transforms
- CSS 3D transforms
- CSS transitions
- html5shiv
The sample code for this book includes a production copy of Modernizr that is configured to check for all of these features. Instead of using the copy of Modernizr that comes with this book, you could customize your own build of Modernizr, selecting the features you plan on using in your website. Or, you could download the development version of Modernizr, which includes checks for all features and also consists of non-minified JavaScript code, so you can learn from the library and how it works. But for live websites, you will always want to use a production version that only checks for the features you will be using.
Leveraging the Modernizr Library
When you use a customized build for Modernizr, you need to reference it from the head element of your HTML page because the html5shiv enables HTML5 elements in Internet Explorer and so must execute before the body. HTML5 documentation also suggests that the Modernizr script be placed after the style sheet references. In Listing 1.1, we include the link to Modernizr in the head section, as well as a link to the two stylesheets we’ll be using, base.css and error.css (which will be defined later, in Listing 1.3).
Listing 1.1. Preparing a Page for Modernizr to Run (warning.html)
<html class="no-js" lang="en"> <head> <link rel="stylesheet" href="css/base.css"> link rel="stylesheet" href="css/error.css"> <script src="js/modernizr.custom.57498.js"></script> </head>
Modernizr is particularly useful because it gives you a choice of how to use it. You can use it by writing conditional CSS or by querying the global Modernizr object that the library creates. This chapter focuses on using Modernizr to create styling hooks via CSS.
CSS Fallbacks via Modernizr
The first thing that Modernizr does when it runs is loop through all the features you’ve asked it to check for and make note of which ones are supported and which ones are unsupported by adding entries to the class attribute of the opening html element.
But before Modernizr can do its job, you need to set up one last thing with your page. You need to add the class no-js to the html element:
<html class="no-js">
When Modernizr runs, if your browser has JavaScript enabled, it will replace that class with the class "js":
<html class="js">
This gives you a styling hook that reflects whether JavaScript is enabled. Modernizr then adds classes for every feature it detects, prefixing them with no- if the browser doesn’t support it. For the custom build of Modernizr you use in this book, the latest version of Chrome generates the following:
<html class="js cssanimations csstransforms csstransforms3d csstransitions" lang="en">
The Default Message for Unsupported Browsers
For each animation you create in this book, you will leverage Modernizr to perform a check to see if the browser supports the features you need to run the animation. In the browsers that don’t have the proper support, you could provide, if you like, a default message that informs the visitor of the problem and provide links to download one of the new browsers with the appropriate support.
This message will consist of a simple div element, some text and links, and the most basic of styling. By default, this div will be styled to display: none. But if any of the required features you rely on for your animations are not present, a selector will follow the default styling to update this div to display and to add additional styles to it.
Listing 1.2 outlines the code for this fallback message, which is contained in the file warning.html. In addition to the fallback message, which includes an unordered list with links to the modern browsers, the listing includes a small div called #dismiss that can be used to close this message. Toward the end of Listing 1.2 you can find the JavaScript that hooks the #dismiss element up to a click event and then applies the style hide to the #unsupportedBrowser element upon click. You can find the complete warning.html file in the ch1code/ folder of this book’s GitHub project, available at: https://github.com/alexisgo/LearningCSSAnimations.
Listing 1.2. HTML and JavaScript for a Warning Message (warning.html)
<body> <div id="unsupportedBrowser"> <div id="dismiss"> <span>x</span> </div> <div id="warningText"> Oh no! Your browser doesn't support CSS3 animations! Please download one of the following modern browsers in order to see what you're missing! <ul> <li><a href="http://www.mozilla.org/en-US/firefox/new/">Firefox</a></li> <li><a href="http://www.google.com/chrome">Chrome</a></li> <li><a href="http://www.apple.com/safari/download/">Safari</a></li> <li><a href="http://www.opera.com/">Opera</a></li> <li><a href="http://ie.microsoft.com/testdrive/">IE 10</a></li> </ul> </div> </div> <h1>YOYOYOYOYOYO</h1> <script type="text/javascript"> window.onload = setup; function setup() { var dismiss = document.getElementById("dismiss"); dismiss.onclick = hide; } function hide() { document.getElementById("unsupportedBrowser").className = "hide"; } </script> </body>
Listing 1.3 first styles the #unsupportedBrowser div to not display by default. Next, the code applies some basic styles to the unordered list. The fun kicks in after that, when you leverage Modernizr’s additions to the opening html tag in order to define a grouped selector. This grouped selector will be applied if even one of the key features is missing.
The grouped selector used will vary slightly based on each specific example used in the book, as not all examples use all the features checked for in Listing 1.1. Typically, the examples reviewed in subsequent chapters check for a subset of the list in Listing 1.3.
After the large grouped selector, let’s add some additional styles for the links, as well as the #dismiss div. Figure 1.4 shows what the warning looks like.
Figure 1.4. A default warning message that appears if the required properties for the CSS3 animation examples are not present.
Listing 1.3. Custom Style for an “Unsupported Browser” Message Added to H5BP Base CSS File (error.css)
#unsupportedBrowser, #unsupportedBrowser.hide { display:none; } #unsupportedBrowser ul li { display: inline; list-style-type: none; padding: 0 10px; } #unsupportedBrowser ul { margin:5px; } .no-keyframes #unsupportedBrowser, .no-cssanimations #unsupportedBrowser, .no-csstransforms #unsupportedBrowser, .no-csstransitions #unsupportedBrowser { display: block; text-align: center; width: 100%; font-size: 0.8em; padding: 10px; background-color: rgb(15,70,131); color: white; -webkit-box-shadow: 0px 2px 2px rgba(0,0,0,0.2); -moz-box-shadow: 0px 2px 2px rgba(0,0,0,0.2); box-shadow: 0px 2px 2px rgba(0,0,0,0.2); } #unsupportedBrowser a { color:white; font-weight:bold; } #unsupportedBrowser #dismiss { background-color: rgb(242,242,242); width: 25px; height: 25px; color: red; -webkit-border-radius: 13px; -moz-border-radius: 13px; border-radius: 13px; -webkit-box-shadow: 1px 1px 2px rgba(0,0,0,0.2); -moz-box-shadow: 1px 1px 2px rgba(0,0,0,0.2); box-shadow: 1px 1px 2px rgba(0,0,0,0.2); position:absolute; right: 3%; top: 2%; margin-left: 100px; font-size: 1.2em; } #warningText { margin:0 10%; }
While there are certainly more graceful and labor-intensive means to falling back when a visitor is using an older browser—namely, rewriting your animation with a library jQuery—the aim of this book is to focus on what you can do with the new technologies, not how to use old ones. Thus, rather than waste space reviewing old techniques, this book focuses on the new properties and how to use them most effectively.
One approach you might take when dealing with CSS3 animations is to fall back to a plain static object that doesn’t animate. That way, the animation is a bonus for modern browsers. In a real world project, it’s often best to allow the animation to degrade to a static object or group of objects.
Repeated CSS Property Definitions
There are times in this book when you will make use of another new CSS3 property, rgba, which allows you to add an alpha value to colors in order to make them semi-transparent. While Modernizr does support checking for this property, you can use a simple method to accommodate noncompliant browsers: Use repeated CSS property definitions.
Anytime you use the new rgba property, you preface that line with a simple background-color property. That way, if the browser does not support rgba, it simply ignores that line and applies the background-color property from the line before. For example, see the code in Listing 1.4.
Listing 1.4. Repeated CSS to Allow for Older Browsers
#someSelector { background-color: black; background-color: rgba(0,0,0,0.4); }