Dynamic external image background url not working with Tailwind
Answered
Rojoss posted this in #help-forum
RojossOP
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.
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.
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.
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
https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
<div style={{backgroundImage: `url(${post.image})`}}></div>https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
1 Reply
RojossOP
Found a solution on Stackoverflow
https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
<div style={{backgroundImage: `url(${post.image})`}}></div>https://stackoverflow.com/questions/70805041/background-image-in-tailwindcss-using-dynamic-url-react-js
Answer