No description
  • Standard ML 65%
  • OCaml 34.3%
  • Nix 0.4%
  • Dune 0.3%
Find a file
2026-03-23 00:32:43 +01:00
bin implement pseudo-classes and pseudo-elements 2026-03-19 12:35:00 +01:00
lib implement some raw properties 2026-03-23 00:32:43 +01:00
.ocamlformat ocamlformat: break string literals 2026-02-15 17:40:57 +01:00
dune-project specify lower bound for ppxlib too 2025-11-19 19:20:34 +01:00
flake.lock bump nixpkgs 2025-12-24 00:22:11 +01:00
flake.nix bump nixpkgs 2025-12-24 00:22:11 +01:00
LICENSE add license 2025-05-04 14:31:31 +02:00
README.md adapt README to latest changes 2026-03-14 22:37:25 +01:00

ppx_stylesheet

ppx_stylesheet is a CSS-in-OCaml library. It allows you to define CSS rules in your OCaml source code and generate a CSS file out of them at compile time.

This project is inspired by elm-css and jsoo-css.

Example

open Css

(* this will define the global CSS variables --red and --page-width *)
let%css red = rgb 255 0 0

let%css _page_width = `px 1000

(* box_styles is a string containing an auto-generated class name *)
let box_styles =
  [%css
    [ margin `zero `auto
    ; padding (`px 10)
    ; background_color (var red)
    ; width_ "calc(var(--page-width) * 0.8)"
    ]]
in
(* it's compatible with any HTML generation library, e.g. dream-html *)
div [class_ box_styles] [ p [] [txt "This is a red box"] ]

After running dune build the following CSS file is generated at _build/stylesheet.css:

/* Main */
:root {
  --page-width: 1000px;
  --red: rgb(255 0 0);
}
.c658277527 {
  background-color: var(--red);
  margin: 0;
  padding: 10px;
  width: calc(var(--page-width) * 0.8);
}

Features

  • no runtime overhead, a single CSS file is generated at compile-time
  • CSS properties are type-checked by the compiler (except for raw properties ending in '_')
  • CSS class names are auto-generated by calculating a hash over the contained rules
  • syntax highlighting, auto-completion and other LSP features work for CSS rules too
  • define and use global CSS variables

Limitations

Some CSS features are not implemented yet:

  • animations
  • pseudo-classes and pseudo-elements
  • at-rules such as media queries
  • functions such as calc