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

              
                <div>
  <h1>Fetching an Elements Declared Styles Demo</h1>
</div>
<hr/>
<div id="container">
  <p id="lorem" style="line-height: 20px;">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent mi velit, pulvinar in pellentesque vel, commodo vel tortor. Maecenas quis risus fringilla, sodales eros at, fringilla lorem. Vivamus ultricies turpis non est varius, vel commodo purus ultrices.
    Aliquam suscipit neque libero, sed vehicula ex aliquam sed. Aliquam sollicitudin risus ut leo venenatis molestie. Phasellus rhoncus pellentesque tellus vitae consectetur. Suspendisse a maximus ex, sit amet tincidunt erat. Phasellus fermentum porttitor
    felis nec hendrerit. Aenean a purus vel diam malesuada tempor sed ut lorem. Duis congue sagittis rhoncus. Aliquam tempor condimentum malesuada. Curabitur euismod dolor lacus, eget aliquam purus auctor sodales.
  </p>
</div>
<hr/>
<hr/>
<div>
  <button id="showStyles" type="button">Show me styles on `p`</button>
  <br/>
  <code id="code">
    </code>
</div>

              
            
!

CSS

              
                 p {
   color: #cccccc;
 }
 
 #lorem {
   font-weight: bold;
 }
 
 @media (min-width: 300px),
 (min-device-width: 350px) {
   p {
     margin: 0;
   }
   #lorem {
     border: 1px solid #000000;
   }
   h1 {
     color: #000000;
   }
 }

@media (max-width: 600px) {
  #lorem {
     border: 1px solid #000000;
   }
}

              
            
!

JS

              
                document.getElementById('showStyles').onclick = function() {
  var $lorem =  document.getElementById('lorem');
  var $code = document.getElementById('code');
  var rules = MEJSX.getCustomCssRulesOnElement($lorem);
  console.log(rules);

  for (var i = 0; i < rules.length; i++) {
    $code.innerText += '\n\n//Order: ' + rules[i].order 
                     + ' | Media:'     + rules[i].media + '\n' 
                                       + rules[i].content;
  }
}

var MEJSX = function() {
  var getCustomCssRulesOnElement = function(elm) {
    var elementRules = [];
    var slice = Function.call.bind(Array.prototype.slice);

    var isCssMediaRule = function(cssRule) {
      return cssRule.type === cssRule.MEDIA_RULE; //cssRule.getName() === 'CSSMediaRule';
    }

    var mediaRuleMatchesDocument = function(cssRule) {
      var match = window.matchMedia( cssRule.media.mediaText ).matches;
      if (!match) console.log(cssRule.media.mediaText + ' does not apply to doc.');
      return match;
    }
    
    var isCssStyleRule = function(cssRule) {
      return cssRule.type === cssRule.STYLE_RULE; //cssRule.getName() === 'CSSStyleRule';
    }

    // Here we get the cssRules across all the stylesheets in one array
    var cssRules = slice(document.styleSheets).reduce(function(rules, styleSheet) {
      return rules.concat(slice(styleSheet.cssRules));
    }, []);

    var mediaRules = cssRules.filter(isCssMediaRule)
                             .filter(mediaRuleMatchesDocument);

    cssRules = cssRules.filter(isCssStyleRule);

    cssRules = cssRules.concat(slice(mediaRules).reduce(function(rules, mediaRule) {
      return rules.concat(slice(mediaRule.cssRules));
    }, []));

    console.log(cssRules);

    // get only the css rules that matches that element
    var rulesOnElement = cssRules.filter(isElementMatchWithCssRule.bind(null, elm));
    var elementRule = function(order, content, media) {
      if (media === undefined || media == null || media == '') {
        media = 'all';
      }
      this.order = order;
      this.content = content;
      this.media = media;
    }
    if (rulesOnElement.length) {
      for (var i = 0; i < rulesOnElement.length; i++) {
        var e = rulesOnElement[i];
        var order = i;
        var content = e.cssText;
        var media = e.parentRule == null 
                    ? e.parentStyleSheet == null 
                      ? 'all' 
                      : e.parentStyleSheet.media.mediaText 
                    : e.parentRule.media.mediaText;

        var _elementRule = new elementRule(order, content, media);
        elementRules.push(_elementRule);
      }
    }

    if (elm.getAttribute('style')) {
      var _elementRule = new elementRule(rulesOnElement.length, 'style {' + elm.getAttribute('style') + '}')
      elementRules.push(_elementRule);
    }
    return elementRules;
  };

  var isElementMatchWithCssRule = function(element, cssRule) {
    var proto = Element.prototype;
    var matches = Function.call.bind(proto.matchesSelector ||
      proto.mozMatchesSelector || proto.webkitMatchesSelector ||
      proto.msMatchesSelector || proto.oMatchesSelector);
    return matches(element, cssRule.selectorText);
  };

  return {
    getCustomCssRulesOnElement: function(element) {
      return getCustomCssRulesOnElement(element);
    }
  }

}()

              
            
!
999px

Console