Excellent question! This is a very common point of confusion.
You cannot use a direct local file system path like D:/myapp/static/... in your CSS url(). The browser has no access to your computer's file system for security reasons.
Instead, you need to use a web-accessible URL path. SvelteKit makes this very easy.
The Correct Way: Using the static Folder
Any file you place in the static folder at the root of your project is served from the root (/) of your website.
Step 1: Make sure your image is in the static folder.
Your project structure should look like this:
myapp/
├── static/
│ └── svelte-logo.svg <-- YOUR IMAGE IS HERE
├── src/
│ ├── routes/
│ │ └── +page.svelte
│ └── ...
└── ...
Step 2: Use a root-relative path in your CSS.
To reference svelte-logo.svg from your CSS, you just use the path /svelte-logo.svg. The leading slash / tells the browser to look for the file at the very root of the site's domain.
Here is the corrected <style> block for your +page.svelte:
<style>
/* ... other styles ... */
.front {
/* It's better to use separate properties for clarity */
background-image:
url('/svelte-logo.svg'), /* <-- CORRECT PATH */
url('/svelte-logo.svg'); /* <-- CORRECT PATH */
background-repeat: no-repeat, no-repeat;
/* Use the positions you had before */
background-position:
5em 5em,
calc(100% - 5em) calc(100% - 5em);
/* The background-size you had */
background-size: 8em 8em;
/* Add a background color as a fallback */
background-color: #333;
/* NOTE: Your original 'background' property would have overridden
your 'background-size' property. Using individual properties
like background-image, background-repeat, etc., is safer. */
}
/* ... rest of your styles ... */
</style>
Complete +page.svelte Example
Here is a full, working example you can copy and paste. I've also added some comments to explain a potential issue with your em units.
<!-- src/routes/+page.svelte -->
<script>
let flipped = false;
</script>
<div class="container">
<p>Flip the card</p>
<button
class="card"
class:flipped={flipped}
on:click={() => flipped = !flipped}
>
<!-- This div now has the background image -->
<div class="front">
<!-- The symbol is now on top of the background image -->
<span class="symbol">💖</span>
</div>
<div class="back">
<div class="pattern"></div>
</div>
</button>
</div>
<style>
:global(:root) {
--bg-1: #222;
--bg-2: #333;
--bg-3: #444;
--fg-1: #ff69b4;
--fg-2: #fff;
}
.container {
display: flex;
flex-direction: column;
gap: 1em;
height: 100vh;
align-items: center;
justify-content: center;
perspective: 1000px;
background: #111;
color: white;
font-family: sans-serif;
}
.card {
position: relative;
aspect-ratio: 2.5 / 3.5;
height: clamp(250px, 60vh, 500px);
border-radius: 2em;
transform: rotateY(0deg);
transition: transform 0.4s;
transform-style: preserve-3d;
padding: 0;
user-select: none;
cursor: pointer;
box-shadow: 0 10px 20px rgba(0,0,0,0.5);
/*
POTENTIAL ISSUE: The original 'font-size' was tiny (min(1vh, 0.25rem)).
This would make the 'em' units in your background properties effectively zero.
Let's set a more reasonable font-size for the card context.
*/
font-size: 16px; /* Adjust as needed */
}
.card.flipped {
transform: rotateY(180deg);
}
.front, .back {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
backface-visibility: hidden;
border-radius: 2em;
border: 1px solid var(--fg-2);
box-sizing: border-box;
padding: 2em;
}
.front {
background-image:
url('/svelte-logo.svg'), /* Correct path for image in 'static' folder */
url('/svelte-logo.svg'); /* Use root-relative paths */
background-repeat: no-repeat;
background-position:
5em 5em,
calc(100% - 5em) calc(100% - 5em);
background-size: 8em; /* You only need one value if they are the same */
background-color: var(--bg-1);
}
.back {
transform: rotateY(180deg);
}
.symbol {
font-size: min(15vw, 150px);
color: var(--fg-1);
/* Add a drop-shadow to make it stand out from the background logos */
filter: drop-shadow(0 0 5px rgba(0,0,0,0.7));
}
.pattern {
width: 100%;
height: 100%;
background-color: var(--bg-2);
background-image:
radial-gradient(var(--bg-3) 0.9em, transparent 1em),
repeating-radial-gradient(var(--bg-3) 0, var(--bg-3) 0.4em, transparent 0.5em, transparent 2em, var(--bg-3) 2.1em, var(--bg-3) 2.5em, transparent 2.6em, transparent 5em);
background-size: 3em 3em, 9em 9em;
background-position: 0 0;
border-radius: 1em;
}
</style>