tailwind css 配置文件背景图片问题

tailwind css config file background image issue

我没有从 tailwind.Config.Js 获得背景图像输出。 Tailwind.Config.Js 完美地生成了背景图片,但是当我在浏览器上看到输出时,它显示的是图片 404 .

这是我的代码示例 tailwind.config.js

module.exports = {
  purge: [],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {
      // backgroundImage: theme => ({
      //   'body-bg': "url('./../images/px1.gif')",
      // })
      backgroundImage: theme => ({
        'hero-pattern': "url('../images/px1.gif')",
       })
    },
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

这里是 webpack 设置

let mix = require("laravel-mix");

mix.js("src/js/app.js", "js/app.js")
mix.css("src/css/app.css", "css/app.css")
  .options({
    postCss: [require("tailwindcss")],
  }).setPublicPath('public')

这是文件夹结构

enter image description here

我得到这个输出 css

.bg-hero-pattern {
  background-image: url(/images/px1.gif?9ee64f464ce65b98bb9e4168105dc9b3);
}

输出CSS应该是这个

.bg-hero-pattern {
  background-image: url(../images/px1.gif?9ee64f464ce65b98bb9e4168105dc9b3);
}

在路径文件前添加~

theme: {
   extend: {
   backgroundImage: theme => ({
    'hero-pattern': "url('~/images/px1.gif')",
   })
   },
},

希望这个回答对和我一样遇到同样问题的人有所帮助。

在对我的代码进行一些研究之后,我找到了解决方案并且它对我有用。我需要在 mix.Option 中添加这个 processcssurls: true, 。你可以在这里学习 Laravel Mix Options