---
name: measure-ui
license: MIT
description: Checks the layout and behavior of a web page in a real browser with measured numbers instead of judging by eye. Sets the viewport first, reports element sizes and positions as numbers, switches between items in more than one order, and keeps one tab open for before and after comparisons. Use it after a visual, CSS or interaction change to a page, when the user reports alignment, spacing, overflow, responsive or mobile problems or says something looks off, asks whether a layout works, or before declaring a UI change done or a page correct. It uses DOM numbers, not screenshots alone. Not for unit tests (they do no layout), backend-only or API changes, code review without a rendered page, or an environment without browser access.
compatibility: Needs an agent tool that can open a page in a real browser. Full use needs JavaScript execution in the page. Without it the screenshot-only fallback in step 0 applies.
metadata:
  author: "Jakob Christensen"
  source: "https://jakobchristensen.com"
---

# Measure UI

Goal: a statement about how a page looks or behaves rests on numbers read from the running page, so a reader can compare them with what was wanted.

## Steps

0. **Check the tool.** Without a real browser tool, stop and say the check was not run. With a browser that cannot run JavaScript, report only what screenshots show, label those numbers "read from screenshot" and write "not checked" for scroll width, computed styles, element counts and anything else that needs the DOM.
1. **Fix the viewport first.**
   - Set an explicit width and height in CSS pixels before loading the page. If the tool can only resize a tab that already shows a page, load any page once, set the size, then load the target page (the read-backs below are taken after step 2 has loaded and settled it, at every size). If the user gave a target (a phone, a screenshot), use its CSS-pixel size (a screenshot's pixel width divided by the pixel ratio if known, else its pixel width, and say which). Otherwise check 375 x 812 and 1280 x 800 and name them, and repeat steps 2 to 5 at each size.
   - Read back `document.documentElement.clientWidth`, `clientHeight` and `devicePixelRatio`, report them and note any difference from what you set (on an emulated phone, `innerWidth` can exceed the set width when content overflows).
   - If the read-back size is 0 x 0 (for example a hidden or collapsed browser view), bring the view to the front or resize it and set the size again, once. If it still reads 0 x 0, stop and report the check as failed instead of measuring.
   - At every size compare `document.documentElement.scrollWidth` with `clientWidth`: larger means horizontal overflow, a fail unless the user asked for it.
2. **Open the page and wait for it to settle.**
   - Load the page from the dev server that serves the files changed in this task (in a monorepo, match the changed package or route). Find its command in the project's launch config or `package.json` scripts. If no server runs, read the body of the project's development script and start it in the background only if it is a local dev server (no deploy, migration, seed or production side effects. If you cannot tell, ask), poll for up to 30 seconds until it answers (else report the start as failed), say which port it uses and leave it running. Use the deployed address only when the question is about the deployed state, and say which one you measured.
   - Confirm that the page that answered is the one you meant (the address plus the title or one project-specific element taken from the code or page content). Another project's server may hold the port. A login or consent wall is the wrong page: do not sign in.
   - Wait on a condition, not a fixed pause: `await document.fonts.ready`, the images that affect the target's size or position complete, the target element present and in the state the request implies (a framework can render after those signals). If no state can be inferred, measure the first stable state that shows the target's real content (not a placeholder, skeleton or collapsed panel) and record that assumption. If an authenticated session already exists, use read-only navigation and measurement, confirm no dialogs and change no data.
   - Read the target's rect twice, 250 ms apart. If the two reads differ, or later rendering shows up, repeat the pair, at most 3 pairs in total. If they still differ, report the element as unstable and follow step 7.
3. **Write down what is wanted as numbers** before you measure: a size, a gap, a position, an order, an alignment, or "no horizontal scroll". If the request has no measurable target ("make it look centered"), derive the smallest set of checks that answers it, list them before measuring, label them "derived checks" and judge only those. If the user gave a reference (a screenshot plus "like this"), it is the spec: take it literally, list the viewport and any condition the screenshot does not show as assumptions, and do not ask design questions that the reference already answers.
4. **Measure with the page's own geometry.**
   - Rects: for each element directly related to the user's request, run in the page `el.getBoundingClientRect()` and report `x`, `y`, `width`, `height` in px (rounded). Rects are relative to the viewport: scroll to a known position first (`scrollTo(0, 0)` for content above the fold, `scrollIntoView({ block: 'center' })` for content below) and report `scrollX` and `scrollY` for each read. To compare a distance or alignment between elements read at different scroll offsets, convert to document coordinates (`x + scrollX`, `y + scrollY`) first. Add any value the wanted result depends on (computed font size, the number of items in a row). A rect of 0 x 0, or a missing element, means hidden or absent: wait 250 ms and read once more, and if it is still 0 x 0 or missing report a failed check, not a size.
   - Special contexts: for an element in an iframe or an open shadow root, measure inside that document and name the frame you used. Write "not checked" for a cross-origin iframe or a closed shadow root. For an infinite or virtualized list, measure only the rendered items and write "not checked" for the rest.
   - Reachability: for an element a user must reach, scroll it into view, then check that `elementFromPoint` at its center (`shadowRoot.elementFromPoint` inside an open shadow root, the frame's `contentDocument.elementFromPoint` with frame-relative coordinates inside a same-origin iframe) returns it or a descendant, so a fixed header or banner is not covering it.
   - Themes and motion: if the change touches them, emulate `prefers-color-scheme` and `prefers-reduced-motion` and repeat the measurement, or write "not checked" if the tool cannot emulate them.
   - Report numbers, not adjectives. Take a screenshot only when the user should look at something, and say what to look at.
5. **Exercise the behavior, not only the first state.** A page that switches between items (tabs, cards, a carousel, an accordion, routes) is checked by switching between at least two items (with 4 or more items include the first, the last and the one with the longest `textContent`), and in more than one order: A then B, B then A, and a repeat of the same item. After a switch that changes route or content, repeat the step 2 stability check, then measure and compare with the expected state (unchanged for tabs, toggled or closed for an accordion or toggle, so a repeat there is not a failure). Check opening and closing, resize, and keyboard focus where the change touches them, and report the measured values or state changes, not adjectives. Use the mechanism the control needs (click, hover, focus or keyboard) only for controls that switch or reveal views (tabs, cards, toggles, menus), and read the control first (`tagName`, the `type` property, `href`, `textContent`, `aria-label`, whether it sits in a form). If you cannot tell what it does, do not click it and write "not clicked". Never trigger (by click, keyboard, form submission, drag or script) anything that changes state or starts a write request or an auth or checkout flow (submit, save, publish, deploy, approve, archive, reset, sign out, log out, unsubscribe, revoke, cancel, delete, buy, send), treat icon-only or unclear buttons, any `input` of type `submit` or `image`, and any `button` inside a form without `type="button"` as "not clicked", and on a deployed page change no data.
6. **For a before and after comparison, measure in one tab.** List the tabs before you start and treat every existing tab as the user's. Measure in one tab you opened, close only tabs you opened, and leave the user's tabs alone. Measure before the change, make the change, reload the same tab, and measure again with the same viewport and the same steps. If the code was already changed and no before state exists, write "before state not available" and measure only after. If you measured timing or frame rate and the user has other tabs of the same site open, write "other tabs open, numbers may be skewed" in the report (other tabs may compete for CPU, or share a renderer process in some configurations, browser-dependent, not verified here, and not expected to change layout sizes).
7. **Mind animation.** Running animations and transitions move rects between reads: wait for them to finish or pause them and say you did. To time a transition, sample it once per frame with `requestAnimationFrame`. `setTimeout` fires late in a hidden or background tab and makes a finished transition look stuck. If you replace `requestAnimationFrame` or timers with a stand-in to make a measurement steady, the stand-in must honor `cancelAnimationFrame` and `clearTimeout`. Otherwise loops of unmounted elements keep running and the numbers drift.
8. **Report** in a short table with columns Check (mark derived checks), Viewport (with pixel ratio), Scroll (x, y), Wanted, Measured, Result (pass, fail or not checked). Name every check you did not run (a size, a browser, a switching order) as "not checked".

## Guardrails

Do not change the code under test in a way that alters the number you are measuring, such as adding styles to make an element visible. Do not sign in to accounts for the check.
