Next.js Discord

Discord Forum

Dynamic external image background url not working with Tailwind

Answered
Rojoss posted this in #help-forum
Open in Discord
Hey I'm trying to render a div with a external background image, for example an Imgur image.
When the image is in the browser cache it works but if it isn't then I always just get a blank div without background.
The css style also doesn't show up when I inspect the div.
const image = `url('${post.image}')`;
<div className={`bg-[${image}]`}></div>


When I inline the image url instead of using variable then it works consistently
And then the image is cached or something and the example above then also works consistently.
But if I never load the image this way then it never works.
<div className={`bg-[url('https://i.imgur.com/...')]`}></div>


Anyone has any clue how to get this working?
I want to use background styling like cover and center and Next Image doesn't allow me to do that.
Answered by Rojoss
Found a solution on Stackoverflow
<div style={{backgroundImage: `url(${post.image})`}}></div>

https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
View full answer

1 Reply

Found a solution on Stackoverflow
<div style={{backgroundImage: `url(${post.image})`}}></div>

https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
Answer