How to binding inline styles in Vue.js ?
Last Updated :
24 Apr, 2025
Vue.js is an open-source Model–View–ViewModel front-end JavaScript framework for building user interfaces and single-page applications. Every Web application needs HTML CSS styles for presenting a better User Interface. Vue.js allows you to dynamically render styling with data model structure. Usually, we can bind the styles using <style> tag with HTML attributes. Vue.js has many in-built directives. In Vue.js, we can use v-bind: style directive for binding Inline styles. The v-bind directive is used for binding styles and adding class attributes. Here, the :style is used as short-hand for v-bind:style. In this article, we will see how to Binding Inline Styles in Vue.js. In Vue.js, use camel case for each CSS attributes in v-bind:style. For instance, the format represents the basic implementation:
{ backgroundColor: 'yellow', textAlign: 'left', fontWeight: "bold" }
There are two ways for binding the Inline Styles in Vue.js:
- Binding to Objects
- Binding to Arrays
We will explore the above approaches & will understand them sequentially.
Binding to Objects: The object is needed to pass to the :style directive, which is the shorthand for the v-bind:style, which enables dynamic styles. It helps to supports binding to JavaScript object values.
Approach 1: In this approach, We will bind the styles with HTML elements directly, it's known as Inline styles.
Syntax:
<span v-bind:style = "{ color: textColor, fontSize: 2px }">
GeeksforGeeks
<span>
Example 1: In this example, we are adding styles directly inside the HTML elements. Now, create a two-way data flow binding text using the v-model directive in the input box and binding inline styles dynamically for input text appearing in DOM. If you need to increase the font size of sample text use the + button and use the - button to decrease it. Inline event handlers are used for buttons to increase and decrease the text size.
HTML
<template>
<div>
<!--binding Inline styles for
heading text ( using v-bind directive )-->
<h1 v-bind:style="{ color: 'green' }">
GeeksforGeeks
</h1>
<h3 v-bind:style="{ backgroundColor: 'yellow',
color: 'red',
textAlign: 'left',
padding: '1rem',}">
Binding Inline styles in Vue.js
</h3>
<button v-on:click="sizeOfFont++">
+
</button>
<input v-model="sampleText"
placeholder="Type Text Here" />
<button v-on:click="sizeOfFont--">
-
</button>
<!--binding styles dynamically for sample
text( using v-bind directive )-->
<h4 v-bind:style="{ color: 'green',
fontSize: sizeOfFont + 'px' }">
{{ sampleText }}
</h4>
</div>
</template>
<script>
// Creating a vue component
export default {
name: "App",
data() {
return {
sampleText: "",
sizeOfFont: 20,
};
},
};
</script>
Output:
Binding to Arrays: The :style directive can be bound to an array of multiple style objects.
Approach 2: In this approach, We will use the v-bind directive to add Inline styles through an array of objects with Vue components. An array of objects helps to add multiple style objects for HTML elements. we will add an extra object in the array syntax, it helps to bind more styles for each HTML element.
Syntax: Add multiple objects as values in the :style directive:
<span v-bind:style ="[geeks, textStyles]">
GeeksforGeeks
</span>
CSS styles are encapsulated as Data objects in the Vue component.
data: {
geeks: {
color: "green",
fontSize: "30px",
fontWeight: "bold"
},
textStyles: {
backgroundColor: 'yellow',
textAlign: 'left',
padding: '1rem'
}
}
Example 2: In the below example, we are adding Inline styles through an array of objects initialized by the Vue Component. Here, we are creating multiple objects for styles, binding two or more objects inside the array [ ] for v-bind:style directive.
HTML
<template>
<div>
<!--binding Inline styles through Array of
objects for heading text ( using v-bind directive )-->
<h1 v-bind:style="[geeks, textStyles]">
GeeksforGeeks
</h1>
<h3 v-bind:style="[textColor, textStyles]">
Binding Inline styles through array of objects
</h3>
</div>
</template>
<script>
// Creating a vue component
export default {
name: "App",
data() {
return {
//multiple objects for styles
geeks: { color: "green" },
textColor: { color: "red" },
textStyles: {
backgroundColor: "yellow",
textAlign: "left",
padding: "1rem",
},
};
},
};
</script>
Output:
Similar Reads
How to replace all occurrences of a string using Vue.js filters ?
In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation
2 min read
How to set a click event once a page or view is loaded in vue.js?
In this article, we will discuss how to set a click event once a page or view is loaded in vue.js. Just like every other JavaScript framework Vue also supports the Mounting hooks. Mounting hooks are often the most used hooks. They allow us to access or modify the DOM of your component just before or
2 min read
How to check an image is loaded or not in VueJS ?
While inserting an Image in a VueJS project, it may fail to load due to the following reasons: Writing wrong image URL.Due to poor connection Approach: We are going to use an event in <img> to check whether an image is loaded or not in VueJS, the event we are going to use is: @load: The @load
3 min read
How to loop through a list of elements and display it in VueJS ?
Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
2 min read
How to Make localStorage Reactive in Vue.js ?
In Vue, localStorage is used to store data locally in the browser. However, by default, changes made to localStorage are not reactive in Vue. This means that if the localStorage is updated outside of the Vue component, the component will not be aware of the changes and will not update accordingly. T
3 min read
How to implement DateTime localization in vue.js ?
In this article, we will see the implementation of Date Time localization in Vue.js, along with knowing its basic implementation through the illustration. Date() Object: In Javascript, the Date object works with dates and times. Date objects are created by the new Date() constructor. Syntax: const d
8 min read
Select different values on two select tags in Vue.js
In this article, we will see the selection of different values on a single select and two <select> tags in vue.js, along with understanding their basic implementation through the examples. HTML <select> tag is used to create a drop-down list. Adding a list of options inside the drop-down
6 min read
How to convert a normal string to a uppercase string using filter in VueJS ?
Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is t
2 min read
How to dynamically add or remove items from a list in Vue.js ?
Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
1 min read
How to binding inline styles in Vue.js ?
Vue.js is an open-source ModelâViewâViewModel front-end JavaScript framework for building user interfaces and single-page applications. Every Web application needs HTML CSS styles for presenting a better User Interface. Vue.js allows you to dynamically render styling with data model structure. Usual
4 min read