We all like creating new stuffs and updating our site theme to meet up
with the latest trends. We added new features, let's say a widget and
after some times, we believe it is outdated and decide to trash it out.
Most times we remove the widget forgetting to dig-out the styles from
the stylesheets. The styles becomes an unused or dead CSS selectors.
It
is important to remove the unused CSS selectors to speed-up browser
rendering of your website. And even if the effect of this unused
selectors on your site performance is minimal, it would make your task
of maintaining CSS easier if the files are kept clean and
well-structured.
Google also recommends the removal of unused CSS:
"Before
a browser can begin to render a web page, it must download and parse
any stylesheets that are required to lay out the page. Even if a
stylesheet is in an external file that is cached, rendering is blocked
until the browser loads the stylesheet from disk. In addition, once the
stylesheet is loaded, the browser's CSS engine has to evaluate every
rule contained in the file to see if the rule applies to the current
page. Often, many web sites reuse the same external CSS file for all of
their pages, even if many of the rules defined in it don't apply to the
current page.
The best way to minimize the latency caused by stylesheet loading and rendering time is to cut down on the CSS footprint; an obvious way to do this is to remove or defer CSS rules that aren't actually used by the current page."
The best way to minimize the latency caused by stylesheet loading and rendering time is to cut down on the CSS footprint; an obvious way to do this is to remove or defer CSS rules that aren't actually used by the current page."
While there are several tools that lets you locate unused CSS selectors within your site pages, among the best are:
Google Chrome Audit Tool
Google browser has an awesome but overlooked feature built in its Developer Tools. It's called the "Audit tool". To make use of this excellent feature, follow the below steps:
- Open any page of your website inside Google Chrome and then launch the Dev Tools available under File > Tools > Developers Tools (or press SHIFT + CTRL + I).
- Click on the Audits tab within the Developer Tools window.
- Make sure the "Web Page Performance" and "Reload Page and Audit on Load" options is checked.
- Now click the "Run" button
The CSS audit tool will start digging out unused CSS on the specified page. You still need to do the gruntwork of locating them in your stylesheet and removing those rules.
If you're going to do this, make sure you run it against lots (if not all) of your pages so you aren't removing selectors that are in use, just not on every page. You can find the count of various selectors in the combined result set, maybe using Google Sheets, and the selectors with the highest count can probably be "safely" removed. This rule also applies to any other tool you might use.
Firefox: Dust-Me selectors extension
For Firefox fan, there is something for you too. Sitepoint released a Firefox extension called Dust-Me selectors, which is a bit advanced than Chrome audit tool and it happens to be my favourite. Dust-Me extracts all of the selectors from all of the stylesheets on the page you’re viewing. It then analyzes that page to see which of those selectors are not used. The data is then stored so that when testing subsequent pages, selectors can be crossed off the list as they’re encountered. Dust-Me also gives you the option of testing pages individually or spider an entire site, and you'll end up with a profile of which selectors are not used anywhere.
UnCSS
Giacomo Martino’s excellent UnCSS tool gives us the ability of removing unused CSS to help keep our site's extra lean. It works across multiple files and supports JavaScript-injected CSS. It can also work in conjunction with other javascript build systems, such as Grunt or Gulp uncss.
The process by which UnCSS removes the unused rules is as follows:
- The HTML files are loaded by PhantomJS and JavaScript is executed.
- Used stylesheets are extracted from the resulting HTML.
- The stylesheets are concatenated and the rules are parsed by css-parse.
- document.querySelector filters out selectors that are not found in the HTML files.
- The remaining rules are converted back to CSS.
Like just about every NodeJS-based utility, you can also take advantage of its JavaScript API. Here's an example:
var uncss = require( 'uncss' );
var files = [ 'my' , 'array' , 'of' , 'HTML' , 'files' ],
options = {
ignore : [ '#added_at_runtime' , /test\-[0-9]+/ ],
media : [ '(min-width: 700px) handheld and (orientation: landscape)' ],
csspath : '../public/css/' ,
raw : 'h1 { color: green }' ,
stylesheets : [ 'lib/bootstrap/dist/css/bootstrap.css' , 'src/public/css/main.css' ],
ignoreSheets : [ /fonts.googleapis/ ],
urls : [ 'http://localhost:3000/mypage' , '...' ], // Deprecated
timeout : 1000 ,
htmlroot : 'public'
};
uncss(files, options, function (error, output) {
console.log(output);
});
/* Look Ma, no options! */
uncss(files, function (error, output) {
console.log(output);
});
/* Specifying raw HTML */
var raw_html = '...' ;
uncss(raw_html, options, function (error, output) {
console.log(output);
});
var files = [ 'my' , 'array' , 'of' , 'HTML' , 'files' ],
options = {
ignore : [ '#added_at_runtime' , /test\-[0-9]+/ ],
media : [ '(min-width: 700px) handheld and (orientation: landscape)' ],
csspath : '../public/css/' ,
raw : 'h1 { color: green }' ,
stylesheets : [ 'lib/bootstrap/dist/css/bootstrap.css' , 'src/public/css/main.css' ],
ignoreSheets : [ /fonts.googleapis/ ],
urls : [ 'http://localhost:3000/mypage' , '...' ], // Deprecated
timeout : 1000 ,
htmlroot : 'public'
};
uncss(files, options, function (error, output) {
console.log(output);
});
/* Look Ma, no options! */
uncss(files, function (error, output) {
console.log(output);
});
/* Specifying raw HTML */
var raw_html = '...' ;
uncss(raw_html, options, function (error, output) {
console.log(output);
});
Mincss
mincss is a python script. As its creator Peter Bengtsson explains, mincss is a tool that when given a URL (or multiple URLs) downloads that page and all its CSS and compares each and every selector in the CSS to find out what isn’t used. The result is your original CSS file with those unused selectors (not found in your HTML) removed.
The way it works is that it parses the CSS payload (from inline blocks or link tags) with a relatively advanced regular expression and then loops over each selector one at a time and runs it with cssselect (which uses lxml) to see if the selector is used anywhere. If the selector isn't used the selector is removed.
Other projects
Some other developers have been creating awesome tools that helps in detecting unused CSS within any page, and worth mentioning:
Conclusion
I think this are great tools for all of us who either just downloaded a bloated CSS framework or working with a heavy modern UI framework such as Twitter Bootstrap, Zurb Foundation or Adobe TopCoat.
Every single tool mentioned in this post worth giving a try especially the Audit tool and UnCSS. And when testing, don't forget to run this across multiple pages in order to be on the safe side.
Subscribe For Free Updates!
*Please confirm the email sent to your inbox after clicking "Sign Up!".
http://www.aktwall.com/post/1336/remove-unused-css-to-reduce-your-stylesheets-size
ReplyDeleteThanks for publish a great article!
ReplyDeletehttp://www.gossip-lankanews.com
coolsquare this site about web development and we graphics
ReplyDeleteHappy Chocolate Day 2016
ReplyDeleteValetine Week List Schedule
Merry Christmas 2015 images pictures photos wallpapersxmas coloring pages free
thanks for sharing css removing tips here
ReplyDeleteValentine day 2016 Quotes SMS
Valentines day facebook messages
Happy Valentines day messages
Valentines day wallpapers Images
Happy Valentines day 2016 SMS
Valentine day Shayari 2016
Valentine day Special 2016
Valentine day Greeting cards
Valentine day 1080p Images
Valentine day 2016 Quotes SMS
Valentines day 2016 wallpapers
Happy Rose day 2016 wallpapers
thanks again
Exam Results
ReplyDeleteExam Results 2016
Exams 2016
www.maharesult.nic.in
ReplyDeleteCheck Class 10th Result
Online CBSE Class 10 Result
CBSE 10th Result
CBSE Class 12 Result
Online CBSE 10th Result
AIPMT Result 2016
AIPMT 2016 Result
AIPMT 2016 Admit Card
www.mahresult.nic.in
Maharashra SSC Result 2016
SSC Result 2016
Mah SSC Result 2016
Maharashtra HSC Result 2016
Mah HSC Result 2016
HSC Result 2016
CBSE Result 2016
Fan Box Office Collection
Box Office Collection of Fan
Quotes for mothers Day
Best Mothers Day Quotes
Poems for Mothers Day
Mothers Day Poems From Daughter
Happy Fathers Day Poems
Fathers Day Quotes From Daughter
father daughter quotes
father and daughter quotes
Mother’s Day Status In Hindi
ReplyDeleteMother’s Day Status In Marathi
Mother’s Day Status In English
Earth Day Activities
ReplyDeleteEarth Day 2016 Activities
Happy Earth Day Activities
Raees Box Office Collection
ReplyDeleteFirst Day Raees Box Office Collection
Raees First Day Box Office Collection
Raees Total Worldwide Box Office Collection
Raees Total Box Office Collection
Raees Movie Review
Raees First Day Earnings
Fathers Day Face book Pictures
ReplyDeleteFathers Day Cards
Sultan Total Income
Raees Total Income
Slogans of Independence Day in English
AIPMT Result
Raksha Bandhan Quotes
Happy Eid-al-fitr Quotes
Bigg Boss Season 10 Final Contestants
friendship day Quotes in English
Bigg Boss 10 Winner leaked
Bigg Boss 10 Starting Date
ReplyDeleteBigg Boss 10 Contestants
Bigg Boss Season 10 Winner announcement
Bigg Boss Season 10 Contestants Name
how to check lic policy status
www.msedcl.net.in
VMware is a cloud computing software and virtualization provider for x86-compatible computers. It is mainly based on the ESX bare metal hypervisor, supporting virtual machines.Keep sharing more about VMware.
ReplyDeleteRegards,
VMware Training in Chennai | VMware course in Chennai
Dangal movie release date
ReplyDeletela liga live streaming
ReplyDeletemerry christmas 2016 song
Happy New year 2017 Messages
Raees Box Office Prediction
ReplyDeleteRaees Hit or Flop
Dangal overseas box office collection
ReplyDeleteDangal domestic box office collection
Dangal movie Kamai
Dangal first weekend box office collection
Dangal movie collection
Dangal first day collection
Dangal second day collection
Dangal third day collection
Total dangal movie collection
michael kors outlet clearance
ReplyDeletepolo ralph lauren
polo ralph lauren outlet online
lacoste polo shirts
michael kors outlet store
pandora outlet
cheap mbt shoes
michael kors outlet
mbt shoes
coach outlet online
huangxiang20170509
ReplyDeleteThanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
please sharing like this information......
Android training in chennai
Ios training in chennai
ReplyDeleteIt's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
Web Design Company
Web Development Company
Mobile App Development Company
nfl jerseys
ReplyDeletechristian louboutin sale
true religion outlet
michael kors outlet
mont blanc pens
coach outlet store online
canada goose
canada goose jackets
nike shoes for men
cheap uggs
20170528wangili
It’s a great and useful post. Keep sharing such kind of noteworthy information.
ReplyDeleteVmware cloud certification | VMware courses in Chennai | Vmware cloud Training
Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Tours and Travels in Madurai | Best Travels in Madurai | Madurai Travels
ReplyDeleteWow great post thanks for sharing
ReplyDeleteSoftware Testing training in Chennai | Software Testing in Chennai
Highly informative article. I was searching for such quality articles for a long time. I am happy that I found this article. Keep going!
ReplyDeleteData Science Course in Chennai | Data Science Training in Chennai
Wow it is really wonderful and awesome thus it is veWow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your site.ry much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your site.vmware training in bangalore
ReplyDeletehermes birkin
ReplyDeleteconverse outlet
cheap jordans
adidas nmd r1
yeezy boost 350 v2
moncler
off white hoodie
longchamp handbags
michael jordan shoes
moncler coat