Vue3 Fundamentals · Rendering & Templates

Inline-style bindings

Inline-style binding

Inline-style bindings

Some values only exist at runtime: a colour picked by the user, a width measured from the screen. A stylesheet cannot know them, so the style has to come from your data.

Vue binds the style attribute with an object of CSS properties:

<p :style="{ color: 'red' }">Out of stock</p>

Each key is a CSS property and each value is the value you would write in CSS. Vue applies the properties to the element, and updates them when the data changes.

The object is not a string: :style="{ color: 'red' }" sets one property, while style="color: red" would be a fixed attribute.

Property names follow JavaScript, so a hyphenated name becomes camelCase: font-size is written fontSize. You can also bind an array of style objects, which Vue merges, but a single object covers most cases.

Your job is to drive an element's colour from a style object instead of a hard-coded attribute.

Challenge

The warning text is the wrong colour and never changes. Export an App component from App.vue that binds the paragraph's style to an object setting color to red, so the text renders red.

The Test mounts your component and reads the paragraph's style attribute. The starter hard-codes a black colour, so the Test fails — bind :style to an object.

App.vue
<script setup>
const color = 'red';
</script>

<template>
  <p style="color: black">Out of stock</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge — you only see the result, never the test source.