Dynamic styling is a crucial part of building interactive user interfaces.
Vue allows you to dynamically bind CSS classes and inline styles to your HTML elements.
This is achieved by using the v-bind directive (or its shorthand :) on the class and style attributes.
You can pass a JavaScript object to :class to dynamically toggle classes.
The key is the class name you want to apply, and the value is a boolean condition.
If the boolean condition is true, the class is added. If false, it is removed.
<div :class="{ active: isActive }">
I am active!
</div>
You can also pass an array to :class to apply multiple classes at the same time.
This is useful when you have class names stored directly as strings inside your data variables.
<div :class="[activeClass, errorClass]"></div>
Binding inline styles works very similarly to binding classes.
You bind to the style attribute using :style and pass an object.
CSS properties should be written in camelCase (e.g., fontSize instead of font-size) within the JavaScript object.
<div :style="{ color: activeColor, fontSize: fontSize + 'px' }">
Inline styling is easy!
</div>
When using the object syntax for :class, what does the boolean value dictate?
How should CSS properties with hyphens be written when passing them to :style as a JavaScript object?