What are some techniques for styling individual letters within a string?

If I were to receive a summary of the latest matches for a specific team from an API, let's consider how it could be presented.

For example, the response might look like this: LLWWWWLWDWDWLW

I would like this information to be displayed in a div with a unique style.

The letter L should have a background color of red (in a square shape), while the letter W should have a green background. Each character should be slightly separated from one another.

Users should also have the option to switch to a different team to view their recent matches.

This project is being developed using Vue.js.

Answer №1

Here is a simple solution using the v-for directive:

const mapColors = new Map([['W', 'green'], ['L', 'red'], ['D', 'yellow'])
const app = Vue.createApp({
  data() {
    return {
      matches: [{team: 'team 1', games: 'LLWWWWLWDWDWLW'}, {team: 'team 2', games: 'WWWWLWDLLWWWDW'}]
    };
  },
  methods: {
    setColor(c) {
      return mapColors.get(c)
    }
  }
})
app.mount('#demo')
.green {
  background: limegreen;
}
.red {
  background: lightcoral;
}
.yellow {
  background: gold;
}
.games {
  width: 1.2em;
  display: inline-block;
  text-align: center;
  margin: .1em;
}
.match {
  margin-right: .5em;
  font-weight: 600;
}
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
  <div v-for="(match, idx) in matches" :key="idx">
    <span class="match">{{ match.team }}</span>
    <span v-for="(game, i) in match.games" :key="i">
      <span class="games" :class="setColor(game)">
        {{ game }}
      </span>
    </span>
  </div>
</div>

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

Angular collapsible panels and floating pop-up windows

I have implemented the Angular UI Bootstrap accordion and popover functionality from http://angular-ui.github.io/bootstrap/. However, I am facing an issue where the popover is only displaying inside the accordion and creating an overlay. Here is the HTML ...

Struggle JS/jQuery conflict

Working on implementing a "Leaderboard" system, I encountered a conflict between two jQuery scripts. These scripts were not written by me but by an old acquaintance, and as I am not well-versed in js/jQuery, I am struggling to resolve the conflict. One of ...

What causes images to be omitted from a PDF file when using mywindow.print()?

Here is the scenario I am dealing with: On a particular webpage, there is a print button. The page contains various information, including receipts. When the user clicks on "print", I want only the receipts to be printed: https://i.sstatic.net/WobKK.png ...

What is the method for identifying modules that rely on a specific module?

Is it possible to view all dependencies modules of a specific module, similar to this: npm-remote-ls <module-name> But how can we see the modules that depend on a particular module? Any help would be appreciated. Thank you. ...

Ensure that the second y-axis in charts.js consistently shows the 100% tick

I have implemented a scatter chart using recharts and it currently looks like this: The right-y-axis in my chart represents the percentage and is showing the correct values as desired. However, I am looking to make a modification so that the 100% mark is ...

Positioning text too far to the left causes it to become disorganized and difficult

I am looking to achieve the following result: LEFT THING, not fixed here comes a text that spans 100% width of this "column" width (content itself but my issue is that when it gets too long, this column drops below determines that) the left ...

Tips on handling jsonp responses in CakePHP without using the .json extension

When working with CakePHP, the framework determines the data type to return by either checking for the presence of the .json extension in the URL or examining the Accepts HTTP header. It becomes a bit trickier when dealing with JSONP, as it doesn't a ...

Unable to access request body data from API - value not defined

It's been a couple of hours and I'm still stuck on this issue >.< I'm having trouble accessing the request body for the user API. While I can retrieve the response 'hi' using res.send (as shown in the screenshot), I'm n ...

Tips for utilizing the find function with nested documents in MongoDB in conjunction with Next.js?

I have structured my data in such a way that it is obtained from localhost:3000/api/notes/[noteTitle]/note2 { "data": [ { "_id": "62ff418fa4adfb3a957a2847", "title": "first-note2" ...

Running time assessment for JavaScript executions

My current focus is on the execution time of JavaScript programs. Upon running them with nodejs/v8, I have noticed that the time command in the shell produces inconsistent results for execution times. Is there a method to sandbox the execution of these pr ...

Struggling to send a POST request to my API using AJAX and jQuery

Struggling with POST and GET requests in jQuery and javascript for my API. Managed to POST using curl request in git bash but unable to do so with JS/jQuery, also not populating on main HTML page. Below is the form code: <h2>My Lists:</h2> < ...

Removing a style attribute by adding a new class

Currently, I am in the process of designing a website prototype using Twitter Bootstrap. One issue that I have encountered is that when I use the hero-unit class, any color attributes specified in the h1 tags are ignored and only displayed in black. This ...

How can you prevent an element from overflowing when employing window.pageYOffset and using a switch case to alter the background color at specified pixel thresholds?

I want the <body> element's background-color to change every 500px as I scroll down. I've scoured the web for answers, but still can't figure out what's going wrong. Please review the code. However, Firefox Browser indicates that ...

Preventing CSS shapes from overlapping with the next DIV

I'm currently working on creating a V-shape using CSS. While I have successfully created the shape, I am facing an issue where I cannot get the shape to appear "above" another div with a background image when it is placed before it and made negative. ...

Display or conceal the grid template row for content that is empty or if the grid area is not displaying

display: grid; grid-template-columns: var(--side-bar-width) 1fr; grid-template-rows: 60px 1fr 90px; gap: 0px 0px; grid-template-areas: "Sidebar Header" "Sidebar Body" "Player Player"; I ...

What is preventing this.form.post(URI) from accessing all API resource routes, beyond just one specific route?

In my API Resource Routes setup, I have multiple Controllers configured, but only one ('project' => 'API\ProjectController') seems to be accessible. Even though I have used the correct URIs in the route list for the other API Re ...

Is there a way to show a progress bar that functions as the background for a table row

My table is structured as follows: th1 th2 th3 th4 th5 th6 td1 td2 td3 td4 td5 td6 td1 td2 td3 td4 td5 td6 I am looking to incorporate a feature where new rows are dynamically added using a form that triggers ...

Creating a custom JavaScript clock based on stored database values

I am looking to create an analog clock using JavaScript. I have both working hours and off hours stored in a database. My goal is to display working hours in one color and off hours in another color on the clock. How can I achieve this? The database prov ...

A guide on updating a boolean field in the database using ajax

Here is a piece of my html code: <form action="" method="post"> {% csrf_token %} {% if donate.is_taken == False %} <br> <button type="submit" class="btn" name="taken_or_not" ...

React Router is updating the URL without actually rendering the view

React router allows for changing the URL without refreshing the page. However, when manually typing in the URL and pressing ENTER, the routes work properly. Unfortunately, clicking on the navlink does not produce the same result. App.js import './App ...