explaingit

scottjehl/respond

11,237JavaScriptAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

A tiny JavaScript file that makes older Internet Explorer browsers (IE6, 8) understand CSS media queries, so responsive web designs don't break in those legacy browsers.

Mindmap

mindmap
  root((repo))
    What it does
      Polyfills media queries
      Targets IE 6 to 8
      Enables responsive design
    How it works
      Fetches CSS via XHR
      Scans for min max width
      Toggles style blocks
    Limitations
      Same domain only
      No nested queries
      No import or inline
    Setup
      Single script tag
      Under 3KB uncompressed
      MIT license
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Add responsive design support to a website that must work in Internet Explorer 8 and older.

USE CASE 2

Fix broken layouts on legacy IE browsers without rewriting existing CSS media queries.

USE CASE 3

Polyfill min-width and max-width media queries for a corporate intranet still using IE8.

Tech stack

JavaScript

Getting it running

Difficulty · easy Time to first run · 5min

Stylesheets must be served from the same domain as the page, CDN-hosted CSS requires a local proxy.

MIT license, use freely for any purpose, including commercial projects, as long as you keep the copyright notice.

In plain English

Respond.js is a small JavaScript file that adds support for CSS media queries to older versions of Internet Explorer (versions 6 through 8) that do not understand them natively. It was created in 2011 and targets a specific, now largely historical, browser compatibility problem. CSS media queries are the technique behind responsive web design: they let a stylesheet apply different visual rules at different screen widths, so a single HTML file can look appropriate on a phone, a tablet, or a desktop. Internet Explorer 8 and earlier do not recognize these rules at all, which means a responsive layout would appear broken in those browsers without a fix like this one. Respond.js works by fetching copies of the page's CSS files using an internal web request, scanning them with a regular expression to find media query blocks, and then enabling or disabling those style blocks as the window width changes. Because it re-fetches the CSS files, it requires that stylesheets be served from the same domain as the page. Hosting stylesheets on a different domain or a CDN requires setting up a local proxy. The script weighs about 3KB uncompressed and roughly 1KB after minification and compression. It handles only min-width and max-width queries, which are the most common building blocks of mobile-first responsive design. It does not support nested media queries, styles loaded via @import, or media queries inside inline style elements. There are also documented edge cases involving UTF-8 byte-order marks, @font-face rules placed inside media query blocks, and pages that reference more than 32 stylesheets in IE. The project notes that other media query polyfills with broader feature coverage exist, but positions Respond.js as the fastest option for the common min-width and max-width case. It is licensed under the MIT license.

Copy-paste prompts

Prompt 1
I have a responsive website that uses CSS media queries but looks broken in IE8. Show me how to add Respond.js to fix it, including where to put the script tag and any same-domain stylesheet requirements.
Prompt 2
My stylesheets are on a CDN and Respond.js isn't working in IE8. Help me set up a local proxy so the polyfill can fetch them.
Prompt 3
I need to support IE6 through IE8 for a client. Walk me through adding Respond.js and what media query features it does and doesn't support.
Prompt 4
What are the known edge cases with Respond.js, UTF-8 byte order marks, font-face rules, and the 32-stylesheet limit, and how do I work around them?
Open on GitHub → Explain another repo

← scottjehl on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.