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 .

Kotlin

Thanks to Jamie Purchase for sending me this example.

import java.io.File
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder

fun main() {

    // Input File
    val input = File("/path/to/file.css")

    // Create Content
    val content = StringBuilder().apply {
        append(URLEncoder.encode("input", "UTF-8"))
        append("=")
        append(URLEncoder.encode(input.readText(), "UTF-8"))
    }.toString()

    // Create Request
    val request = (URL("https://cssminifier.com/raw").openConnection() as HttpURLConnection).apply {
        requestMethod = "POST"
        doOutput = true
        setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
        setRequestProperty("charset", "utf-8")
        setRequestProperty("Content-Length", content.length.toString())
        OutputStreamWriter(outputStream).apply {
            write(content)
            flush()
        }
    }

    // Parse Response
    if(request.responseCode == 200) {

        // Print Result
        println(InputStreamReader(request.inputStream).readText())
    }

    // Handle Error
    else println("Error: ${request.responseCode} ${request.responseMessage}")

}

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: