← Back to all snippets
JAVASCRIPT

Dynamic Class and Style Binding in Vue 3

Learn to conditionally apply CSS classes and inline styles to elements in Vue 3 based on component state, enhancing dynamic UI responsiveness and user experience.

<template>
  <div>
    <!-- Object Syntax for Classes -->
    <div :class="{ 'active-item': isActive, 'error-text': hasError }">
      Item with dynamic classes
    </div>

    <button @click="toggleActive">Toggle Active</button>
    <button @click="toggleError">Toggle Error</button>

    <!-- Array Syntax for Classes -->
    <div :class="[baseClass, { 'highlight': isHighlighted }]" class="static-class">
      Another item with array classes
    </div>
    <button @click="toggleHighlight">Toggle Highlight</button>

    <!-- Object Syntax for Inline Styles -->
    <div :style="{ color: textColor, fontSize: fontSize + 'px' }">
      Text with dynamic styles
    </div>
    <button @click="increaseFontSize">Increase Font Size</button>

    <!-- Array Syntax for Inline Styles (less common, but possible) -->
    <div :style="[baseStyles, conditionalStyles]">
      Text with array styles
    </div>
    <button @click="toggleBorder">Toggle Border</button>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';

const isActive = ref(true);
const hasError = ref(false);
const isHighlighted = ref(false);
const fontSize = ref(16);
const showBorder = ref(false);

const baseClass = 'common-style';

const textColor = ref('green');

const baseStyles = reactive({
  backgroundColor: '#eee',
  padding: '10px'
});

const conditionalStyles = computed(() => ({
  border: showBorder.value ? '1px solid black' : 'none'
}));

function toggleActive() {
  isActive.value = !isActive.value;
}

function toggleError() {
  hasError.value = !hasError.value;
}

function toggleHighlight() {
  isHighlighted.value = !isHighlighted.value;
}

function increaseFontSize() {
  fontSize.value += 2;
}

function toggleBorder() {
  showBorder.value = !showBorder.value;
}
</script>

<style scoped>
.active-item {
  background-color: lightblue;
  border: 1px solid blue;
  padding: 10px;
}
.error-text {
  color: red;
  font-weight: bold;
}
.common-style {
  margin-top: 10px;
  padding: 5px;
  border: 1px solid gray;
}
.highlight {
  box-shadow: 0 0 5px orange;
}
</style>
How it works: This snippet illustrates how to dynamically bind CSS classes and inline styles in Vue 3. For classes, you can use an object syntax (`:class="{ 'class-name': condition }"`) to conditionally apply classes or an array syntax (`:class="[staticClass, { 'conditional': condition }]"`) to combine multiple class names. Similarly, for inline styles, an object syntax (`:style="{ 'property': value }"`) allows dynamic styling, useful for changing colors, font sizes, or other CSS properties based on component data.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs