Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Style Binding

Vue 3 Style Binding

Vue.js class

class and style are attributes of HTML elements used to set element styles; we can use v-bind to set style attributes.

When v-bind handles class and style, the expression can be a string, an object, or an array, in addition to a string.

v-bind:class can be abbreviated as :class.


Class Attribute Binding

We can set an object for v-bind:class to dynamically toggle class:

Example 1

In the example, setting isActive to true shows a green div block; if set to false, it does not show:

<div:class=">

{ 'active': isActive }

"

div

Try it »

The rendered result of the div class in the example above is:

We can also pass more properties in the object to dynamically toggle multiple classes.

In addition, the :class directive can coexist with a normal class attribute.

Example 2

The text-danger class background color overrides the active class background color:

<div class="static" :class=">

{ 'active' : isActive, 'text-danger' : hasError }

"

div

Try it »

The rendered result of the div class in the example above is:

When isActive or hasError changes, the class attribute value updates accordingly. For example, if active is true, the class list becomes "static active text-danger".

We can also directly bind an object from the data:

Example 3

The text-danger class background color overrides the active class background color:

<div id="app">
<div class="static" :class="classObject" div div>

Try it »

Example 3 and Example 2 produce the same rendering result.

In addition, we can also bind a computed property that returns an object here. 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 ; the example is as follows:

Example 5

<div class="static" :class=">

[activeClass, errorClass]

"

div

Try it »

The rendered result of the div class in the example above is:

We can also use a ternary expression to toggle a class in the list:

Example 6

errorClass is always present; when isActive is true, the activeClass is added:

<div id="app">
<div class="static" :class=">

[isActive ? activeClass : '', errorClass]

"

div

div

Try it »

The rendered result of the div class in the example above is:


Vue.js style (inline styles)

We can set styles directly in v-bind:style, which can be abbreviated as :style:

Example 7

<div id="app">
<div:style=">

{ color: activeColor, fontSize: fontSize + 'px' }

"

Runoob Tutorial

div

div

Try it »

The rendered result of the div style in the example above is:

Runoob Tutorial

You can also directly bind to a styleobjectto make the template clearer:

Example 8

<div id="app">
<div:style="styleObject">

Runoob Tutorial

div

div

Try it »

v-bind:style You can use an array to apply multiple style objects to an element:

Example 9

<div id="app">
<div:style=">

[baseStyles, overridingStyles]

"

Runoob Tutorial

div

div

Try it »

Note: when v-bind:style uses CSS properties that require a specific prefix, such as transform, Vue.js automatically detects and adds the corresponding prefix.

Multiple Values

You can provide an array containing multiple values for a property in a style binding, commonly used to provide multiple prefixed values, for example:

Writing it this way only renders the last value in the array that the browser supports. In this case, if the browser supports unprefixed flexbox, only display: flex is rendered.


Using the class Attribute on Components

When you use the class attribute on a custom component with a single root element, these classes are added to that element. Existing classes on this element will not be overwritten.

Example 10

<

div

id

=

"

app

"

<

runoob

class

=

"

classC classD

"

runoob

div

<

script

// Create a Vue app const app = Vue.createApp({}) // Define a new global component named runoob app.component('runoob', { template: '

<h1 class=">

classA classB

"

I like runoob!

h1

' }) app.mount('#app')

script

Try it »

The rendered result of the div class in the example above is:

I like runoob!

This also applies to data-bound class:



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 do this using the $attrs component property:

Example 11

<

div

id

=

"

app

"

<

runoob

class

=

"

classA

"

runoob

div

<

script

const app = Vue.createApp({}) app.component('runoob', { template: `

<p:class="$attrs.class">

I like runoob!

p

<

span

This is a child component

span

` }) app.mount('#app')

script

Try it »

Note: In the template, ` is a backtick, not a single quote '.

The rendered result of the div class in the example above is:

I like runoob!

这是一个子组件