We've been hosted on Digital Ocean since 2013 and highly recommend it for your hosting too. Try it now!

CSS Minifier

Online CSS Minifier/Compressor. Free! Provides an API. Simple Quick and Fast.

Written and hosted by Andrew Chilton (@andychilton) since 2009.

No ads in .

C# Async Example

Thanks to Dennis Janiak for contributing this example.

public static class CssMinifier
{
    private const string URL_CSS_MINIFIER       = "http://localhost:80/raw";
    private const string POST_PAREMETER_NAME    = "input";

    public static async Task<String> MinifyCss(string inputCss)
    {
        List<KeyValuePair<String, String>> contentData = new List<KeyValuePair<String, String>>
        {
            new KeyValuePair<String, String>(POST_PAREMETER_NAME, inputCss)
        };

        using (HttpClient httpClient = new HttpClient())
        {
            using (FormUrlEncodedContent content = new FormUrlEncodedContent(contentData))
            {
                using (HttpResponseMessage response = await httpClient.PostAsync(URL_CSS_MINIFIER, content))
                {
                    response.EnsureSuccessStatusCode();
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
    }
}

Usage:

using (Task<String> task = CssMinifier.MinifyCss("/* test commentary */ input { font-size: 9pt ; }   "))
{
    task.Wait();
    Console.WriteLine(task.Result); // input{font-size:9pt}
}

C# Example

Thanks to Juan Herrera for contributing this example.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:80/raw");
request.Method = "POST";
string formContent = "input=" + cssContent.Text;
byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream str = request.GetRequestStream();
str.Write(byteArray, 0, byteArray.Length);
str.Close();

WebResponse response = request.GetResponse();
str = response.GetResponseStream();
if (str != null)
{
  StreamReader reader = new StreamReader(str);
  cssMinified.Text = reader.ReadToEnd();
  reader.Close();
  str.Close();
}
response.Close();

Many people have sent language examples over the years, but we want more! We'd love to add more examples in your favourite programming language, so please send them our way. Email andychilton at that gmail place in the sky, tweet me @andychilton, or open an issue or PR on webdev-sh/cssminfier.com.

Click on the language of your choice to see an example: