Ember Octane is here! A lot has changed since Ember 3.14, including these Guides. Read more in the Ember Blog.

Old Guides - You are viewing the guides for Ember v1.10.0. VIEW v3.15.0
Edit Page

Adding CSS Classes to Your Components Based on Properties


Problem

You want to add or remove CSS class names to your Ember Components based on properties of the component.

Solution

Add property names to the classNameBindings property of subclassed components.

Discussion

You can apply classes based on properties of the component, or even by properties bound to data passed into the component. This is done by binding the class attribute using classNameBindings.

classNameBindings: ['active'],
active: true

You can also set the class name based on a computed property.

classNameBindings: ['isActive'],
isActive: function() {
  return 'active';
}.property('someAttribute')

Another way would be to bind the class name to a bound property.

classNameBindings: ['isRelated:relative'],
isRelatedBinding: "content.isRelated" // value resolves to boolean

Example

JS Bin

See Customizing a Component's Element for further examples.