Skip to content

Instantly share code, notes, and snippets.

@bencallahan
Created May 2, 2012 20:42
Show Gist options
  • Select an option

  • Save bencallahan/2580313 to your computer and use it in GitHub Desktop.

Select an option

Save bencallahan/2580313 to your computer and use it in GitHub Desktop.
Responsive Retrofitting: CSS Loading: Option 3
<!--
This is a series of gists comparing options for how to load CSS for a large
responsive retrofit. The requirements for this are as follows:
1. limit to one http request for CSS
2. keep the size of this CSS request as minimal as possible
3. serve only the original CSS to large resolutions (>= 980)
4. serve only the new "responsive" CSS to smaller resolutions (< 980)
5. serve only the original CSS to browsers that don't support media queries
Because this is a retrofit, we're not touching the original CSS. So, smaller
screen devices will have unique CSS. We can't touch the original CSS files,
so we need to do this in a way that keeps these mutually exclusive.
-->
/* this is a SCSS file which aggregates the necessary components for the
medium layout ONLY if JavaScript is enabled */
.js {
@import "reset";
@import "small";
@import "medium";
}
/* this is a SCSS file which aggregates the necessary components for the
original "desktop" design ONLY if JavaScript is enabled */
.js {
@import "reset";
@import "grid";
@import "main";
}
/* this is a SCSS file which aggregates the necessary components for the
small layout ONLY if JavaScript is enabled */
.js {
@import "reset";
@import "small";
}
/* this is a SCSS file which aggregates the necessary components for the
small layout */
@import "reset";
@import "small";
<html class="no-js">
<head>
<link media="min-width: 980px" rel="stylesheet" type="text/css" href="css/base-original-js.css">
<link media="max-width: 500px" rel="stylesheet" type="text/css" href="css/base-small-js.css">
<link media="(min-width: 501px) and (max-width: 979px)" rel="stylesheet" type="text/css" href="css/base-medium-js.css">
<noscript>
<link rel="stylesheet" type="text/css" href="css/base-small.css">
</noscript>
<script>
/* pseudo-code */
if ((no media query support) and
(document viewport width <= 500px)) { load "css/base-small-js.css" }
if ((no media query support and)
(document viewport width >= 501px) and
(document viewport width <= 979px)) { load "css/base-medium-js.css" }
if ((no media query support) and
(document viewport width >= 980px)) { load "css/base-original-js.css" }
</script>
</head>
</html>
@bencallahan

Copy link
Copy Markdown
Author

Requirements:

  • mostly passes 1 (if browser supports Media Queries but has JS disabled, 2 requests are made)
  • passes 2
  • mostly passes 3 (if browser supports MQ but has JS disabled, base-small.css is served after appropriate MQ)
  • passes 4
  • mostly pases 5 (if JS is disabled, only "small" styles are served)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment