The :class attribute in Vue is used to dynamically set the CSS class of an element. It can add or remove classes, bound class arrays, or objects based on conditions. It accepts an object, array, or string containing a CSS class name as a value, can only be applied to elements with a class attribute, and does not override existing classes.
Usage of class in Vue
:class attribute
:class is a property in Vue that allows dynamically setting the CSS class of an element. It accepts an object, array, or string containing the CSS class name as a value.
Syntax
<code>:class="['class1', 'class2', { 'class3': true }]"</code>
Usage
: The class attribute is mainly used to dynamically add or remove CSS classes based on data. It can implement the following functions:
:class="{ 'active': isVisible }"
:class="['class-' propValue]"
:class="classArray"
:class="{ 'class1': true, 'class2': false }"
Example
<code class="html"><div :class="{ 'active': isVisible, 'error': hasError }"></div></code>
The above example will dynamically add or remove the active
and error
classes based on the values of isVisible
and hasError
.
<code class="html"><div :class="classArray"></div></code>
The above example will bind an array of classes containing the class names to be added to the element.
Note
class
attribute. The above is the detailed content of In vue: usage of class. For more information, please follow other related articles on the PHP Chinese website!