[Try it Β»](#)
The rendering result of Example 3 is the same as Example 2.
Furthermore, we can bind to a computed property that returns an object. This is a common and powerful pattern:
## Example 4
data(){return{isActive: true, error: null}}, computed: {classObject(){return{active: this.isActive&& !this.error, 'text-danger': this.error&&this.error.type === 'fatal'}}}
[Try it Β»](#)
### Array Syntax
We can pass an array to **v-bind:class**, as shown in the following example:
## Example 5
[Try it Β»](#)
The rendered `div` class in the above example is:
We can also use a ternary expression to toggle classes in the list:
## Example 6
`errorClass` is always present. `activeClass` is added when `isActive` is `true`:
[Try it Β»](#)
The rendered `div` class in the above example is:
* * *
## Vue.js Style (Inline Styles)
We can set styles directly in `v-bind:style`, which can be abbreviated as `:style`:
## Example 7
[Try it Β»](#)
The rendered `div` style in the above example is:
!(#)
We can also bind directly to a style **object** to make the template cleaner:
## Example 8
[Try it Β»](#)
**v-bind:style** can use an array to apply multiple style objects to a single element:
## Example 9
[Try it Β»](#)
> Note: When **v-bind:style** uses CSS properties that require specific prefixes, such as `transform`, Vue.js will automatically detect and add the appropriate prefixes.
### Multiple Values
You can provide an array of multiple values for a property in style bindings, often used to provide multiple prefixed values, for example:
This will only render the last value in the array that the browser supports. In this example, if the browser supports flexbox without a vendor prefix, it will only render `display: flex`.
* * *
## Using Class Attribute on Components
When you use the `class` attribute on a custom component with a single root element, these classes will be added to that element. Existing classes on this element will not be overwritten.
## Example 10
// Create a Vue app const app = Vue.createApp({}) // Define a new global component named app.component('', { template: '
I like !
' }) app.mount('#app') [Try it Β»](#) The rendered `div` class in the above example is:I like !
!(#) This also applies to components with data-bound classes: When `isActive` is `true`, the HTML will be rendered as:Hi
If your component has multiple root elements, you need to define which parts will receive this class. You can use the `$attrs` component property to do this: ## Example 11 const app = Vue.createApp({}) app.component('', { template: `I like !
This is a child component ` }) app.mount('#app') [Try it Β»](#) **Note**: In the template, `` ` `` is a backtick, not a single quote `'`. The rendered `div` class in the above example is:I like !
This is a child component
YouTip