11

I've just recently picked up React and Tailwind for a project, and I am still very much a beginner. I wanted to make an element have a background image as a custom class variable, something like this:

<div className="bg-[url(`https://example.com/${variable}.png`)]"></div>

But as Tailwind purges classes, would this somehow be possible? I hope I'm not missing anything, but it doesn't seem doable to me right now

7
  • I think you have a closing square bracket too much? className="bg-[url(https://example.com/${variable}.png)]" Commented Jan 11, 2022 at 19:53
  • That's on me from copying it wrongly. It still doesn't work on className="bg-[url('https://example.com/${variable}.png')]" Commented Jan 11, 2022 at 20:05
  • what does not work? any errors? Commented Jan 11, 2022 at 20:06
  • No errors, inspecting the site when it's all loaded just has the bg-[url('https://example.com/${variable}.png')] class on that element Commented Jan 11, 2022 at 20:13
  • 7
    While my code generates a class that looks correct, tailwind won't generate css for it. I have come to the conclusion that this isn't possible. Tailwind doesn't generate any CSS in runtime, so there's no way it could create all the possible classes that string-interpolation would entail. Tailwind just analyzes the strings during compilation and creates the appropriate classes for that. You should probably use <div style={`background: url(https://example.com/${variable}.png`}> instead Commented Jan 11, 2022 at 21:10

2 Answers 2

3

Tailwind has a feature called "safelisting" which may provide you with the functionality you need – see here in the docs.

That said:

1 - There's nothing wrong with using both className and style props on the same element – you can have e.g. <div className="w-full" style={`background: url(https://example.com/${variable}.png`}>

2 – Depending on what flavor of React you are using, your might be better off using a component like Next.js's Image to optimize your images instead of setting background via CSS. Below is how I do it:

import Image from "next/image";

export default function MyBackground(props) {
  return (
    <div
      className="z-0 absolute inset-0 w-full h-full flex justify-center items-center"
    >
      <Image
        placeholder="blur"
        alt={props.alt}
        src={props.src}
        className="h-full w-full object-cover"
      />
    </div>
  );
}


Sign up to request clarification or add additional context in comments.

Comments

-2

You can define a different variable for this. In that way, you can manipulate the value accordingly and use that variable to set the styling properties of that element. The code will look something like this:

const YourComponent = () => {

    //the variable named variable will hold the dynamic value as per your need
    let elementStyle = "bg-[url(`https://example.com/" + variable + ".png";

    return (<>
                <div className={elementStyle}"></div>
            </>
    );
} 

1 Comment

I think it's better to use a css variable rather than make js variable. As I know, tailwind does not work with dynamically generated classnames

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.