home
  • Blog
1.10
  • Getting Started
  • Getting Ember
  • Concepts
  • The Object Model
  • Application
  • Templates
  • Routing
  • Components
  • Controllers
  • Models
  • Views
    • Introduction
    • Defining a View
    • Handling Events
    • Inserting Views in Templates
    • Adding Layouts to Views
    • Customizing a View's Element
    • Built-in Views
    • Manually Managing View Hierarchy
  • Enumerables
  • Testing
  • Configuring Ember.js
  • Cookbook
  • Understanding Ember.js
  • Contributing to Ember.js
Old Guides - You are viewing the guides for Ember v1.10.0.
Go to v5.0.0

Customizing a View's Element

Edit pencil

A view is represented by a single DOM element on the page. You can change what kind of element is created by changing the tagName property.

App.MyView = Ember.View.extend({
  tagName: 'span'
});

You can also specify which class names are applied to the view by setting its classNames property to an array of strings:

App.MyView = Ember.View.extend({
  classNames: ['my-view']
});

If you want class names to be determined by the state of properties on the view, you can use class name bindings. If you bind to a Boolean property, the class name will be added or removed depending on the value:

App.MyView = Ember.View.extend({
  classNameBindings: ['isUrgent'],
  isUrgent: true
});

This would render a view like this:

<div class="ember-view is-urgent">

If isUrgent is changed to false, then the is-urgent class name will be removed.

By default, the name of the Boolean property is dasherized. You can customize the class name applied by delimiting it with a colon:

App.MyView = Ember.View.extend({
  classNameBindings: ['isUrgent:urgent'],
  isUrgent: true
});

This would render this HTML:

<div class="ember-view urgent">

Besides the custom class name for the value being true, you can also specify a class name which is used when the value is false:

App.MyView = Ember.View.extend({
  classNameBindings: ['isEnabled:enabled:disabled'],
  isEnabled: false
});

This would render this HTML:

<div class="ember-view disabled">

You can also specify to only add a class when the property is false by declaring classNameBindings like this:

App.MyView = Ember.View.extend({
  classNameBindings: ['isEnabled::disabled'],
  isEnabled: false
});

This would render this HTML:

<div class="ember-view disabled">

If the isEnabled property is set to true, no class name is added:

<div class="ember-view">

If the bound value is a string, that value will be added as a class name without modification:

App.MyView = Ember.View.extend({
  classNameBindings: ['priority'],
  priority: 'highestPriority'
});

This would render this HTML:

<div class="ember-view highestPriority">

Attribute Bindings on a View

You can bind attributes to the DOM element that represents a view by using attributeBindings:

App.MyView = Ember.View.extend({
  tagName: 'a',
  attributeBindings: ['href'],
  href: "http://emberjs.com"
});

You can also bind these attributes to differently named properties:

App.MyView = Ember.View.extend({
  tagName: 'a',
  attributeBindings: ['customHref:href'],
  customHref: "http://emberjs.com"
});

Customizing a View's Element from Handlebars

When you append a view, it creates a new HTML element that holds its content. If your view has any child views, they will also be displayed as child nodes of the parent's HTML element.

By default, new instances of Ember.View create a <div> element. You can override this by passing a tagName parameter:

{{view "info" tagName="span"}}

You can also assign an ID attribute to the view's HTML element by passing an id parameter:

{{view "info" id="info-view"}}

This makes it easy to style using CSS ID selectors:

/** Give the view a red background. **/
#info-view {
  background-color: red;
}

You can assign class names similarly:

{{view "info" class="info urgent"}}

You can bind class names to a property of the view by using classBinding instead of class. The same behavior as described in bind-attr applies:

App.AlertView = Ember.View.extend({
  priority: "p4",
  isUrgent: true
});
{{view "alert" classBinding="isUrgent priority"}}

This yields a view wrapper that will look something like this:

<div id="ember420" class="ember-view is-urgent p4"></div>
left arrow
Adding Layouts to Views
Built-in Views
right arrow
On this page

  • Attribute Bindings on a View
  • Customizing a View's Element from Handlebars
Team Sponsors Security Legal Branding Community Guidelines
Twitter GitHub Discord Mastodon

If you want help you can contact us by email, open an issue, or get realtime help by joining the Ember Discord.

© Copyright 2023 - Tilde Inc.
Ember.js is free, open source and always will be.


Ember is generously supported by
blue