Adapt.js - Adaptive CSS

What is this?

Adapt.js is a lightweight (848 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.

Default CSS Files & Widths
File Name Screen Width
mobile.css below 760px
720.css 760px to 980px
960.css 980px to 1280px
1200.css 1280px to 1600px
1560.css 1600 to 1940px
1920.css 1940px to 2540px
2520.css above 2540px

Download   |   Blog post   |   GitHub repo


// Edit to suit your needs.
var ADAPT_CONFIG = {
  // Where is your CSS?
  path: 'assets/css/',

  // false = Only run once, when page first loads.
  // true = Change on window resize and page tilt.
  dynamic: true,

  // Optional callback... myCallback(i, width)
  callback: myCallback,

  // First range entry is the minimum.
  // Last range entry is the maximum.
  // Separate ranges by "to" keyword.
  range: [
    '0px    to 760px  = mobile.css',
    '760px  to 980px  = 720.css',
    '980px  to 1280px = 960.css',
    '1280px to 1600px = 1200.css',
    '1600px to 1920px = 1560.css',
    '1940px to 2540px = 1920.css',
    '2540px           = 2520.css'
  ]
};

Configuration

For all possible optionsRead more

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.

The code is available via GitHub.


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.

Background

This whole browser resizing craze was started by none other than the inestimable Ethan Marcotte, when he wrote his seminal article for A List Apart, entitled Responsive Web Design. Since then, some have criticized @media queries as fool’s gold. Also worth reading are One Web and Toffee-Nosed, further defining responsiveness.


Licensed under GPL and MIT.

Powered by Adapt.js + 960.gs.   |   Repository at GitHub.

Custom grids via SprySoft.