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

File Location

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

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:

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.

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

Integrate with theme colors:

Set up in hooks.js:

Utility Classes

Generate utility classes based on properties:

Animation Properties

Dynamic animations based on user settings:

Last updated

Was this helpful?