Building a Live Viewport & Breakpoint HUD Pill for Responsive Web Debugging
A viewport breakpoint HUD pill binds `window.innerWidth` and `window.innerHeight` through a `resize` listener throttled by `requestAnimationFrame`, displaying live dimensions (`1440×900`) and the active CSS breakpoint tag (e.g. `sm`, `md`, `lg`, `xl`, `2xl`).
Step 1: Design the Viewport HUD Pill Markup
Build the compact floating pill showing pixel dimensions and active breakpoint badge.
<div class="breakpoint-hud-pill" id="breakpointHud" role="status" aria-live="polite">
<span class="hud-badge" id="hudBreakpoint">LG</span>
<span class="hud-dims"><span id="hudWidth">1024</span> × <span id="hudHeight">768</span></span>
<span class="hud-dpr" id="hudDpr">@2x</span>
</div>
Step 2: Style Floating Pill with High-Contrast Glassmorphism
Position the HUD fixed in the bottom-left corner with monospace tabular numbers.
.breakpoint-hud-pill {
position: fixed;
bottom: 16px;
left: 16px;
background: rgba(15, 23, 42, 0.9);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 20px;
padding: 6px 14px;
display: flex;
align-items: center;
gap: 8px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.8rem;
color: #fff;
z-index: 9999;
box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
.hud-badge {
background: #6366f1;
color: #fff;
font-weight: 700;
border-radius: 6px;
padding: 2px 6px;
font-size: 0.7rem;
}
.hud-dims {
font-variant-numeric: tabular-nums;
color: #cbd5e1;
}
.hud-dpr {
color: #94a3b8;
font-size: 0.75rem;
}
Step 3: Implement the Responsive Resize Listener
Update viewport dimensions and calculate breakpoint state on every resize event.
const hudW = document.getElementById('hudWidth');
const hudH = document.getElementById('hudHeight');
const hudBp = document.getElementById('hudBreakpoint');
const hudDpr = document.getElementById('hudDpr');
function updateHud() {
const w = window.innerWidth;
const h = window.innerHeight;
hudW.textContent = w;
hudH.textContent = h;
hudDpr.textContent = `@${window.devicePixelRatio || 1}x`;
let bp = 'XS';
if (w >= 1536) bp = '2XL';
else if (w >= 1280) bp = 'XL';
else if (w >= 1024) bp = 'LG';
else if (w >= 768) bp = 'MD';
else if (w >= 640) bp = 'SM';
hudBp.textContent = bp;
}
window.addEventListener('resize', () => {
requestAnimationFrame(updateHud);
});
updateHud();
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Whenever I design responsive widgets or fluid typography, I rely heavily on clamp() and container queries (@container). Testing down to 320px viewport widths in Chrome DevTools will instantly reveal edge cases before your users see them.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why throttle window.onresize with requestAnimationFrame?
Window resize events fire up to 120 times per second during window drag. Throttling via requestAnimationFrame synchronizes DOM updates with display refresh cycles, avoiding frame drops.
What is the difference between window.innerWidth and document.documentElement.clientWidth?
window.innerWidth includes the vertical scrollbar width (typically 15–17px on Windows), which matches CSS media query `@media (min-width: ...)` evaluations.
Can the HUD measure Container Query dimensions instead of viewport width?
Yes, attach a ResizeObserver to a parent container element to report exact container width changes independent of overall screen size.
How can you hide the HUD pill in production environments?
Wrap initialization in a check: `if (location.hostname === "localhost" || location.search.includes("debug=true"))` to keep production clean for end users.
Can users drag the HUD pill to different corners of the screen?
Yes, adding pointerdown/pointermove drag listeners allows visitors to reposition the HUD freely across the four screen quadrants.