📝 Originally published (in Japanese) at forge.workstyle.tech.
TL;DR A slide fitting check measured a box whose min-height kept it at the viewport height. The scrollHeight and clientHeight comparison then kept reporting that the content did not fit, even as its zoom changed. The search reached its lower bound of 0.3 on both mobile and desktop.
TL;DR
All slides shrank to 30% because the box used for the fit check had a min-height set to the viewport height. Measuring the box kept reporting that the content did not fit, even as the scale changed, so the binary search shrank it to the 0.3 lower bound. The fix was to temporarily set the content’s min-height to 0, measure its offsetHeight, and then restore the original value.
How I Built a System That Shrinks Only Slides That Overflow
I used CSS zoom to add a system that shrinks only content that overflows. It measures the height, then uses binary search—a method that narrows the search range by half each time—to find a scale that fits. But after someone reported that a little scrolling remained on the last page, I tightened the fit check, and the display broke right away.
At first, I thought, “Maybe it still doesn’t fit.” The opposite was happening: the content fit, but the function was reporting that it didn’t.
At 02:41 on October 11, someone checking the page asked me to “adjust the content so it fits on one screen in every case.”
At 02:45, I added a system that uses zoom to shrink the content only on slides that overflow. The padding used for page navigation stays at its original size. On portrait phones, the content shrank to around 30% and became unreadable, so I switched to a two-column layout in landscape mode.
I also measured the scale for different screen sizes. It was 88% at 390×844, 67% at 375×667, and 75% at 360×740. In landscape, it was 80% at 844×390 and 72% at both 667×375 and 740×360. At 1280×720, it was 83%. At 768×1024, 1440×900, and 1920×1080, the content fit without scaling.
At this point, I proceeded with the deployment and commit even though one test was still failing. It was the stylesheet size limit check. The stylesheet was 56,000 bytes against a 52,000-byte limit, so I raised the limit to 56,000 bytes to make the test pass.
Why Did a 2px Buffer Shrink Every Slide?
At 03:08, I got a message: “There’s still a little scrolling on the last page.”
I added a 2px buffer to the measurement. The 02:45 version reported “+1px of breathing room.” At 03:10, I changed it to leave a 2px buffer. I also remeasured every time a page loaded and every time visualViewport fired a resize event.
Then, at 04:20, I got another message: “Is it broken on desktop?” I checked the screen. It wasn’t just mobile: every slide was at 30% on both desktop and mobile. I fixed it at 04:30.
That’s when I took another look at what I was measuring.
The fit check was using a box that stayed as tall as the viewport
To center short content, I had set the viewport height as the box’s min-height (a CSS property that sets an element’s minimum height). Even when the content was short, the box itself stayed as tall as the viewport.
The 03:10 version used this check:
slide.scrollHeight <= slide.clientHeight - 2 scrollHeight is the height of the content, including anything that overflows. But when min-height stretches the box to the viewport height, scrollHeight can’t be less than the viewport height either. clientHeight is the element’s inner height.
The check compares against a value 2px less than the viewport height. So even content that fits exactly fails the check. No matter how much the scale changes, the result is always “doesn’t fit.”
The code uses binary search to find the scale. If the content doesn’t fit at 1x, it searches for a scale that does. If the condition is never met, the search can’t find an answer, no matter how long it continues. Eventually, it shrinks to the lower bound of 0.3.
export function largestFit(fits, lo = .3, steps = 8) { if (fits(1)) return 1; let low = lo; let high = 1; for (let i = 0; i < steps; i++) { const mid = (low + high) / 2; if (fits(mid)) low = mid; else high = mid; } fits(low); return Math.floor(low * 1000) / 1000; } The 02:45 version used “+1,” so the check passed even when the box and viewport were the same height. By adding a 2px margin in the 03:10 version, I turned that same height into a “doesn’t fit” result.
Until I found this difference, I was only checking whether anything overflowed. I wasn’t checking how far the scale had shrunk. Only after everything got smaller did I finally check the result of the fit check itself.
How I fixed it: measure the content height
The fix measures the content height after temporarily setting its min-height to 0. It uses the content’s offsetHeight (the element’s height), then restores the original min-height after the check.
inner.style.minHeight = '0'; const fits = zoom => { apply(zoom); return inner.offsetHeight <= slide.clientHeight - 2; }; apply(largestFit(fits)); inner.style.minHeight = ''; After the fix, no slides needed to shrink at 1920×1080. At 1280×800, only the final page was scaled to 95%; at 1366×768, only the final page was scaled to 88%. At 1280×720, the final page was scaled to 82% and the cover to 99%. At 390×844, slides stayed at 87% or higher; at 375×667, they stayed at 66% or higher.
I also checked screenshots of every slide at 1280×800 and 1920×1080. I didn’t just measure the scale percentages; I checked how the slides actually looked.
I haven’t confirmed whether this version with a 30% scale appeared only in the preview or also after it was published.
Key takeaways
- Check whether
zoomhas reached its lower bound of 0.3. - Inspect the measured element and its
min-height. - Check whether the box grows to the viewport height in the
scrollHeightandclientHeightcomparison. - If needed, temporarily set the content's
min-heightto 0, measureoffsetHeight, then restore it after the check. - Review screenshots at sizes such as 1280×800 and 1920×1080, as well as the zoom values.