简体   繁体   English

使用动态 url (React.js) 在 tailwindcss 中的背景图像

[英]Background image in tailwindcss using dynamic url (React.js)

I have an image url fetched from an API and I want to display it as a background image.我有一个从 API 获取的图像 url,我想将其显示为背景图像。 Is there any way I can implement this with tailwindcss or should I use styles attribute?有什么方法可以用 tailwindcss 实现这个,还是应该使用 styles 属性?

I think the best solution is to do what you suggested and use a style attribute.我认为最好的解决方案是按照您的建议进行操作并使用样式属性。 Tailwind CSS doesn't really deal with dynamic data, but rather with class names to add predefined styles to your element. Tailwind CSS 并不真正处理动态数据,而是使用 class 名称来将预定义的 styles 添加到您的元素中。 The best you could without using the style attribute is to conditionally add/remove classNames from an element, but that would require you to know the image URL ahead of time, which defeats the purpose of getting it from an API.如果不使用样式属性,最好的办法是有条件地从元素中添加/删除类名,但这需要您提前知道图像 URL,这违背了从 API 获取它的目的。

I would do:我会做:

style={{backgroundImage: `url(${fetchedImgSrc})`}}

Edit: It looks like you can actually use Tailwind to do something similar to the code above as per https://tailwindcss.com/docs/background-image .编辑:看起来您实际上可以根据https://tailwindcss.com/docs/background-image使用 Tailwind 来执行类似于上述代码的操作。 The code looks like:代码如下所示:

<div class="bg-[url('/img/hero-pattern.svg')]">
  <!-- ... -->
</div>

I think I have found a solution other than simply using a style attribute.我想我已经找到了一个解决方案,而不是简单地使用样式属性。

<div
  style={{'var(--image-url)': fetchedUrl}} 
  className='bg-[image:var(--image-url)]'>
    <!-- ... -->
</div>

This works perfectly fine.这工作得很好。

Although it looks a bit tedious, it can be used to enable the background image on hover, focus, etc. In which the style attribute is incapable of.虽然看起来有点繁琐,但是可以用来启用hover上的背景图,焦点等,其中style属性是不能用的。

className='hover:bg-[image:var(--image-url)] focus:bg-[image:var(--image-url)] ...'

This application of custom properties can be used for implementing dynamic values with tailwind like colors fetched from an API.这种自定义属性的应用可用于实现具有顺风的动态值,例如从 API 获取的 colors。

<div
  style={{'var(--color)': fetchedColor}} 
  className='text-[color:var(--color)]'>
    <!-- ... -->
</div>

Tailwind Doc: https://tailwindcss.com/docs/background-image#arbitrary-values顺风文档: https://tailwindcss.com/docs/background-image#arbitrary-values

You could try something like below你可以试试下面的东西

From Doc example (HTML)来自文档示例 (HTML)

<div class="bg-[url('/img/hero-pattern.svg')]">
  <!-- ... -->
</div>

In JSX it would be something like(I might be getting the syntax wrong.)在 JSX 中会是这样的(我可能语法错误。)

<div className=`bg-[url('{DYNAMIC_IMG_PATH}')]`>
  <!-- ... -->
</div>

If you are having this same issue in 2022, then this is what helped me with tailwind version ^3.0.23.如果您在 2022 年遇到同样的问题,那么这就是顺风版本 ^3.0.23 帮助我的原因。 I just moved the image to the public folder and used the link in my tailwind.config.js like so backgroundImage: { 'cover-pic': "url('/public/img/cover_pic.jpg')" } where /public/ img is the directory where I placed the image within the public folder, yours might different.我只是将图像移动到公用文件夹并使用我的 tailwind.config.js 中的链接,就像这样backgroundImage: { 'cover-pic': "url('/public/img/cover_pic.jpg')" } where /public / img是我在公共文件夹中放置图像的目录,您的可能会有所不同。 and then called the css like so bg-cover-pic within my project, and that was all it took.然后在我的项目中调用 css 就像bg-cover-pic一样,这就是全部。

you can just use backgroundImage in Style你可以在 Style 中使用 backgroundImage

const bag2 = "https://via.placeholder.com/500"

<div 
    className = "someting"
    style={{backgroundImage: `url(${bag2})`}}

</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM