Utilize a dual-color gradient effect on separate words within the <li> element

I am attempting to display the fizz buzz function in an unordered list, with each word being a different color ('fizz'-- green, 'buzz'--blue) as shown here:

https://i.sstatic.net/Yvdal.jpg

I have successfully displayed "fizz" and "buzz" in their respective colors individually, but when it comes to displaying the "fizzbuzz" line, the entire background of the <li> is divided between green and blue instead of only coloring the individual words.

Below is the css selector responsible for "fizzbuzz":


li:nth-child(3n + 0):nth-child(5n + 0) {
  background-image: linear-gradient(to right, green 50%, blue 50%);
  background-clip: text;
  text-fill-color: transparent;
}

I attempted changing the display property on the parent <ul> to "inline", but that did not solve the issue:

ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  list-style-type: none;
  display: in-line;
  
}

I am trying to achieve this using only css without altering my html/js. Here's the code:

const unorderedList = Array.from(document.querySelector("ul").children);

function fizzbuzz(elements) {
  for (var i = 0; i < elements.length; i++) {
    var result = "";
    var line = i + 1;
    if (line % 3 === 0) result += "Fizz";        
    if (line % 5 === 0) result += "Buzz";
    if (result.length ===0) result = line;
    elements[i].innerText = result;
    }
}

fizzbuzz(unorderedList)
ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  list-style-type: none;
  display: in-line;
  
}

li:nth-child(3n + 0) {
  color: green;
}

li:nth-child(5n + 0) {
  color: blue;
}

li:nth-child(3n + 0):nth-child(5n + 0) {
  background-image: linear-gradient(to right, green 50%, blue 50%);
  background-clip: text;
  text-fill-color: transparent;
}
}
<html lang="en">
<head>
  <title>FizzBuzz</title>
</head>
<body>
  <ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
  
</body>
</html>

Any assistance would be greatly appreciated.

Answer №1

Try adding the following CSS rule: width:fit-content;

const unorderedList = Array.from(document.querySelector("ul").children);

function fizzbuzz(elements) {
  for (var i = 0; i < elements.length; i++) {
    var result = "";
    var line = i + 1;
    if (line % 3 === 0) result += "Fizz";        
    if (line % 5 === 0) result += "Buzz";
    if (result.length ===0) result = line;
    elements[i].innerText = result;
    }
}

fizzbuzz(unorderedList)
ul {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  list-style-type: none;
  display: in-line;
  
}

li:nth-child(3n + 0) {
  color: green;
}

li:nth-child(5n + 0) {
  color: blue;
}

li:nth-child(3n + 0):nth-child(5n + 0) {
  background-image: linear-gradient(to right, green 50%, blue 50%);
  background-clip: text;
  -webkit-background-clip: text;
  color:transparent;
  width:fit-content;
}
<html lang="en">
<head>
  <title>FizzBuzz</title>
</head>
<body>
  <ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
  
</body>
</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

SCORM: moving between SCOs by clicking a button in the presentation

Currently, I am developing a website that allows users to create presentations. One of the website's features is the ability to export presentations in SCORM format (either 1.2 or 2004). This is my first time working with SCORM and I am currently impl ...

Learn how to move a div inside another div using drag and drop, ensuring that the entire element moves along with it

I am trying to implement jQueryUI for my project. I have a list of components that I want to drag and drop into a grid system, similar to a designer tool. I am using the following code to make the div draggable: $( "#Component1" ).draggable(); Then I dro ...

A guide on updating data with Vue-SweetAlert2 by passing user input

I'm currently working on creating a "create" button that will prompt a popup with a text input field for the user, similar to the image below. https://i.sstatic.net/eP7ki.png The expected behavior is that when the OK button is clicked, the name ente ...

Securing a namespace using passport js: A step-by-step guide

Imagine I have set up a specific route with the following namespace: app.use('/registered', userRoute); Recently, I wrote the following passportjs function: app.get('/logged/dashboard', function (req, res) { if (req.user === undefi ...

Is there a way to turn off TypeScript Inference for imported JavaScript Modules? (Or set it to always infer type as any)

As I attempt to utilize a JS module within a TypeScript File, I encounter errors due to the absence of type declarations in the JS module. The root cause lies in a default argument within the imported JS function that TypeScript erroneously interprets as ...

Challenges I'm Facing with my Vue.js API Integration using axios

I am currently working on a bookstore application using Vue.js. The book data is stored in this API . However, I am encountering issues displaying the information on my HTML page with the provided function and I am struggling to identify the root cause: ...

express-validator: bypass additional validation in a user-defined validator

Utilizing the express-validator package for validating my request data. As per the documentation, we need to declare them in this manner: app.use(expressValidator({ customValidators: { isArray: function(value) { return Array.isArray(value); ...

Is there a way to create a timed fadeIn and fadeOut effect for a div using HTML and CSS?

I have a single div that initially displays text, and I want it to fade out after a specific time so that another div will then fade in. However, my attempt at coding this transition is not producing the desired result: $(function() { $(".preloa ...

Is it possible for ng-model to verify its own value?

Recently, I've been experimenting with Angular and found myself facing a dilemma. Is it possible to apply a different CSS style based on whether the value of ng-model is greater than 0? `<span ng-model="users2" ng-hide="!myVar" ng-class="{'te ...

What is the best way to create two fields side by side within a single row?

I'm struggling to merge the data array of two different identity types into one row as a field. Here's the code I've written: import React from 'react' import {Form,Field,ErrorMessage,Formik} from 'formik' import * as yup ...

Receiving a NaN output rather than a numerical value

Experiencing what seems to be a straightforward syntax error, but I'm unable to pinpoint it. Controller: $scope.startCounter = 3; $scope.startTimeouter = function (number) { $scope.startCounter = number - 1; mytimeouter = $t ...

JavaScript code using jQuery's ajax method is sending a request to a PHP server, but

Attempting to utilize jQuery ajax for PHP call and JSON return. The test is quite simple, but only receiving an empty object in response. No PHP errors appearing in the LOG File. jqXHR is recognized as an object with 'alert', yet not displayin ...

Adjusting the width of a Material UI select/dropdown component

In my material-ui grid setup, each <Grid> contains a <Paper> element to define the grid's boundaries precisely. Inside every <Paper>, there is a custom <DropDown> component (a modified version of Material-UI's Select). I ...

JavaScript personalized video controls. Silence the video by manipulating the input range element

As a newcomer to javascript, I am working on creating custom video controls using js. One issue I am facing is with a span element that contains a mute/unmute icon. When I hover over this span element, a child element appears with an input range bar for ad ...

Guide on using a while-loop in selenium with python to continuously run until a specific element is no longer present on the page

I'm currently facing a challenge with creating a while-loop that will run until a specific element is no longer present on a website. My existing solution gets the job done, but I know there's room for improvement. I would love to hear suggestion ...

Styling Scrollbars in Datatables

I need assistance on styling a specific datatable scrollbar using Webkit. Below is the code snippet: id ::-webkit-scrollbar-track { background-color: transparent; border-radius: 5px; } id::-webkit-scrollbar { width: 13px; } ...

React-Leaflet continuously updates the map with the "MouseMove" event

I'm looking to implement a feature in my React app that displays the geographic coordinates as the mouse moves. However, I've noticed that using "Mousemove" causes the map to be continually redrawn with all objects each time, resulting in poor pe ...

jQuery struggles to properly interpret JSON data retrieved from a PHP file

I have successfully encoded WordPress data as JSON for a store locator page, however, I am encountering an issue where the page fails to parse the returned JSON. Even though the file is present and it indeed returns the correct JSON data, I receive a 404 N ...

Middleware that handles form post requests quickly became overwhelmed and resulted in a RangeError

Currently, I am working with a form using ejs templates to collect data. When accessing the "/" route, my application renders the "main" view and utilizes a middleware to handle the form data. However, I encountered an error stating "RangeError: Maximum ca ...

The integration of AngularJS with Bootstrap 3 accordion seems to encounter issues when included through ng-view

Here's the issue: When using the accordion in a view loaded with the ng-view directive, the accordion title clicks stop working correctly. Check out this demo for an example However, when the accordion is used directly on the page without ng-view, i ...