question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unocss works well with background images in test environments, but does not display them properly when packed into production

See original GitHub issue

一、普通写法

<div bg="center no-repeat" class="bg-[url(./src/assets/uploads/bg.png)]"></div>

二、配置写法

 <!-- <div soumns-min soumns-bg-bg-png>777</div> -->
rules:[
  [
      'soumns-min',
      {
        'min-width': '100vw',
        'min-height': '100vh',
        'box-sizing': 'border-box'
      }
    ],
    // 动态背景
    [
      /^soumns-bg-(.*)$/,
      ([_, d]) => {
        let img = d.split('-')[0]
        let type = d.split('-')[1]
        return {
          background: `url('./src/assets/uploads/${img}.${type}')`,
          'background-size': '100% 100%',
          'background-repeat': 'no-repeat',
          'box-sizing': 'border-box'
        }
      }
    ]
]

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
UltraCakeBakerycommented, Aug 11, 2022

This issue is probably not related to unocss. You probably want to read up on https://vitejs.dev/guide/assets.html#the-public-directory

Basically, vite does not bundle your images because they are in your src folder. It isn’t aware of them. You want them in the public folder instead (or the static folder like in frameworks like svelte-kit and nuxt).

0reactions
enochzzzcommented, Dec 2, 2022

I created a simple plugin to handle this requirement. vite-plugin-unocss-bgimg

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix CSS background-image not working | HTML/CSS
To help with that, here are four ways to fix your background-image not working, using HTML and CSS: 1. Check that your CSS...
Read more >
Background image not showing in Safari - Stack Overflow
Safari has an apparent bug where it won't show some jpg/JPEG images of a certain type in backgrounds if some criteria are met....
Read more >
How to Use Background Effects in Microsoft Teams Meetings
Microsoft Teams meetings support the use of background effects like blurring or background images (custom or a default set from Microsoft).
Read more >
WordPress Background Images: How to Add, Edit ... - Kinsta
Learn how to activate, edit, and customize them in this in-depth guide. ... The background image may not be shown in its entirety...
Read more >
SVG in CSS - Iconify Documentation
Mask image, combined with background color set to currentColor. First 2 methods can be ... Icons with palette (hover does not change icon...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found