May 7, 2012

Merging CSS files

Question by newbie

I have to merge CSS files to one big CSS file to reduce connections made during page load. Basically if I just append CSS files to one big file, are these styles going to work exactly like before, or are there any issues when you merge multiple CSS files together? I’m developing my software in Java, if there is some library that already does CSS merging, I would like to hear about that.

Answer by Mike Samuel

If your CSS files use @import, then merging them into one file might change the imported URLs.

When an @import has a relative URL it is resolved against the URL of the containing CSS, so you could end up inadvertently breaking @imports.

http://www.w3.org/TR/css3-values/

In order to create modular style sheets that are not dependent on the absolute location of a resource, authors should use relative URIs. Relative URIs (as defined in [URI]) are resolved to full URIs using a base URI. RFC 3986, section 3, defines the normative algorithm for this process. For CSS style sheets, the base URI is that of the style sheet, not that of the source document.

Answer by Starx

Use ANT build script from HTML Boilerplate, does the task you are attempting perfectly.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!