React / 34
Four ways to add images
Import it, drop it in public/, point at a remote URL, or use it from CSS. Where the file lives decides who turns it into a URL.
1 · import
import logo from "./logo.png"↓ browser loads
/assets/logo-B2xX8a1c.pngbundler checks the file exists and adds a hash
2 · public/
<img src="/logo.png" />↓ browser loads
/logo.pngcopied as-is: no hash, no check
3 · remote
<img src={recipe.image} />↓ browser loads
https://cdn.dummyjson.com/…another server hosts it
4 · css url()
background-image: url("./hero.jpg")↓ browser loads
/assets/hero-Dk3s91aQ.jpgbundler resolves it like an import
1 · Import it (the default)
Put the image next to your code in src/ and import it like a module:
import logo from "../assets/logo.png";
export default function Logo() {
return <img src={logo} alt="Tasty Bites" width={120} height={40} />;
}The import gives you a URL string. After npm run build it looks like /assets/logo-B2xX8a1c.png:
- If the file is missing or misspelt, the build fails, so you find out immediately.
- The hash in the name changes whenever the image changes, so browsers can cache it forever and still get updates.
- Very small files (under 4 KB by default in Vite) are inlined as
data:URLs, which saves a request.
Use this for images that belong to your UI: logos, icons, illustrations.
2 · The public/ folder
Files in public/ are copied to the site root untouched. Refer to them with an absolute path, no import:
// file: public/images/hero.jpg
<img src="/images/hero.jpg" alt="A table of dishes" />No hash and no check: a typo gives a 404 at runtime, not a build error. Use it for files that need a fixed name or URL: favicon.ico, robots.txt, social preview images, or big files you don't want processed.
3 · A remote URL
Images that come with your data are usually hosted elsewhere, on a CDN or the API's own server. Use the URL as it is:
export default function RecipeCard({ recipe }) {
return (
<article>
<img src={recipe.image} alt={recipe.name} width={300} height={200} loading="lazy" />
<h3>{recipe.name}</h3>
</article>
);
}Some APIs only give you an image id. Then keep the base URL in one place, like export const IMG_CDN = "https://…/"; in a constants.js, and build the URL: src={`${IMG_CDN}${recipe.imageId}`}.
4 · From CSS
For decorative backgrounds, reference the image in CSS:
.hero {
background-image: url("../assets/hero.jpg");
background-size: cover;
min-height: 240px;
}A relative url() goes through the bundler just like an import (hashed, checked). url("/images/hero.jpg") points at public/. Background images have no alt, so use them only for decoration, never for content people need to see.
Which one?
| Image | Use |
|---|---|
| Logo, icon, illustration in your UI | Import it |
| Favicon, robots.txt, a file that needs a fixed URL | public/ |
| Product or recipe photos from your API | Remote URL from the data |
| Decorative background | CSS url() |
Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
