Use an array of styles within v-bind:style

In my Vue project, I am using a v-for loop to create spans and applying different styles. The current setup successfully applies one style using Bootstrap4's background-color:

<span v-for="(group, index) in item.primary" 
      :key="index" 
      :class="'badge'" 
      :style="{`background-color: ${tagsText[group].color}`">
      {{ group }}
</span>

<script>
    export default {
     data () {
      return {
        tagsText: {
         calling_spirits: {
           color: '#800000',
           text: 'light'
           }
         }};
     }
    };
</script>

Now, I want to add a second styling for text colour but I'm having trouble making it work. When I change the span style to the following, there are no errors or any visible styling changes:

:style="`background-color: ${tagsText[group].color}`, `text-light`">

The same issue occurs with:

:style="`background-color: ${tagsText[group].color}`, `text-${tagsText[group].text}`">

Answer №1

If you're looking for further information on how to utilize object and array syntaxes, I suggest checking out https://v2.vuejs.org/v2/guide/class-and-style.html. As mentioned by Shekhar Chikara, it seems like you may be combining inline styles with classes incorrectly. Your template structure should resemble the following:

<span
  v-for="(group, index) in item.primary" 
  :key="index" 
  :class="`badge text-${tagsText[group].text}`"
  :style="{ backgroundColor: tagsText[group].color }">
  {{ group }}
</span>

In this case, text-light denotes a CSS class that should be added to the class attribute of the span element.

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

How can I retrieve values of selected checkboxes using the Express Data API?

I have a scenario where I need to retrieve data from input checkboxes only when the checkbox for my express post function is selected. There are 3 checkboxes with values of 1000, 2000, and 3000 as follows: <input type="checkbox" name=" ...

Invoking a URL handler with JavaScript

Recently, I developed a Chrome extension that successfully identifies phone numbers in our CRM. I have tested it by using console.log and the results display correctly. Furthermore, we utilize Jitsi softphone which is set up with a URL handler. This means ...

Enhancing CSS menus with Jquery or JavaScript to include a delay

As someone who is just starting to learn about jquery and JavaScript, I am seeking help in adding some code to a css menu. My goal is to create a delay between options so that users have enough time to select an option without it closing prematurely. After ...

Cross-platform mobile browsers typically have scrollbars in their scrollable divs

I have successfully implemented scrollable div's on a page designed for tablets running Android 3.2+, BlackBerry Playbook, and iOS5+ (except iPad 1). However, I would like to add scrollbars to improve the user experience. For iOS5, I can use the suppo ...

How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include: .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } However, the current implementation caus ...

Iterate over a series of button elements within a loop in order to dynamically control corresponding input fields

Having trouble looping through an array with JavaScript/jQuery to display data and remove contents with button clicks. Original Code in Question HTML <textarea rows="4" cols="40" id="sds">Apple Banana Grape Orange</textarea> <input type=" ...

JQuery immediately triggers the firing of $.when

I've been attempting to group together AJAX requests in order to trigger an event once all of them have finished: this.xhrs.push($.ajax({ type: "POST", url: this.options.firstUrl, data: this.options.data, datatype: 'json' })); this.xhrs.pus ...

What could be the reason for my https source being restricted from accessing the navigator Geolocation service?

While trying to retrieve a user's location using the code below: var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(BingMap.map); geoLocationProvider.getCurrentPosition( { showAccuracyCircle: false, timeout: 6000, successCa ...

The Colorful World of CSS Backgrounds

I've been searching for hours trying to track down the source of this strange greenish background color. I've combed through every single file and it's starting to drive me insane. Any ideas where this color could be coming from? I highly d ...

Converting Blob Data to JPG Image: A Step-by-Step Guide

Currently, I am utilizing croper.js to crop and save an image. However, the function that processes the cropped blob to convert the image format is not functioning correctly. cropper.getCroppedCanvas().toBlob(function (blob) { var formData = ...

Creating the greatest possible number using elements from an array in JavaScript

Given the array arr = [1,3,34,44,4,45,6,76,9,98,23], my goal is to create the largest possible number from those values. The output should be 99876645444343231. I attempted this with a solution that works well for two-digit numbers, but struggles with lar ...

Using JavaScript to Transmit URL

Imagine I have a URL similar to this: http://localhost:8000/intranet/users/view?user_id=8823 All I aim to achieve is to extract the value from the URL using JavaScript, specifically the user_id (which is 8823 in this instance), and transmit it through an ...

Generating a .html file through the use of a Chrome extension

I am currently working on my first Chrome extension. At the moment, my code is capable of extracting elements from a webpage and generating HTML markup, which is then stored in a JavaScript string. Within my extension, there is a button that... $(".colum ...

Full-width sub menu within the menu

I'm trying to make a sub menu appear below my main menu that is the same width as the main menu, which has a fixed width. I want the sub menu to adjust its width based on the number of links it contains. Is this even possible? Here's the code I h ...

Implement multiple selection of parameters in React Material UI version 1.0 and handle the onChange

Currently, I am working on implementing React Material UI 1.0.0-beta.34 and encountering an issue with the Select component. My challenge lies in trying to include an extra parameter in the onChange event handler, yet it seems that only the event parameter ...

Is there a method in CSS animations that allows for endlessly repeating successive animations in a specified sequence?

While working with CSS animations, I encountered a challenge of making two animations occur successively and repeat infinitely without merging keyframes. Is there a way to achieve this using only CSS? If not, how can I accomplish it using JavaScript? I a ...

Hot reloading functionality ceases to function within Docker container after a period of time

Scenario In our development setup, we utilize Docker to containerize a Vue.js application and attach a volume with the source code for easy testing and maintenance. Docker Configuration FROM node:13.8-alpine RUN yarn install && \ apk ad ...

Remove movable element from react-dnd

I am working with react-dnd and I am trying to figure out how to remove a draggable object once it has been used. I modified the react-dnd multiple type example by adding a delete feature when an object is dropped. Here is the current behavior: 1. Drag a ...

Vuetify's autofocus feature is limited to functioning only when the modal is first opened

When attempting to utilize Vuetify's v-text-field with the autofocus attribute, I am facing an issue where it only works the first time. Once I close the dialog, the autofocus functionality no longer works. This is the code snippet I am trying to imp ...

I am looking to dynamically print countries from an array in my code based on the selected option

I'm in need of some simple code assistance. I want to display a list of countries that correspond to a selected letter, but I'm struggling to do so dynamically at the moment. For example, if I select the letter A from a dropdown menu, I want the ...