Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Dynamic Classes and Styles | Section
Vue.js Fundamentals and App Development

bookDynamic Classes and Styles

メニューを表示するにはスワイプしてください

Vue allows you to apply classes and styles dynamically based on data. This helps change the appearance of elements depending on the application state.

To bind classes, use the :class directive.

<script setup>
import { ref } from "vue";

const isActive = ref(true);
</script>

<template>
  <p :class="{ active: isActive }">Status</p>
</template>

<style>
.active {
  color: #4f46e5;
  font-weight: bold;
}
</style>

When isActive is true, the active class is applied. If it becomes false, the class is removed.

You can also apply multiple classes.

<script setup>
import { ref } from "vue";

const isActive = ref(true);
const isError = ref(false);
</script>

<template>
  <p :class="{ active: isActive, error: isError }">Message</p>
</template>

To bind inline styles, use the :style directive.

<script setup>
import { ref } from "vue";

const color = ref("blue");
</script>

<template>
  <p :style="{ color: color }">Styled text</p>
</template>

Dynamic classes and styles allow you to control the appearance of elements based on your data.

question mark

Which Vue directive is used to bind dynamic CSS classes to an element?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  21

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  21
some-alt