Lazy Load Code Generator: Free Online Tool
FREE TOOL · NO SIGN-UP
This lazy load code generator writes ready-to-paste HTML that delays loading images, iframes and background images until a visitor scrolls near them. For images and embeds it uses the browser’s native loading="lazy" attribute, and for CSS background images it adds a small Intersection Observer script. Lazy loading cuts the data a page downloads at first, which helps load times, especially on mobile. Enter the source URL, size and alt text, pick the element type and copy the code. It all runs in your browser.
How to Use the Lazy Load Code Generator
Pick an element type, fill in a few fields and copy the code.
Step 1: Enter or paste your input
Choose the Element:
- Image for a normal
<img>tag. - iframe / video embed for YouTube, Vimeo, maps or any other embedded page.
- Background image (JS) for a
<div>with a CSS background, such as a hero or banner section.
Then paste the Source URL of the image or embed.
Step 2: Choose your options
- Alt / title: alt text for images, or the title attribute for iframes, which screen readers use.
- CSS class: optional, to match your theme’s styles.
- Width and Height: the default is 800 × 450. Use the real dimensions or aspect ratio of your media.
- decoding=”async”: on by default for images. It lets the browser decode the image off the main thread.
- Add
Step 3: Copy, download or apply the result
Copy the generated code or download it as an HTML file. In WordPress, paste it into a Custom HTML block. In theme templates, replace your existing tag. A reminder appears with the code: do not lazy load the main image at the top of the page.
How Lazy Loading Speeds Up Pages
Lazy loading means the browser waits to download an image or embed until it is close to the visible part of the screen, instead of loading everything as soon as the page opens.
A long article with 20 images, two YouTube videos and a map can download several megabytes before the reader scrolls at all. Most of that is never seen by visitors who leave early. With lazy loading, the browser downloads only the first screen’s media straight away and fetches the rest as the reader scrolls.
The benefits:
- Faster initial load, because fewer requests compete for bandwidth.
- Less data used on mobile connections.
- Lower server and CDN costs, since unseen media is not downloaded.
- Lighter iframes. YouTube and map embeds load lots of scripts. Deferring them makes a big difference.
The browser starts loading lazy images a little before they scroll into view, so readers rarely see blank spaces. The distance depends on the browser and connection speed.
Native loading=”lazy” vs Intersection Observer
| Native loading=”lazy” | Intersection Observer (JavaScript) | |
|---|---|---|
| Works on | <img> and <iframe> | Any element, including CSS backgrounds |
| Code needed | One attribute | A short script |
| Browser support | All modern browsers | All modern browsers |
| Without JavaScript | Works | Needs a <noscript> fallback |
| Control over distance | Browser decides | You set it (this tool uses 200px) |
Native lazy loading is the right choice for normal images and embeds. It needs no script, cannot break when JavaScript fails and is supported by Chrome, Edge, Firefox and Safari. Browsers that do not support it simply load the image normally.
Intersection Observer is needed for background images, because CSS backgrounds cannot use the loading attribute. The generated script finds every element with the lazy-bg class, waits until it is within 200px of the screen and then sets the background. If the browser lacks Intersection Observer support, it loads all backgrounds straight away, so nothing goes missing. You only need the script once per page, even if you have several lazy backgrounds.
WordPress has added loading="lazy" to content images automatically since version 5.5, so check your existing images before adding it by hand.
Lazy Loading, LCP and Core Web Vitals
Largest Contentful Paint (LCP) measures how long it takes for the biggest visible element, often a hero image, to appear. It is one of Google’s Core Web Vitals.
Lazy loading helps Core Web Vitals overall by freeing bandwidth for what matters. But lazy loading the LCP image itself makes it worse. The browser has to work out the layout before it knows the image is visible, which delays the download.
So:
- Do not lazy load the hero image, logo or any image in the first screen on mobile or desktop.
- Do lazy load images and embeds further down the page.
- For the main image, consider adding
fetchpriority="high"by hand to tell the browser to load it first.
Check your LCP element in PageSpeed Insights or Chrome DevTools’ Performance panel. If it has loading="lazy", remove it. WordPress skips lazy loading for the first content image in recent versions for this reason, but themes and page builders may still add it.
Avoiding Layout Shift With Width and Height
Cumulative Layout Shift (CLS), another Core Web Vital, measures how much content jumps around while a page loads. Lazy loaded images are a common cause: if the browser does not know an image’s size, it reserves no space, and when the image arrives, the text below jumps down.
The fix is to always set width and height. The generator includes them by default. Browsers use the two numbers to calculate the aspect ratio and reserve the right space, even if your CSS makes the image responsive with max-width: 100%; height: auto;.
Use the real dimensions or at least the real ratio. An 800 × 450 setting reserves a 16:9 box. If your image is square, the box will be wrong and a shift will still happen.
For background images, the generated div uses your height as a min-height, so the section keeps its size while the background loads. For iframes, the width and height attributes, or a CSS aspect-ratio, keep embeds from collapsing.
Key Features of Our Lazy Load Code Generator
- Free with no signup.
- Three element types: image, iframe and background image.
- Native
loading="lazy"for images and iframes. - Intersection Observer script with fallback for background images.
- Width and height included to prevent layout shift.
- Optional decoding=”async” and noscript fallback.
- Copy and Download buttons; runs in your browser.
Who Should Lazy Load Media
- Bloggers with long, image-heavy posts.
- Ecommerce stores with category pages full of product photos.
- Developers hand-coding templates and landing pages.
- Site owners with embeds like YouTube videos, maps and social posts.
- SEOs working on Core Web Vitals.
Pro Tips and Mistakes to Avoid
Checklist of best practices
- Lazy load everything below the first screen.
- Load the hero or LCP image normally.
- Always include width and height.
- Write meaningful alt text for every image.
- Use responsive images with the Srcset Generator for different screen sizes.
Common mistakes and quick fixes
- LCP got worse. The hero image is lazy loaded. Remove the attribute from it.
- Content jumps while scrolling. Width and height are missing or wrong. Add the real dimensions.
- Background images missing. The script was not included, or the class is not
lazy-bg. Keep the class from the generated code. - Double lazy loading. A plugin and your code both add it. Use one method.
Load Only What Users See
Generate your lazy load code above and paste it into your page. Create responsive images with the Srcset Generator, embed videos with the YouTube Embed Code Generator, and shrink your code with the HTML Minifier. Find every free tool in the FHSEO Hub tools directory.
