For the complete documentation index, see llms.txt. This page is also available as Markdown.

CSS Templates

Using CSS files in the templates directory with template directives

CSS files placed in the templates/ directory are processed through the Elements template engine, allowing you to use property insertion, conditionals, and other template directives within your stylesheets. This enables dynamic, component-specific styling based on user properties.

Overview

CSS template files provide:

  • Property insertion - Use component properties in CSS values

  • Conditional styles - Include or exclude CSS based on conditions

  • Dynamic selectors - Generate class names from properties

  • Per-instance processing - Each component instance gets its own processed CSS

In the core components, most production styling is done with Tailwind utility classes generated in hooks.js and inserted into HTML with {{classes.*}} — see Component Styling. Template CSS files are used sparingly — mostly small editor-focused helpers, often gated by @if(edit) (see the Tabs example below).

File Location

com.yourcompany.component/
├── templates/
│   ├── index.html
│   ├── styles.css         # Component styles
│   └── include/
│       └── ...

Any .css file at the root of templates/ is processed — the filename is up to you. The core components use names like styles.css (Reveal), edit.css (Grid), and editor-ui.css (Tabs, Container, Shapes).

Basic Usage

CSS files can use standard CSS syntax along with Elements template directives:

Property Insertion

Insert component properties directly into CSS values:

Conditional Styles

Use @if directives to conditionally include CSS rules. Template @if accepts a single condition only — compute comparisons in hooks.js first, then test the resulting boolean. See Combining Conditions.

Component ID Scoping

Use the unique component {{id}} to scope styles to specific instances:

This is particularly useful when multiple instances of the same component exist on a page with different property values.

Examples from Core Components

Reveal Component (Dynamic Groups)

The Reveal component uses property insertion for dynamic group styling:

This creates group selectors scoped to each component instance.

Tabs Component (Edit-Mode Styles)

Several core CSS templates are small edit-mode helpers gated by @if(edit). The Tabs component's templates/editor-ui.css hides every tab panel except the one being edited:

editorActiveTabIndex is computed in hooks.js from the tab currently selected in the editor. Because the whole file is wrapped in @if(edit), it produces no output at all on the published site.

Responsive Styles

Generate responsive styles based on properties:

Processing Behavior

Per-Instance Processing

CSS template files are processed once per component instance. If you have three instances with different properties, you'll get three sets of CSS rules:

Inline Styles

The processed CSS is inserted inline into the page within <style> tags, not as external stylesheets.

Templates vs Assets

Choose the right location for your CSS:

Use templates/ CSS When:

  • Styles depend on component properties

  • Styles need conditional logic

  • Styles are instance-specific

  • You need dynamic selectors or values

Example:

Use assets/ CSS When:

  • Styles are static and don't change

  • Styles are shared across all instances

  • You want better caching and performance

  • Styles are large frameworks or libraries

Example:

Combining Both

A common pattern is to use both:

Best Practices

Scope with Component ID

Always scope your CSS to prevent conflicts:

Keep Templates CSS Minimal

Only include CSS that actually needs property insertion:

Use CSS Custom Properties

For complex styling systems, consider using CSS custom properties:

This keeps the template processing minimal while allowing CSS to handle the styling logic.

Avoid Heavy Computation

Don't use template CSS for complex calculations. Do that in hooks.js:

Consider Performance

Remember that each component instance generates CSS. For components that might appear many times:

In your HTML template:

Limitations

No @import

CSS @import statements won't work in template CSS files as they're processed as inline styles:

Limited Preprocessor Support

Template CSS files don't support Sass, Less, or other preprocessors. Use the Elements template language for logic instead.

Browser Compatibility

Generated CSS still needs to be valid CSS. Template directives don't add vendor prefixes or polyfills:

Common Patterns

Theme Integration

There is no hooks API for reading theme colors — rw.theme exposes breakpoints, not colors. Core components apply theme colors with Tailwind utility classes instead: a Theme Color control with format: "bg-{{value}}" delivers a ready-made class such as bg-brand-500, which hooks.js merges into the component's class strings:

If you need a theme color inside a CSS template (gradient stops, SVG fills), Tailwind 4 themes expose every theme color as a --color-* CSS custom property — format a Theme Color control as var(--color-{{value}}) and interpolate that. See Component Styling for the full approach.

Utility Classes

Generate utility classes based on properties:

Animation Properties

Dynamic animations based on user settings. Compute the animation booleans in hooks.js first:

Last updated

Was this helpful?