38

I'm looking to remove duplicate CSS declarations from a number of files to make implementing changes easier. Is there a tool that can help me do that?

Right now I'm faced with something like this:

styles.css
#content {
width:800px;
height:1000px;
background: green;
}

styles.game.css
#content {
width:800px;
height:1000px;
background: blue;
}

And I want this:

styles.css
#content {
width:800px;
height:1000px;
background: green;
}

styles.game.css
#content {
background: blue;
}

The total number of lines across all files is well over 10k, so techniques that rely on manual editing aren't an option.

Suresh Karia
  • 17,550
  • 18
  • 67
  • 85
kotekzot
  • 1,518
  • 1
  • 14
  • 23

6 Answers6

13

I wrote a tool specifically for this purpose called csscss. As a bonus it also integrates with Sass and LESS. Give it a shot and let me know if you have an issues in github.

Zach Moazeni
  • 375
  • 2
  • 5
9

helped me to clean up selectors - CSS usage - Firebug extension to view which CSS rules are actually used.

https://addons.mozilla.org/en-US/firefox/addon/css-usage/

http://web.archive.org/web/20120127123107

Jan
  • 2,178
  • 3
  • 14
  • 26
Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28
7

I made a nodejs tool to help with this, it currently handles single files but lemme know if it helps or of any improvements, feel free to fork it and take it to another level too :)

https://npmjs.org/package/css-purge

https://github.com/rbtech/css-purge

AEQ
  • 1,339
  • 1
  • 16
  • 20
3

This system claim to do that: http://sourceforge.net/projects/cssmerge/?source=dlp

But I couldn't make it work, though.

So here it goes some tools to compare the CSS files. It is not as fast as an automatic solution, but would make it faster than going by visual comparison alone.

http://www.diffchecker.com/

http://www.araxis.com/merge_mac/index.html

(csscompare.codeplex.com) -> https://github.com/bertjohnson/CSSCompare

Jan
  • 2,178
  • 3
  • 14
  • 26
Fabio Nolasco
  • 7,122
  • 6
  • 35
  • 32
2

You can use W3C CSS validator to remove the duplicates of the properties. Upload the css file by clicking By file upload and click on check, then go to warnings part where you can see the duplicate properties repeated. Then you can remove your duplications by going to specific line in the file.

URL: jigsaw.w3.org/css-validator

Jan
  • 2,178
  • 3
  • 14
  • 26
harshakasireddy
  • 322
  • 1
  • 2
  • 13
  • Doesn't seem to work. I get a few warnings about background and text colors being the same, but no warning about duplicate declarations. – kotekzot Mar 30 '12 at 06:47
0

Probably the closest thing to what you're looking for is a css preprocessor and css imports. I like LESS: http://lesscss.org/

I would do something like

styles.css
@site-width: 800px;
@site-height: 1000px;

#content {
width: @site-width;
height: @site-height;
background: green;
}


styles.game.css
@import url("style.css");

#content {
width: @site-width;
height: @site-height;
background: blue;
}

EDIT: I sort of overlooked that you don't even need LESS at all, or the height and width in styles.game.css

It would just look like

styles.game.css
@import url("style.css");

#content {
background: blue;
}
Connor
  • 1,024
  • 4
  • 16
  • 24
  • An interesting solution, but in the end I would still have to deal with duplicate declarations when editing CSS, and the total number of lines in those files is well over 10k, editing them by hand is not feasible. – kotekzot Mar 11 '12 at 16:18
  • RE: edit, is there a way to arrive at that state without editing all the files by hand? – kotekzot Mar 11 '12 at 16:22
  • Not that I know of... from what you've said, it sounds like a lot of Find & Replace – Connor Mar 11 '12 at 16:26