Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find all the global styles that apply to a web page?

Tags:

javascript

css

As a company, we use components (Angular, Vue and React) to build our applications, but we still have a good number of global styles that we inherited from our legacy app.

eg:

.active {   background: red; } 

Will apply to any element anywhere on the page that has a class of active.

Is there a way, in the browser, to generate a list of all the global (i.e. non-namespaced) style rules that apply to a page, bearing in mind that these might be present inside of third-party libraries, or other miscellaneous legacy JavaScripts?

like image 209
superluminary Avatar asked Nov 21 '17 11:11

superluminary


People also ask

How do I find the stylesheet of a website?

On Chrome's Developer Tools tab (CTRL + SHIFT + I), go to Resources (you may have to enable Resource tracking on that page), and click on the sub-tab Stylesheets. That will show all css files loaded by that page.

How do you know where an element style is coming from?

In Chrome, if you right click on an element and "inspect," then view the styles in the "Computed" tab then you should see what styles are affecting the element.

What are global styles?

What are Global Styles? Global styles is a system and an interface created to help users change the overall style across their entire site without having to edit individual blocks or pages. Users can select a body background color or change the line-height of all their heading blocks from one place.

What are global styles in CSS?

What are global styles? When people talk about the global nature of CSS, they can mean one of a few different things. They may be referring to rules on the :root or <body> elements that are inherited globally (with just a few exceptions).


1 Answers

The only option for evaluating the current page's CSS styles is to use document.styleSheets. It will return a list of CSSStyleSheets.

You will want to focus on document.styleSheets[n].cssRules, where n equals which stylesheet you want to evaluate. That will give you a list of all the styles applied by that stylesheet. Each stylesheet will have a cssText and a selectorText property.

If you just want to loop through to find which styles are 'non-namespaced' you should probably just use the selectorText properties.

Here is some more information on MDN about document.styleSheets.

Here is an example (press 'Run code snippet' to see results):

var selectors = [];    var sheets = Array.from(document.styleSheets);    sheets.forEach(function(sheet) {    // Only Stylesheets from a same-origin domain expose `cssRules` for security reasons    try {      var rules = Array.from(sheet.cssRules);      rules.forEach(function(rule) {        selectors.push(rule.selectorText);      });    } catch (e) {      // Do something with external stylesheets if you want    }  });    console.log(selectors);
<!DOCTYPE html>  <html>    <head>    <meta charset="utf-8">    <meta name="viewport" content="width=device-width">    <title>Stylesheets</title>    <style>      .hello-world {        background: url(none.gif);      }    </style>    <!-- Won't work as it is not a same-original stylesheet -->    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.2/normalize.min.css" rel="stylesheet" type="text/css" />  </head>    <body>    <style>      .foo {        background: url(none.gif)      }            .bar {        background: url(none.gif);      }    </style>  </body>    </html>
like image 199
pseudosavant Avatar answered Sep 19 '22 17:09

pseudosavant