Whenever you load large image on the page and it's taken too much time, that time you can show loading image or you can say Placeholder image ( Html Image Placeholder ) within the same image tag.
You can do this simply by using CSS. Use background property of css and set url of an loading image. According to me you should write no-repeat with it because if you not add this line then your image will repeat more than one times.
Html Image Placeholder : For large size image loading on the html page you can show loading image or you can say Placeholder image within the same image tag. If you want to upload image on server without page reload then must read upload image without page reload.
Html Image Placeholder
Now let's start with an example:
.image_placeholder{
background: url('loading.gif') no-repeat;
background-position: center;
width:200px;
height:200px;
}
Now, you can use this css class into yout img tag to show placeholder image until original image loading into img tag.
<img class="image_placeholder" />
Output
Finally your HTML page will be like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Show Placeholder Loading Image</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.image_placeholder{
background: url('loading.gif') no-repeat;
background-position: center;
width:200px;
height:200px;
}
</style>
</head>
<body>
<div class="container">
<h2 class="text-danger">Show Placeholder Loading Image </h2>
<div class="col-md-12 text-center">
<img class="image_placeholder" src="images/my_image.png" />
</div>
</div>
</body>
</html>
Basic CSS Image Placeholder
Why use a placeholder
When an image takes time to load, the space it occupies may collapse or shift while the file arrives. A placeholder keeps the layout stable and tells the visitor that content is coming.
The simplest approach is a CSS background on an image-sized element. It works even before the real image loads, because the background is a separate asset from the main image.
Complete HTML and CSS Example
Image markup
Place an <img> element inside a container that has the placeholder class.
<div class="image_placeholder">
<img src="images/my_image.png" alt="Description of the image" />
</div>
Placeholder styling
Set a width and height that match the final image, and use a centred, non-repeating background.
.image_placeholder {
width: 320px;
height: 240px;
background: url('loading.gif') no-repeat;
background-position: center;
background-size: cover;
}
If the real image has the same dimensions, the placeholder and the final image occupy the same space, so the layout does not shift when the image arrives.
Responsive Image Placeholder Layout
Aspect ratio
Fixed pixel dimensions are unreliable on small screens. Use a wrapper with an aspect ratio so the placeholder scales with the container.
.image_placeholder {
width: 100%;
aspect-ratio: 16 / 9;
background: url('loading.gif') no-repeat center;
background-size: cover;
}
This keeps the placeholder proportional at every viewport size, so the layout remains stable on mobile and desktop.
object-fit and sizing
If the image uses object-fit, the placeholder should use the same approach so the visual result matches the final image as closely as possible.
Show a Placeholder While an Image Loads
Loading class pattern
A common approach is to add a loading class to the container and remove it once the image's load event fires.
<div class="image_placeholder is-loading">
<img src="images/my_image.png" alt="Description" onLoad="this.parentElement.classList.remove('is-loading')" />
</div>
The placeholder stays visible until the browser reports that the image has finished loading, then it disappears.
Handle Broken Image URLs
Error fallback
If an image fails to load, the placeholder should not remain forever. Use an error event to switch to a fallback image or show a neutral empty state.
This prevents a loading indicator from spinning indefinitely when the file is missing or the URL is wrong.
Lazy Loading and Performance
Native lazy loading
For images below the fold, add loading="lazy" to the image element. The browser defers loading until the user scrolls near the image.
<img loading="lazy" src="images/my_image.png" alt="Description" />
Combine lazy loading with a placeholder so the layout does not jump when the image eventually loads.
Prioritizing above-the-fold images
Do not lazy-load the main hero image or the first image in the article. Lazy-load only the images that are below the initial viewport.
Common Placeholder Problems
Repeating backgrounds
If you forget no-repeat, the loading image will tile across the entire box. Always include it unless you intentionally want a pattern.
Missing dimensions
Without explicit width and height, the placeholder collapses to zero size. Set dimensions or an aspect ratio before the image loads.
Placeholder remaining after load
If the placeholder stays visible after the image loads, the class removal or event listener may not be firing. Check the load event and the selector in your script.
Accessibility for Placeholder Images
Meaningful alt text
The placeholder itself is visual, but the final image must carry meaningful alt text. Describe what the image shows, not that it is a loading image.
Decorative images
If the image is decorative and carries no information, set alt="" so assistive technology skips it.
FAQ
What is an HTML image placeholder?
It is a visual state, usually a background or skeleton, that occupies the image's space while the real image loads or if the image fails to load.
How do I show a loading image in HTML?
Apply a CSS background with no-repeat and a centred position to an image-sized container, then hide it when the image loads.
How do I prevent layout shift?
Set explicit width and height or an aspect ratio on the placeholder so the space is reserved before the image arrives.
What should alt text contain?
Alt text should describe the image's content or purpose. If the image is purely decorative, use an empty alt attribute so assistive technology skips it.
Should I use a GIF or CSS placeholder?
A CSS placeholder is lighter and more flexible. A GIF works, but it adds an extra file and may not match the final image dimensions as well.
Conclusion
An image placeholder keeps the layout stable and gives visitors feedback while an image loads. The simplest approach is a CSS background on a sized container, but responsive aspect ratios and load-event cleanup make it robust.
Add lazy loading for below-the-fold images, provide meaningful alt text, and handle broken URLs so the placeholder never stays forever.




(1) Comments
Sankar
great post.
Always helpful
Write a comment