cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <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>

            
          
!
            
               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;
   }
}

            
          
!
            
              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
🕑 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.
Loading ..................

Console