What could be causing the issue of not being able to set the background color in tailwindcss?

I need help setting dynamic CSS, can someone please guide me on the code snippet where I am supposed to set it?

export default function DynamicCard({
        title,
        background = "#2B5CEA",
        icon,
        href,
    }: DynamicCardProps) {
        return (
            <Link
                href={href}
                rel="noreferrer"
                target="_blank"
                className={`hover:bg-[${background}] p-2 rounded-xl text-gray-400 bg-gray-800 hover:shadow-xl transition-all flex items-center`}
            >
                <span
                    className="inline-flex items-center justify-center w-8 h-8 md:w-11 md:h-11 bg-gray-800 rounded-lg mr-3 shrink-0"
                    style={{ backgroundColor: background }}
                >
                    <span className="relative w-4 h-4 md:w-6 md:h-6">
                        <Image
                            src={icon}
                            fill
                            alt={title}
                        />
                    </span>
                </span>

                <h4 className="text-sm md:text-base lg:text-lg font-bold text-gray-200 truncate">
                    {title}
                </h4>
            </Link>
        );
    }

    

and here is how I am sending props:

<DynamicCard
                title="VS Code"
                background="#0065A9"
                icon="/svg/stacks/vscode.svg"
                href="https://code.visualstudio.com/"
            />
            <DynamicCard
                title="Github"
                background="#070A52"
                icon="/svg/stacks/github.svg"
                href="https://hyper.is/"
            />
    

The issue I'm facing is that the background color works fine in the VS Code card but not in the Github card. Can anyone help understand why this is happening?

Answer №1

The problem could arise from how you're supplying the background prop to the ThingCard component. You have passed background as a string with a hash symbol in front, such as background="#0065A9".

export default function ThingCard({
  title,
  background = "2B5CEA",
  icon,
  href,
}: ThingCardProps) {
  return (
    <Link
      href={href}
      rel="noreferrer"
      target="_blank"
      className="hover:bg-gray-700 p-2 rounded-xl text-gray-400 bg-gray-800 hover:shadow-xl transition-all flex items-center"
      style={{ backgroundColor: `#${background}` }}
    >
      <span
        className="inline-flex items-center justify-center w-8 h-8 md:w-11 md:h-11 bg-gray-800 rounded-lg mr-3 shrink-0"
        style={{ backgroundColor: `#${background}` }}
      >
        <span className="relative w-4 h-4 md:w-6 md:h-6">
          <Image src={icon} fill alt={title} />
        </span>
      </span>

      <h4 className="text-sm md:text-base lg:text-lg font-bold text-gray-200 truncate">
        {title}
      </h4>
    </Link>
  );
}

Now we are using #${background} to define the backgroundColor property in both the className string and the style object. This guarantees that the background prop is applied consistently and without any additional characters.

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

Tips for removing a row from a DataGrid column with the click of a button

I am facing a challenge with my data table that contains users. I am trying to implement a delete button for each row, but it seems like the traditional React approach may not work in this situation. Currently, I am utilizing the DataGrid component in the ...

The integration of express and cors() is malfunctioning

Currently, I am developing a React application and facing an issue while trying to make an API call to https://itunes.apple.com/search?term=jack+johnson In my project, there is a helper file named requestHelper.js with the following content : import &apo ...

Issue with React Hot Toast not displaying properly due to being positioned behind the <dialog>

The Challenge of Toast Notifications Visibility with <dialog> Element tl;dr When utilizing the native dialog.showModal() function, the <dialog> element appears to consistently remain on top, which causes toast notifications to be obscured by ...

How to fix the issue of a static background image in Bootstrap that does not

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--Bootstrap--& ...

Using React.js - What is the process for submitting a form automatically when the page loads?

Upon loading the page, I have a form that needs to be displayed. Here is the code snippet: <form name="myform" method="POST" className={classes.root} target="mydiv" action="https://example.com/submit" onLoad= ...

What is the best method for incorporating various fonts in CSS?

Despite being new to the world of coding, I have been exploring ways to enhance the typography on my website. A tutorial I found on W3Schools touches on the usage of CSS for incorporating different font styles across my webpages. Among other things, it exp ...

What is the approach of next/image for optimizing images in terms of file sizes? Is it necessary to establish a file size restriction for the images that will be uploaded to my web application?

According to the documentation: https://nextjs.org/docs/basic-features/image-optimization https://nextjs.org/docs/api-reference/next/image The Automatic Image Optimization feature provided by Next.js allows images to be resized, optimized, and served i ...

Is there a way to make sure that the antd datepicker response is sent to the backend only after I click on the submit button?

After receiving a response from the antd datepicker and successfully sending it to the backend, I encountered an issue. The response is automatically sent to the backend as soon as a date is selected. However, I want the request to be sent only after click ...

Generating a file directory using the current operating system for Python [importing css files]

When trying to link CSS in my HTML while running a Django server, I encountered an issue. The problem arises when using backslashes (\) on Windows and forward slashes (/) on Linux. Is there a recommended way to adjust the path in base.html according ...

Using React with Typescript to iterate through a list of countries in order to generate a dropdown selection menu

In the file countries.tsx, I have stored a list of countries along with their ISO codes... // countries.tsx export const COUNTRIES: { [x: string]: { [y: string]: string } } = { en: { AX: 'Aaland Islands', AF: 'Afghanistan', ...

Begin expanding new circles in jQuery from the exact location where the previous ones ended

A captivating circle animation unfolds before your eyes, featuring dark blue circles that materialize at random points and gradually expand. As these expanding circles cover more than half of the screen, their color shifts to a lighter shade of blue. Exper ...

What are your preferred HTMLPurifier configurations for sanitizing CSS styles?

I'm currently in the process of developing an application that allows users to input their own custom CSS for their profiles, similar to platforms like MySpace, Friendster, and Blogger. However, I'm struggling to find an effective method to prot ...

I am noticing multiple quantities of the same item being added to my shopping

Recently, I encountered a problem with my online shop that seems to be related to the Javascript. The issue arises when I add an item to my shopping cart from this particular page: . Initially, when I click 'Add to Cart,' the item is correctly ad ...

Different methods to display my view when the initial value is late

I am facing an issue with a component that manages a boolean value in its state and has a button to toggle this value. The boolean value is obtained from an object created in a parent component. The button gets rendered when the object is created, but th ...

What is the best way to integrate Stripe's 3D secure feature for both one-time payments and recurring subscriptions within a unified pop-up

I'm currently working on integrating 3D secure Stripe payment for a unique "bundle" that requires both a one-time payment and a yearly recurring payment. After some research, I discovered that the solution is to use stripe.payment_intent for the one- ...

What is the best method to restrict the size of a div to match the dimensions of the

In my webpage, I have a menubar (div) that contains bookmarks. However, when too many bookmarks are added, the menu becomes too wide for the page size I prefer (1280, 720) and becomes scrollable, causing some bookmarks to be out of view. My goal is to ens ...

collecting user input in React.js

After doing some research on React.js from this website, I stumbled upon a piece of code that left me puzzled. As far as I can tell, the checkbox for isGoing will be pre-filled as true (checked) and the numberOfGuests will be set to 2. However, I found m ...

Retrieve information by utilizing query string parameters in Next.js

When using my getInitialProps function, I make two requests to fetch data. Now, I want to add a condition to make an additional request if the query params key is for a legal person. For example, if the URL is https://www.example.com.br/?legal-person, I n ...

Creating a dropdown menu with HTML and CSS is causing me a major migraine

<div class="fbtop"> <img src="https://static.solidshops.com/1441/files/Logo-site.png" title="Pieke Wieke" alt="Pieke Wieke"> <h2 class="title">Handcrafted with love</h2> <ul class="dropdown"> <li> <a hre ...

AngularJS allows for the ng-view directive to showcase a dynamic footer element that is positioned outside of its

I am relatively new to using angular.js and I could use some guidance on the best approach to implementing the functionality described in this scenario. My HTML structure consists of a header, sidebar, and content area provided by <div class="container- ...