How to use UNOCSS background image

532 Views Asked by At

I want to use the backgroundImage when using UNOCSS and set the theme according to TailwindCSS, but the desired effect is not achieved. May I ask how can I achieve the backgroundImage in UNOCSS?

1

There are 1 best solutions below

0
Anton Karpenko On

I believe you can create custom rule:

export default defineConfig({
  ...
  rules: [
    ...
    [/^bgi-\[([\w\W]+)\]$/, ([, d]) => {
      const path = <your path>
      return ({ 'background-image': `url('${path}/${d}')` })
    }],
  ],
})

Then, you can use your custom rules:

<template>
  <div class="bgi-[img.png] bg-no-repeat">
</template>

or you can try this (dk if it works) without custom rule:

<div class="bg-[url(./<path>/img.png)] bg-no-repeat"></div>