CSS DSL
CSS DSL extends HTML DSL and allows you to author stylesheets in Kotlin by using the kotlin-css wrapper.
Add dependencies
CSS DSL doesn't need installation, but requires including the following artifacts in the build script:
The
ktor-server-html-builderartifact for HTML DSL:implementation("io.ktor:ktor-server-html-builder:$ktor_version")implementation "io.ktor:ktor-server-html-builder:$ktor_version"<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-server-html-builder-jvm</artifactId> <version>${ktor_version}</version> </dependency>The
kotlin-css-jvmartifact for building CSS:implementation("org.jetbrains.kotlin-wrappers:kotlin-css:$kotlin_css_version")implementation "org.jetbrains.kotlin-wrappers:kotlin-css:$kotlin_css_version"<dependency> <groupId>org.jetbrains.kotlin-wrappers</groupId> <artifactId>kotlin-css</artifactId> <version>${kotlin_css_version}</version> </dependency>You can replace
$kotlin_css_versionwith the required version of thekotlin-cssartifact, for example,1.0.0-pre.625.
Serve CSS
To send a CSS response, you need to extend ApplicationCall by adding the respondCss method to serialize a stylesheet into a string and send it to the client with the CSS content type:
Then, you can provide CSS inside the required route:
Finally, you can use the specified CSS for an HTML document created with HTML DSL:
You can find the full example here: css-dsl.