Adapt.js - Adaptive CSS

What is this?

Adapt.js is a lightweight (820 bytes minified) JavaScript file that determines which CSS file to load before the browser renders a page. If the browser tilts or resizes, Adapt.js simply checks its width, and serves only the CSS that is needed, when it is needed.

A potential drawback of Adapt.js is the possibility of a brief flash of unstyled content as a new stylesheet is being fetched (think of it as “Ajax” for CSS). I have done my best to mitigate this by keeping CSS files small (3 KB). It is worth noting this is a proposed, not prescribed, approach to a problem with multiple solutions.

Other methods include: Build a separate site for mobile. Or, use media queries to adjust layout, with a polyfill for older browser support, and conditional Internet Explorer comments for Windows phones. Also a factor is how to handle multiple image resolutions without adding file size. Filament Group is advocating context aware image sizing.

Configuration

Adapt.js, accepts a few parameters: path is where your stylesheets reside, dynamic is a boolean (true or false) that says whether to watch the window for its resize event, also triggered by tablet or phone tilt. Widths and optional CSS files are specified in range. The defaults are shown in the adjacent code example. You can also specify an optional callback function, that will pass range index and width.

Open Source

Just like the 960 Grid System itself, Adapt.js is licensed under GPL and MIT. That means it is free, as in speech. If you want to use it in a product that is already licensed under the GPL, you can. Or, if you want to use it in a commercial product, you can choose the MIT license instead. No strings attached.

Fallback

In the case of JavaScript being purposefully disabled or unavailable, stylesheet defaults can be served via <noscript>, which is perfectly valid in the <head> for HTML5. Taking a mobile first approach, I specified the mobile.css file, assuming devices without JS capabilities are likely to be less capable “feature” phones.

“It Depends”

As with any field in which technological methods are open for debate, there is the danger of religious fanaticism, where we each rally behind a respective technique and defend it vehemently. I would advise you to consider your audience, weigh the options, and find an approach that makes sense for that particular context.