Implementing CSS styles using a JavaScript variable

I'm facing a unique challenge right now. My JavaScript variable contains CSS rules like this:

         "floating_icons": {
                    "main_color": "",
                    "phone_color": "",
                    "mail_color": "",
                    "whatsapp_color": ""
                }
            },
            "style_css": ".pad-t { padding: 0 20px 0 20px;} .grey-t { color: rgba(127.765849375934, 127.765849375934, 127.765849375934, 0.217430264}"
        }
    },
    "entity": {
        "data": {
            "data": { 

I'm trying to apply 'style_css' in my VueJS application but haven't found the right solution yet. Can anyone provide some help with this?

Answer №1

Implementing Inline Styles tutorial for Vue.js version 2

The syntax for binding styles with v-bind:style is simple - it resembles CSS, but in the form of a JavaScript object. You can use camelCase or kebab-case (with quotes) for CSS property names:

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

data: {
  activeColor: 'blue',
  fontSize: 25
}

It's recommended to bind directly to a style object to keep the template clean:

<div v-bind:style="styleObject"></div>

data: {
  styleObject: {
    color: 'blue',
    fontSize: '15px'
  }
}

The object syntax is commonly used with computed properties that return objects.

Using Array Syntax The array syntax for v-bind:style lets you apply multiple style objects to the same element:

<div v-bind:style="[baseStyles, overridingStyles]"></div>

To explore more on this topic, refer to https://v2.vuejs.org/v2/guide/class-and-style.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing a Jquery check based on a checkbox

Hey, I'm having an issue with a condition. When I uncheck the checkbox, it doesn't uncheck. I've tried to make a block display, but the JavaScript isn't working. I attempted to add: document.getElementById("Reload").style.display = "b ...

A more effective method than utilizing the append function in a contenteditable div

In my contenteditable div, whenever a user hits the enter key after typing some content, two break tags are automatically created. line 1 </br> </br> If the user then manually types something else, it looks like this: line 1 </br> line ...

Issue with React Routes only occurring in the production website

I'm encountering an issue on my personal website that only occurs in production, but not in my local environment. Here's the situation: I have set up the routes as follows const Routes = () => ( <Router> <Route exact path=&quo ...

Using `window.location.href` will terminate any pending asynchronous calls to the API

Before all async calls to the API are completed, window.location.href is triggered when the code runs. Setting a breakpoint on the location resolves the issue. How can I ensure that all calls are finished before invoking window.location.href? Code: const ...

Transferring variables from the existing scope to a compiled directive

I'm having trouble passing an object from the current scope to a directive that I added using the $compile service. While I can successfully pass a string to the child directive, I'm unable to pass the actual object. Take a look at this fiddle ...

Converting a space-separated string into tags-input (treating each segment as a separate tag) in AngularJS

I need assistance with pasting a copied string into a tags-input element, like the example below: "[email protected] [email protected] [email protected] [email protected]": https://i.sstatic.net/0aFD5.jpg When I click outside of the tags-input element, I ...

Adding 7 days to a JavaScript date

Can you spot the bug in this script? I noticed that when I set my clock to 29/04/2011, it displays 36/4/2011 in the week input field! The correct date should actually be 6/5/2011 var d = new Date(); var curr_date = d.getDate(); var tomo_date = d.getDate( ...

Ways to position text over an image and ensure that it adapts to different screen sizes

Hey there, I'm new to HTML and CSS and I've been trying to create a responsive landing page. I managed to display text on an image by adjusting the image's style to relative and the text's style to absolute. However, when I resize the s ...

Implementing a default property for a composite component

I am currently utilizing a Modal component from a library that is built on top of ReactModal and offers the following API: <Modal {...props} /> // all props are passed to ReactModal <Modal.Header /> // custom header styles applied <Moda ...

Guide on navigating to a specific page with ngx-bootstrap pagination

Is there a way to navigate to a specific page using ngx-bootstrap pagination by entering the page number into an input field? Check out this code snippet: ***Template:*** <div class="row"> <div class="col-xs-12 col-12"> ...

Passing key value pairs with jQuery's get method

For my project, I am developing a universal function that interacts with different types of data and executes ajax get requests based on the data. At times, I need to make a get request like this: {option1: 'delete', id: idToRemove}, and other ...

Positioning of a paper-dialog in Polymer

I'm currently utilizing polymer's paper-dialog element in my application. While the dialog is always centered by default, I am looking to establish a minimum distance between the dialog and the right side of the window. This way, as the window re ...

Retrieved information from Firestore to set as the initial value for my useState hook, but I keep receiving an undefined value

I'm facing an issue where I want to use the fetched data from Firestore as the initial value of my state using useState, but it always returns undefined. This is because when updating a user profile, I need to know which property has been edited or up ...

Order list based on matching keyword in data attribute

I am currently working with a lengthy list that utilizes basic JavaScript search functionality. The search function uses regex to check for specific conditions and hides items that do not meet the criteria. I am attempting to organize the list in such a wa ...

AngularJS encountered an unhandled syntax error

My current approach involves utilizing the code below to display data fetched from Parse API into a table using AngularJS and Bootstrap. However, the JavaScript section where I have defined the controller doesn't seem to be running as expected. Below ...

Leverage a JavaScript function to manipulate the behavior of the browser's back button

Is there a way to trigger the Javascript function "backPrev(-1)" when the back button of the browser is clicked? Appreciate any help, thank you. ...

Failure of $.post to activate the function

I'm really struggling to understand why the alert or console.log functions are not being triggered in this snippet of code: $.post("http://localhost:8080/mail", jsonObject, function(data) { ...

An error occurred while attempting to execute the 'load' method on tabs that have not been initialized yet

Trying to use jquery-ui .tabs methods to refresh/load bootstrap tabs. $('#myTab a').on('click', function () { console.log("testing"); $("#myTab").tabs('load') }) Encountering this error message: Cannot ...

Having Difficulty Applying a Background Color to a Class in Bulk

I am working on a unique HTML canvas for pixel art, which is created using a table made up of various divs all sharing the "pixel" class. I want to add a clear button that can reset the entire canvas, but changing the background color of each individual di ...

Creating a JavaScript function that generates a heading element

Hey there, I'm just starting out and could really use some guidance. I want to be able to have a function called addHeading() that takes the heading type selected from a drop-down menu and the text inputted in a form field, then creates and displays t ...