#css url() path of an asset
3 messages · Page 1 of 1 (latest)
Hi @tacit kettle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To reference an image in your CSS within an Astro file, you should use the import statement in the frontmatter of your Astro file. This will allow Astro to include a built, optimized copy of that static asset in your final build. Here's how you can do it:
---
import dogUrl from '../assets/dog.png';
---
<style>
.foo {
background: url({dogUrl});
}
</style>
In this example, dogUrl is the resolved URL to the image. The path in the import statement ('../assets/dog.png') should be adjusted based on the actual location of your image file relative to the Astro file.
Please note that the Astro.resolve() method, which was used to get resolved URLs to assets, has been deprecated in v0.24 and removed entirely in Astro v2.0. So, it's recommended to use the import approach for resolving asset paths.
Sources:
- Astro Documentation - Imports (https://docs.astro.build/en/guides/imports)
- Astro Documentation - Upgrade to v2 (https://docs.astro.build/en/guides/upgrade-to/v2#removed-astroresolve)