Styling
How CSS reaches a node, what the stylesheet engine supports, and the Tailwind paths.
Four ways to put CSS on a node:
styleprop: inlineCSSPropertieson one node. Overrides the tag default.twprop: Tailwind classes on one node.<style>tags: CSS that travels inside the JSX.stylesheetsoption: full stylesheets matched againstclassNameandid.
Stylesheets
Pass real CSS through stylesheets and match it by className or id.
import { } from "takumi-js";
const = await (< ="card">Hello</>, {
: 1200,
: 630,
: [`.card { display: flex; padding: 48px; background: #0f172a; color: white; }`],
});The engine handles:
| Selectors | At-rules | Properties |
|---|---|---|
| class, id, descendant | @keyframes, @media, @supports | custom properties with var(), shorthands, gradients, box-shadow, filter, backdrop-filter, mix-blend-mode, transform |
A render is one static frame, so interactive pseudo-classes like :hover and :focus parse but
never match.
A <style> tag feeds the same engine and gets extracted from the JSX automatically.
<div className="card">
<style>{`.card { display: flex; padding: 48px; }`}</style>
Hello
</div>Tailwind
Bring your stylesheet
Compile Tailwind with your bundler, then pass the CSS through stylesheets. On Vite, import it with the ?inline query.
import { ImageResponse } from "takumi-js/response";
import stylesheet from "~/styles/global.css?inline";
export function GET() {
return new ImageResponse(
<div className="bg-background text-foreground flex justify-center items-center w-full h-full text-4xl">
Hello Tailwind!
</div>,
{
width: 1200,
height: 630,
stylesheets: [stylesheet],
},
);
}import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [tailwindcss()],
});Native parser
The tw prop runs a built-in parser with no build step. It won't cover every Tailwind feature.
- Arbitrary values work.
- See the parser mapping for every supported class.
tw has no Tailwind Preflight, so elements keep their UA margins. A <h1> gets a 0.67em top
margin until you add mt-0. box-sizing still defaults to border-box.
Utilities lose to any rule from stylesheets, the way @layer utilities loses to unlayered CSS. Mark the utility with ! to win instead.
<div tw="bg-blue-500 p-4 rounded-lg">
<h1 tw="text-white text-2xl font-bold">Hello Tailwind!</h1>
</div>Theme tokens
A utility reads a CSS custom property, the way Tailwind compiles it. bg-red-500 resolves var(--color-red-500) and p-4 resolves calc(var(--spacing) * 4), with the built-in scale behind them. Define the variable and the utility follows.
:root {
--color-brand-500: #5b21b6;
--spacing-gutter: 2.5rem;
}<div tw="bg-brand-500 p-gutter" />Pass the same tokens as a variables option when it is easier to build them in code. They become a :root rule appended after your stylesheets, so an equally specific :root declaration in your CSS loses to them. A declaration on the element itself, or an !important one, still wins.
const image = await render(<div tw="bg-brand-500 p-gutter" />, {
width: 1200,
height: 630,
variables: { "--color-brand-500": "#5b21b6", "--spacing-gutter": "2.5rem" },
});Because the value comes from the cascade, redefining a token under a media query or a selector works like it does for any other custom property:
:root {
--color-brand-500: #5b21b6;
}
@media (prefers-color-scheme: dark) {
:root {
--color-brand-500: #a78bfa;
}
}The namespace picks which utilities a token reaches:
| Namespace | Utilities |
|---|---|
--color-* | color utilities: bg-*, text-*, border-*, outline-*, decoration-* |
--spacing-* | length utilities: p-*, m-*, w-*, h-*, gap-* |
--container-* | max-w-* |
--text-* | font sizes, text-xl |
--font-* | font families, font-sans |
--font-weight-* | font weights, font-bold |
--tracking-* | tracking-* |
--leading-* | leading-* |
--radius-* | rounded-* |
--aspect-* | aspect-* |
Some things behave differently from Tailwind here:
- Gradients, shadows, filters, transforms and animations merge across utilities before the cascade runs, so
--shadow-*,--drop-shadow-*,--text-shadow-*,--blur-*and--animate-*are not read. - Directional corners (
rounded-t-*), logical sides (ms-*,ps-*) and the bareroundedandaspect-videoshorthands keep their built-in values. - A color with an opacity modifier,
bg-brand-500/50, uses the built-in scale rather than the variable. --color-red-500: initialfalls back to the built-in red instead of removingbg-red-500.--spacingneeds a unit. A bare number makesp-4compute pixels here, where a browser rejects the declaration.
tw is a plain prop, so build it dynamically:
import clsx from "clsx";
const isError = true;
<div tw={clsx("p-4 rounded", isError ? "bg-red-100 text-red-700" : "bg-green-100 text-green-700")}>
{isError ? "Something went wrong" : "Success!"}
</div>;Last updated on