CSS classes and styling
Class Binding in Aurelia
Aurelia provides flexible and powerful ways to dynamically manage CSS classes on HTML elements. You can bind classes conditionally, dynamically, and with different binding modes.
Basic Class Binding Syntax
Aurelia offers multiple ways to bind classes to an element:
String Interpolation
Binding Directive
One-Time Binding
Conditional Class Binding
You can apply classes based on component state or conditions:
Binding Modes
Aurelia supports different binding modes for classes:
Default
.bind
Updates dynamically, two-way binding on form inputs.
One-Time
.one-time
Sets class once, no future updates
One-Way
.to-view
Updates from view-model to view
Style Binding in Aurelia
Aurelia provides robust methods for dynamically binding styles to HTML elements, supporting string and object-based style assignments.
String-based Style Binding
You can bind styles using a string representation of CSS:
Object-based Style Binding
Bind styles using a JavaScript object for more structured style management:
Style Interpolation
Incorrect Interpolation
Correct Interpolation
CSS Custom Attribute Usage
The css
attribute is crucial for style interpolation, especially for compatibility with Internet Explorer and Edge:
Important Notes
Use
css
for string interpolationStatic styles can use standard
style
attributeInterpolated styles require the
css
attributeObjects and strings can both be bound to styles
Last updated
Was this helpful?