Styling components
Aurelia provides powerful and flexible ways to manage CSS classes and inline styles for your elements. This documentation covers the various methods to manipulate classes and styles, including working with ShadowDOM.
Class Bindings
Aurelia provides several ways to bind an element's class attribute dynamically. These methods allow you to add or remove classes based on your application's state.
String Interpolation
You can use string interpolation to apply classes conditionally:
This approach allows you to combine static classes with dynamic ones.
Binding Expressions
Alternatively, you can use .bind
or .to-view
to set classes:
For one-time bindings, use .one-time
:
Class Name Binding
For performance-critical situations, you can bind directly to the className
property:
This method is faster but less safe, as it may overwrite classes added by other code.
Style Bindings
Aurelia allows you to bind styles to elements using both string and object formats.
String Binding
You can bind a CSS string directly to the style
attribute:
Where styleString
in your view-model might look like:
Object Binding
You can also bind a style object:
With a corresponding view-model:
The css
Attribute
css
AttributeWhen using string interpolation for styles, use the css
attribute instead of style
to ensure compatibility with Internet Explorer and Edge:
Note that css
should only be used when you need interpolation. For static styles, use the standard style
attribute:
Working with Shadow DOM
When using Shadow DOM, class and style bindings work similarly but only affect the shadow root's elements. To style the host element, you'll need to use :host
in your CSS:
Last updated
Was this helpful?