Extracting only the css used in a specific page

前端 未结 15 1793
难免孤独
难免孤独 2020-12-04 06:51

Say you had a dynamically generated site that has been worked on by too many people, past and present, and you now have a collection of shared stylesheets that contain over

相关标签:
15条回答
  • 2020-12-04 07:28

    Hands down the best tool that actually does exactly what you want by only getting the used CSS on the page and allows you to simply copy it to your clipboard is this Chrome extension CSS Used

    Pretty Picture Example

    0 讨论(0)
  • 2020-12-04 07:30

    These look promising:

    • Unused-CSS.com -- Service that spiders your website and emails you the results
    • CSS Usage -- Firebug addon
    0 讨论(0)
  • 2020-12-04 07:32

    Here's my solution using JavaScript :

        var css = [];
        for (var i=0; i<document.styleSheets.length; i++)
        {
            var sheet = document.styleSheets[i];
            var rules = ('cssRules' in sheet)? sheet.cssRules : sheet.rules;
            if (rules)
            {
                css.push('\n/* Stylesheet : '+(sheet.href||'[inline styles]')+' */');
                for (var j=0; j<rules.length; j++)
                {
                    var rule = rules[j];
                    if ('cssText' in rule)
                        css.push(rule.cssText);
                    else
                        css.push(rule.selectorText+' {\n'+rule.style.cssText+'\n}\n');
                }
            }
        }
        var cssInline = css.join('\n')+'\n';
    

    In the end, cssInline is a textual list of all the steelsheets of the page and their content.

    Example :

        /* Stylesheet : http://example.com/cache/css/javascript.css */
        .javascript .de1, .javascript .de2 { -webkit-user-select: text; padding: 0px 5px; vertical-align: top; color: rgb(0, 0, 0); border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); margin: 0px 0px 0px -7px; position: relative; background: rgb(255, 255, 255); }
        .javascript { color: rgb(172, 172, 172); }
        .javascript .imp { font-weight: bold; color: red; }
    
        /* Stylesheet : http://example.com/i/main_master.css */
        html { }
        body { color: rgb(24, 24, 24); font-family: 'segoe ui', 'trebuchet MS', 'Lucida Sans Unicode', 'Lucida Sans', sans-serif; font-size: 1em; line-height: 1.5em; margin: 0px; padding: 0px; background: url(http://pastebin.com/i/bg.jpg); }
        a { color: rgb(204, 0, 51); text-decoration: none; }
        a:hover { color: rgb(153, 153, 153); text-decoration: none; }
        .icon24 { height: 24px; vertical-align: middle; width: 24px; margin: 0px 4px 0px 10px; }
        #header { border-radius: 0px 0px 6px 6px; color: rgb(255, 255, 255); background-color: rgb(2, 56, 89); }
        #super_frame { min-width: 1100px; width: 1200px; margin: 0px auto; }
        #monster_frame { -webkit-box-shadow: rgb(204, 204, 204) 0px 0px 10px 5px; box-shadow: rgb(204, 204, 204) 0px 0px 10px 5px; border-radius: 5px; border: 1px solid rgb(204, 204, 204); margin: 0px; background-color: rgb(255, 255, 255); }
        #header a { color: rgb(255, 255, 255); }
        #menu_2 { height: 290px; }
    
        /* Stylesheet : [inline styles] */
        .hidden { display: none; }
    
    0 讨论(0)
  • 2020-12-04 07:32

    Hmm.. I'd throw some brute force at this by extracting the various CSS selectors using a serverside parsing of the CSS file, then run each of these as a jQuery selector within the browser. Not very elegant, but should work?

    0 讨论(0)
  • 2020-12-04 07:33

    As of Sept 2020 this question is almost 10 years old. Most of the provided solutions no longer work or the linked projects have disappeared.

    However, the question is still extremely relevant as Google now uses page speed as one of its prioritization metrics.

    After doing a bunch of research into all the links listed, I found purgecss.com. This seems to be the best option to clean up unused CSS in modern web apps/SPAs using Angular, React, Vue, etc. There's also build integration with PostCSS, Webpack, Grunt, and Gulp.

    Also, UnCSS still seems to be maintained. It's as powerful as PurgeCSS but not as integrated into build processes or single-page javascript apps.

    0 讨论(0)
  • 2020-12-04 07:36

    I came across Uncss-Online - Unofficial server, very convenient for testing or one-off usage! I think its the best tool I've come across.

    UnCSS is a tool that removes unused CSS from your stylesheets. It works across multiple files and supports Javascript-injected CSS. It can be used in this way:

    • Copy & paste your HTML and CSS into provided boxes
    • Click button
    • Wait for magic to happen
    • Unused CSS is gone, take the rest and use it!

    You can check their Github Page for other advanced ways to use this tool

    0 讨论(0)
提交回复
热议问题