Design comparison
Solution retrospective
I used React and Vite for this project.
For some reason I could not use
<img src="../images/illustration-thank-you.svg">
.
In this case images didn't work. However if I use
import ImgSrc from '../images/illustration-thank-you.svg';
<img src={ImgSrc}>
It works, why is that?
Please share any advice and recommendations. Thanks in advance!
Community feedback
- @visualdennissPosted over 1 year ago
Good work,
"<img src="../images/illustration-thank-you.svg">" i think this works when you have your image assets inside a public folder in React (idk how it is with Vite) when its created with create-react-app, so it is not searching inside the src folder. I assume it might be same for Vite. And about the question, could we not use src="../src/images/" etc then? my theory is that that kind of relative path might not work in production because what is being served and how the setup of root files are (e.g. index.html is served and then app takes over to place inside #root). stuff inside the src gets bundles to a js file, for it to be bundled like so, it needs to import { } like a named import, but it cannot bundled like js + images so that it can use relative paths later on like the index.html would and could. After all, those paths become in the end a relative path for the index.html, so when you have something like "<img src="../src/images/illustration-thank-you.svg">" directly inside your html, it will ask, what src, isnt that bundled and minified already into a single js file? This is my assumption and understanding. Hope it make sense.
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord