Fix an image layout shift and check it on a slow connection
The image arrives and the link you were about to press moves down the page. Compressing the image may shorten the wait, but the layout still depends on the moment its size becomes known. Build a case slow enough to see, then compare the same geometry before and after the fix.
- Before you start
- Python 3, a browser with developer tools and three files in one empty directory. Stop any other demo server on port 8000 first.
- What to verify
- Prevent the text below a responsive 3:2 image from jumping as its bytes arrive, at desktop and mobile widths.
Choose an image with a known ratio
Save this tiny fixture as chair.svg. Its natural dimensions are 900 by 600, a 3:2 ratio. The small file keeps the example independent of a third-party image host. A deliberate server delay in the next steps will make the missing reservation visible even on a fast machine.
<svg xmlns="http://www.w3.org/2000/svg" width="900" height="600" viewBox="0 0 900 600">
<rect width="900" height="600" fill="#ead7af"/>
<rect x="275" y="150" width="350" height="275" rx="35" fill="#34312c"/>
<rect x="250" y="380" width="400" height="65" rx="20" fill="#d65c26"/>
</svg>For a real photograph, inspect the exported file rather than trusting the size on a design mock-up.
Record the position that matters
Create index.html. Its CSS scales the image to the available column. The image element is deliberately missing its intrinsic width and height. The script records the text position before the image loads, then reports the change in CSS pixels. Keep the page still during this measurement.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Layout test</title>
<style>
main { max-width: 600px; margin: auto; }
img { display: block; width: 100%; height: auto; }
</style>
<main>
<h1>Equipment</h1>
<img src="chair.svg" alt="">
<p id="next">Compare models</p>
</main>
<script>
const img = document.querySelector('img');
const topOfText = () => document.querySelector('#next').getBoundingClientRect().top;
const before = topOfText();
img.addEventListener('load', () => {
console.log('delta px', Math.round(topOfText() - before));
});
</script>
</html>This number is a diagnostic distance. The browser’s CLS metric also considers viewport impact and timing, so do not report a pixel delta as a CLS score.
Make the network delay repeatable
Save server.py, run python3 server.py and visit http://127.0.0.1:8000/. In the Network panel, disable the cache while developer tools are open. Reload and watch the text move when chair.svg finishes. The 1.5-second delay is local and repeatable. To inspect slower conditions, apply a network throttle and reload again.
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from time import sleep
class Demo(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path.split('?')[0] == '/chair.svg':
sleep(1.5)
super().do_GET()
ThreadingHTTPServer(('127.0.0.1', 8000), Demo).serve_forever()Read the console. The unfixed example should show a positive delta. Record the viewport width alongside it so your second measurement is comparable.
Reserve the ratio, then measure again
Replace only the img line in index.html with the line below. The width and height describe the original file. Responsive CSS still chooses the displayed width. On a 600-pixel column the matching reserved height is 400 pixels. Reload at the first width and again in a narrow mobile viewport.
<img src="chair.svg" alt="" width="900" height="600">For this isolated page, expect delta px 0 at both widths. If it still jumps, compare the declared ratio to the actual file and check for a rule forcing an unrelated height. Stop the lab server with Ctrl+C.
Try this prompt with your agent
Fix this reproducible image shift. Preserve the responsive width. Use the source dimensions, identify the element that moves and give me a before/after check at two viewport widths.A trap worth catching
A single fast reload with a warm cache can hide the problem. Keep the artificial delay and compare cold loads before claiming the shift is fixed.