Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>

<!-- 
Technique for maintaining the media queries in CSS only. Media query CSS execution is detected by JavaScript that reads the CSS generated content value.

  Credits: 
  http://adactio.com/journal/5414/
  http://adactio.com/journal/5429/
  https://gist.github.com/2481019
  https://gist.github.com/2482049
  https://css-tricks.com/examples/ConditionalCSS/

  Author: Aki Karkkainen (putting this demo together from the resources above)
  URL: https://gist.github.com/2944398
  Twitter: https://twitter.com/akikoo
-->

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8" />
    <title>Detecting from JavaScript whether media queries have been executed in CSS</title>

  <!-- Mobile viewport optimization https://goo.gl/b9SaQ -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
<body>

<h1>Conditional Content based on CSS Media Queries</h1>

<p>The media queries are maintained <strong>only</strong> in the CSS. Width check happens on resize event, you might want to remove that and trigger the script on page reload only.</p>

<p>
  <span class="mediaquery mediaquery-0">Width is 0&ndash;480px.</span> 
  <span class="mediaquery mediaquery-1">Width is 480&ndash;768px.</span> 
  <span class="mediaquery mediaquery-2">Width is more than 768px.</span>
</p>

</body>
</html>
              
            
!

CSS

              
                @import "compass/css3";

body:after { /* Mobile first, no media query here */
  content: "nolayout";
  display: none; 
}

/* Demo start */
.mediaquery {
  display: none;
}

.mediaquery-0 {
  display: inline;
  color: red;
}/* Demo end */


@media only screen and (min-width: 30em) { /* 480px */
  body:after {
    content: "mobile";
  }

  /* Demo start */
  .mediaquery {
    display: none;
  }
    
  .mediaquery-1 {
    display: inline;
    color: blue;
  }/* Demo end */
}


@media only screen and (min-width: 48em) { /* 768px */
  body:after {
    content: "widescreen";
  }
  
  /* Demo start */
  .mediaquery {
    display: none;
  }
    
  .mediaquery-2 {
    display: inline;
    color: green;
  }/* Demo end */
}
              
            
!

JS

              
                (function($, win, doc) {

  'use strict';

  $(window).resize(function() {

    var size = win.getComputedStyle(doc.body, ':after').getPropertyValue('content');

    // Some browsers have quotes, some don't in the returned value
    size = size.replace(/"/g, "");
    size = size.replace(/'/g, "");

    // If you want to open your console to see the current value
    // console.log('Current width is: ' + size);

    if (size === 'mobile') {
      // More code here, maybe loading content with Ajax
    }

    if (size === 'widescreen') {
      // More code here, maybe loading even more content?
    }

  }).resize();

}(jQuery, this, document));
              
            
!
999px

Console