简体   繁体   English

如何在 CRA 应用程序中将背景图像 class 添加到顺风配置

[英]How to add background image class to tailwind config in CRA application

I am trying to use image as background using tailwind css in Create react app.我正在尝试在 Create React 应用程序中使用 tailwind css 将图像用作背景。 But can't figure out how to add background image class.但不知道如何添加背景图片 class。

The error:错误:

Module not found: Error: Can't resolve './src/images/pages/login.svg' in 'C:<some_project>\src'

what i tried:我试过的:

backgroundImage: {
      "login-image": "url('./src/images/pages/login.svg')",
},
// assumed it starts from src
backgroundImage: {
      "login-image": "url('/images/pages/login.svg')",
},
// also tried to place it into public page and include like
backgroundImage: {
      "login-image": "url('/images/pages/login.svg')",
},

Any help would be appreciated!任何帮助,将不胜感激! Thanks谢谢

This is how I got mine to work:这就是我如何让我的工作:

In the tailwind.config.js, I added my background to extend (make sure to put your appropriate image url):在 tailwind.config.js 中,我添加了要扩展的背景(确保输入适当的图片 url):

extend: {
  backgroundImage: {
    awlogo: "url('./images/aw-lg-logo.png')",
    awOpac: "url('./images/awopac.png')"
  },
},

Here is my App.js example:这是我的 App.js 示例:

import "./index.css";

function App() {
  return (
    <div className="grid place-items-center h-screen bg-blue">
      <img
        className="bg-no-repeat w-screen h-screen opacity-5 fixed overflow-hidden z-1 bg-cover bg-awOpac  "
        alt="aw logo opacity"
      />
    </div>
  );
}

where it says bg-awOpac that basically says:它说bg-awOpac基本上说:

.bg-awOpac {
    background-image: url('./images/awopac.png');
}

Make sure you are giving the image a height and width as well.确保您也为图像指定了高度和宽度。

I am using "tailwindcss": "^3.0.23"我正在使用"tailwindcss": "^3.0.23"

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

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