Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js dynamic <style> with variables

Tags:

css

vue.js

vuejs2

Is it possible to add the dynamic variable in style?

I mean something like:

<style>     .class_name {         background-image({{project.background}});     }     @media all and (-webkit-min-device-pixel-ratio : 1.5),     all and (-o-min-device-pixel-ratio: 3/2),     all and (min--moz-device-pixel-ratio: 1.5),     all and (min-device-pixel-ratio: 1.5) {         .class_name {             background-image({{project.background_retina}});         }     } </style> 
like image 658
Dominik Traskowski Avatar asked Nov 16 '17 06:11

Dominik Traskowski


People also ask

How do I change my Vue style?

Vue. js provides style binding that you can use to change the style dynamically. You can bind a variable to the style attribute in any HTML tag and change the style when the bound variable is changed.

How can you bind styles in Vue JS?

Binding Styles Dynamically To this end, Vue provides us with the v-bind:style directive. On each click event, the v-bind:style directive increments and decrements the value of your fontSize variable. This attaches the value of fontSize to the CSS font-size property.

How do I add a conditional class to Vue?

We can conditionally add classes to a Vue component by using the v-bind:class directive. The shorthand for that is :class . to set the class according to the value of isRed . Since it's false initially, the green class will be applied.

How to add dynamic styles with variables with Vue JS?

To add dynamic styles with variables with Vue.js, we can use the style prop. to set the background-color of the image to backgroundColor where backgroundColor is a reactive property. To add dynamic styles with variables with Vue.js, we can use the style prop.

How do I avoid inline styles in a Vue template?

To avoid inline styles while gaining the benefit (or necessity— e.g., user-defined colors within a data payload) of dynamic styling, use a <style> tag inside of the <template> (so that values can be inserted by Vue). Use a :root pseudo-class to contain the variables so that they are accessible across the CSS scope of the application.

Is it possible to add style tag in Vue JS template?

Or how to check it in JS.. Maybe i will found solution in that way. Yes, this is possible. Vue.js does not support style tags in templates, but you can get around this by using a component tag. Untested pseudocode: props: { color: String } computed: { style () { return `.myJSGeneratedStyle { color: $ {this.color} }`; } }

What do you like about Vue JS?

When I started using Vue.js as a front-end framework I immediately enjoyed the easy way I can set up and manage my components.


1 Answers

I faced the same problem. I have been trying to use a background color value from a database. I find out a good solution to add a background color value on inline CSS which value I set from database.

<img :src="/Imagesource.jpg" alt="" :style="{'background-color':Your_Variable_Name}"> 
like image 74
Didarul Alam Rahat Avatar answered Oct 02 '22 18:10

Didarul Alam Rahat