Styling
This page goes over how to style your pages in Elegance.
CSS Object
Element's support style={} objects, just as is normal in JSX.
You can use them like this:
<button
style={{
backgroundColor: "black",
}}
>
</button>Obviously these also work in function-style:
button({
style: {
backgroundColor: "black";
}
});CSS Files
If you're slightly more sane, you'll want .css files to write your styles in. The easiest way to write these is to put them in your public/ directory, but for a more robust approach, you can put them in your pages/ directory, and use our build hooks to publish them.
CSS Frameworks
If you'd like to use something like tailwind, postcss, sass, etc; those are all very much usable in Elegance.
Sass / SCSS
npm i sass
import sass from "sass";
import { dirname } from "node:path";
export const config: EleganceConfig = {
css: {
transformers: [
{
test: /\.(scss|sass)$/,
transform: (source, file) =>
sass.compileString(source, {
loadPaths: [dirname(file), "node_modules"],
// .sass uses the indented syntax — must be explicit
syntax: file.endsWith(".sass") ? "indented" : "scss",
}).css,
},
],
},
};Less
npm i less
import less from "less";
import { dirname } from "node:path";
{
test: /\.less$/,
transform: (source, file) =>
less.render(source, {
paths: [dirname(file), "node_modules"],
filename: file,
}).then((out) => out.css),
}Stylus
npm i stylus
import stylus from "stylus";
import { dirname } from "node:path";
{
test: /\.styl(us)?$/,
transform: (source, file) =>
new Promise<string>((resolve, reject) => {
stylus(source)
.set("filename", file)
.set("paths", [dirname(file)])
.render((err, css) => (err ? reject(err) : resolve(css)));
}),
}PostCSS (autoprefixer, nesting, etc.)
PostCSS's JS API does not auto-load postcss.config.js — pass plugins directly:
npm i postcss autoprefixer postcss-nested
import postcss from "postcss";
import autoprefixer from "autoprefixer";
import postcssNested from "postcss-nested";
{
test: /\.(css|pcss|postcss)$/,
transform: (source, file) =>
postcss([postcssNested(), autoprefixer()])
.process(source, { from: file })
.then((r) => r.css),
}Tailwind v4
The official path is its PostCSS plugin (@tailwindcss/postcss handles @import "tailwindcss" and source detection itself; from drives its base directory):
npm i tailwindcss @tailwindcss/postcss postcss
You'll need this somewhere in a css file.
@import "tailwindcss";Add this to your css transformers:
import postcss from "postcss";
import tailwindcss from "@tailwindcss/postcss";
{
test: /\.css$/,
transform: (source, file) =>
postcss([tailwindcss()])
.process(source, { from: file })
.then((r) => r.css),
}Tailwind v3 (needs content config)
npm i tailwindcss@3 postcss autoprefixer
import postcss from "postcss";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";
{
test: /\.(css|pcss|postcss)$/,
transform: (source, file) =>
postcss([tailwindcss(), autoprefixer()])
.process(source, { from: file })
.then((r) => r.css),
}export default {
/* ... */
content: ["./pages/**/*.{tsx,ts}"],
/* ... */
};Chaining multiple processors
Transformer order matters. To run Sass first, then autoprefix everything (including compiled output), the post-process step must match the original file paths too:
transformers: [
{ test: /\.(scss|sass)$/, transform: sassTransform },
{ test: /\.less$/, transform: lessTransform },
{
test: /\.(css|scss|sass|less|styl|stylus|pcss|postcss)$/,
transform: (source, file) =>
postcss([autoprefixer()]).process(source, { from: file }).then((r) => r.css),
},
]