Could it be true that adding a string with ${class} to Tailwind does not function properly?

Some time ago, I inquired about a way to avoid repeating the same prefix in TailwindCSS like `hover:` multiple times here.

Someone was able to solve the issue using javascript, which seems to be working perfectly fine for now.

However, a commenter mentioned that using `${className}` is not ideal.


The specific line in question is:

element.classList.add(`${pseudo}:${className}`);

// example output: "hover:bg-blue-500"

At this juncture, I am uncertain whether it is correct or not, as ultimately it will simply add a string in the developer tools.

JavaScript will automatically translate it into browser-friendly code.


Previously:

<div class="text-white">

After:


// ✅ This is what I obtain
<div class="text-white hover:bg-blue-500">

// This could be what the individual is concerned about happening
// Or it may be an unknown bug he referenced
<div class="text-white ${pseudo}:${className}">

As you can see, the browser correctly adds it.

I am unsure where the script might be incorrect, so I intend to rectify it to prevent any bugs in the future.


Currently, I am solely utilizing HTML and CSS. Perhaps the bug might only occur in React and frameworks where you are unable to use classList.add() but need to directly input the class in the return?

If anyone knowledgeable and experienced knows about the bug being mentioned, please inform me.

(And also, if it is something not worth worrying about, kindly let me know so I can implement it across all my projects.)

Answer №1

Discover more information here

An important point to note about Tailwind is that it only recognizes classes that are complete and unbroken strings in your source files.

If you try to use string interpolation or combine partial class names, Tailwind will not be able to identify them and thus won't generate the associated CSS:

Therefore, simply creating a string for classes doesn't suffice.

Tailwind creates a CSS file containing only the classes used in your codebase. If you utilize a template string, these classes will go unrecognized and will not appear in the final CSS file.

To optimize file size, Tailwind does not automatically include all possible classes.

P.S.

A drawback of using template strings is that it may seem like they are working because you have included the class elsewhere in your code.

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

The functionality of the Bootstrap carousel may be compromised when initialized through JavaScript

Why isn't the Bootstrap carousel working in the example below? It starts working when I paste it into .content <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script sr ...

Simple trick to transfer an object or variable value from an HTML script tag to an angular controller

function displayMessage(msg) { console.log("message type is" + msg.type); var reply = document.getElementById('reply'); var para = document.createElement('p'); para.style.wordWrap = 'break-word'; para.appen ...

Using jQuery to insert a new string into the .css file

I'm currently working on a jQuery function to make my font size responsive to changes in width. While I am aware of other options like Media Query, I prefer a solution that offers smoother transitions. Using vw or vh units is not the approach I want t ...

A combination of Webpack CSS modules with Angular templates

I am currently trying to integrate webpack CSS modules into my angular/ionic project. I'm wondering if it's possible for the CSS module class definitions to be correctly appended to an external template instead of an inline template. When I embe ...

"Learn how to showcase a picture in full-screen mode when the webpage is opened

I recently came across a script on Stack Overflow that allows me to select an image at random from an array. The script can be found here: Script to display an image selected at random from an array on page load However, I want to take this concept furthe ...

Spread the picture on social media using Progressive Web App

I am currently working on a Nuxt PWA where I have implemented a function to convert HTML to Canvas using a specific package. The output generated is in base 64 format. My goal now is to find a way to easily share this image through various platforms such a ...

Creating clickable phone numbers in JSON elements for browser display to enable direct calling action

There is a Phone.json file with the following contents: { "Call": "Hi, Please contact the custom care at (119)239-9999 for further discussions" }, { "Call": " For emergency dial 911 as soon as possible" } The goal is to dis ...

Navigating drop down lists using selenium: A step-by-step guide

I have been scouring various websites, including this one, for solutions but so far I haven't had any luck. The website I'm navigating with selenium is coded in HTML4, although I'm not sure if that's relevant, I felt it was worth mentio ...

Embed script tags into static HTML files served by a Node.js server

Looking to set up a Node server where users can request multiple pages from a static folder, and have the server inject a custom tag before serving them. Has anyone had success doing this? I've tried with http-proxy without success - not sure if a pro ...

What is the technique for applying an image or texture to a specific section of a geometry in three.js?

In my three.js project, I successfully generated a sphere using SphereGeometry and applied a static color using MeshPhongMaterial. Now, I am looking to overlay an image onto the sphere in a specific area rather than wrapping it entirely around the object ...

Resizing tables dynamically using HTML and JavaScript

I am attempting to reproduce the functionality demonstrated in this example When dealing with a large table, I want it to resize to display 10 entries and paginate them accordingly. The only thing I require is the pagination feature without the search bar ...

Ways to ensure that a div, header, container, and other elements completely cover the viewport

Currently, I am attempting to create a header that spans the entire viewport regardless of screen size. This header will be split horizontally into 3 equal sections, each occupying one third of the header space. I experimented with setting the header hei ...

How can you create a personalized sorting order using odata?

Within my AngularApp, I retrieve data from a WebAPI using OData queries. All the retrieved results contain both a date and an integer status code. The status codes range from 1 to 4. I am aiming to organize my results in such a way that all items with a ...

What purpose do the curly brackets serve when importing a module?

Consider the following code snippet: const {set} = require("lodash"); Can you explain the purpose of using {} in this code? ...

Arranging Bootstrap panels in a stacked layout following the row

Currently, I am in the process of constructing a web dashboard for my application. This dashboard is designed to dynamically load various modules and place them within a Bootstrap container. These modules have varying heights and will stack vertically when ...

Displaying additional content with the load more button in JQuery

My script below: Main page: jQuery code <script type="text/javascript"> $(document).ready(function(){ $("#loadmorebutton").click(function (){ $('#loadmorebutton').html('<img src="ajax-loader.gif ...

Can MUI 5 replicate the Emotions 'css' function through analog?

From what I've learned, MUI 5 utilizes Emotion for its styling framework. Emotion offers a convenient helper function called css, which generates a string - a class name that can be used as a value for the className property or any other relevant prop ...

Removing the rubber band scrolling feature on Mac computers

While I am pleased with the overall look of my website, I have noticed that users with trackpads on Macs experience distortion when accessing it. I am seeking assistance in addressing this issue, particularly in disabling or minimizing the impact of trackp ...

The clash between Angular's ng-if directive and Bootstrap's popover feature causing unexpected

<div class="form-group" ng-if="firstname"> <label>First Name</label> <input readonly type="text" class="form-control" id="first_name" ng-model="firstname" placeholder="First Name"> <a href="" data-toggle="popover" dat ...

Iterate through a JavaScript object while simultaneously executing asynchronous calls

My goal is to use the THREE.XHRLoader to load multiple files, then add the key of each loaded object along with the file to another object called loadedFiles. The issue I'm facing is that when trying to retrieve the key for each loaded object, it alwa ...