Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!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>
            
          
!
            
              @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 */
}
            
          
!
            
              (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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console