Edited: codesandbox link here.
I followed this instruction to display loading status before the page is fully rendered. The concept is that, <div id="root" class="loader"></div>
will stay empty until app is rendered into it. So I can use css psuedo-class .loading:empty
to display css before app renders, and content will disappear after app is rendered (the div is no longer empty).
.loader:empty {
position: absolute;
top: calc(50% - 4em);
left: calc(50% - 4em);
width: 6em;
height: 6em;
background-color: red;
}
<div id="root" class="loader"></div>
It's was alright when I followed the instruction; however, when I was trying to use local images by background-image: url(./src/images/image.svg);
, the image couldn't be displayed. Instead, I was able to use hyperlinks from Unsplash or Picsum
Workable:
.loader:empty {
position: absolute;
top: calc(50% - 4em);
left: calc(50% - 4em);
width: 6em;
height: 6em;
background-image: url(https://picsum.photos/200);
}
Not workable:
.loader:empty {
position: absolute;
top: calc(50% - 4em);
left: calc(50% - 4em);
width: 6em;
height: 6em;
background-image: url(./src/images/image.svg);
}
Is there any ways to use image in CSS?
By the way, I've also tried the second method which is add another div and a image tag in index.html and use hooks to remove it after mounted. It works fine at first, but not in the routed page. I have multiple pages so I have to copy and paste every useEffect and useState hook at each of my router which is quite annoying.