In a community-board web app's windowed feed on phones, how would you keep the reader's place steady when heights change — a photo above loading, a post edited or deleted?
answer
- fixed heights only by clamping content
- estimate per type, then measure
- reserve media boxes from known dimensions
- compensate changes above, ignore below
- a tombstone instead of a hole
basics
~20 sReserve space so most heights never change: media boxes sized from API dimensions, fixed-width counts. Otherwise estimate per post type, measure once rendered, and when a post above the viewport changes height, shift the scroll by the difference.
solid answer
~50 sFixed heights would mean clamping a mixed feed's content, so I'd measure: an estimate per post type, a real height once each post renders, stored by post id. The first lever is to stop heights changing at all: the API sends each photo's width and height, so its box is reserved before it loads, video embeds are fixed-ratio posters, and counts sit in fixed-width slots. When a height still changes — a post edited, a measurement differing from its estimate — a change below the viewport needs nothing, and a change above it means shifting the scroll by the difference so the visible post stays put. Browsers that support scroll anchoring adjust for such shifts themselves; a windowed list that positions posts from its own height table usually compensates on its own, so exactly one of them must do it. A deleted post near the reader becomes a same-height 'post removed' row.
code
pseudocode · 7 linesanchor = first post whose bottom is below the viewport top
offsetBefore = top(anchor) - viewportTop
apply height changes (measurements, edits, deletions)
recompute positions from the height table
scrollBy(top(anchor) - viewportTop - offsetBefore)go deeper
Recall that a windowed list needs every post's height, and that photos and edits can change heights after the post first renders.
Explain estimate, measure, store by id and correct, and why a change above the viewport needs a scroll adjustment while one below does not.
Remove most jumps at the source with pre-sized media and fixed-width counts, own scroll compensation in one place, and handle deletions near the reader with a tombstone.
Make sizing part of the content contract: media dimensions from the API and per-type layout rules, so the client is not fighting the data.
## Why heights matter in a windowed feed A **windowed** list keeps only the posts near the viewport in the DOM and stands in spacers for the rest. To place those spacers it needs every post's height — including posts it has never rendered. In a community-board feed the heights vary wildly: a one-line post, a gallery of six photos, an event card with an RSVP button, a poll that grows when results show. Every height the list gets wrong becomes a jump the reader sees. ## Fixed or measured | | Fixed heights | Measured heights | |---|---|---| | Position of post N | N times one height | sum of known and estimated heights | | Content | must be clamped to fit | free | | Corrections | none | needed whenever a measurement differs | | Fits a mixed feed | only by truncating text and cropping media | yes | Fixed heights are a fine choice for uniform rows such as a member list. For this feed they mean truncating posts and cropping galleries, which the product will not accept, so heights are **measured**: 1. **Estimate** each unrendered post by type and content: text length, number of photos, poll options. 2. **Measure** the real height once a post renders, and again if it resizes (a resize observer watching the rendered posts is the usual tool). 3. **Store** the measured height by post id, so the post keeps its true height after it leaves the window and when the reader scrolls back. 4. **Correct** the running positions with the difference between estimate and measurement. ## Stop heights changing in the first place Most jumps in feeds come from content that arrives late, and most of it can be sized before it arrives: - **Photos**: the API returns each image's width and height (or aspect ratio), and the post renders a box of that ratio before the image loads. A photo finishing above the viewport then changes nothing. - **Video embeds**: a fixed-ratio poster with a play control; the player loads into the same box. - **Counts**: like and comment counts sit in fixed-width slots, so "9" becoming "10" does not reflow the line. The count itself is patched in place on the post's record by id; only that post re-renders. - **Fonts**: a fallback font with similar metrics keeps rewrapping small when the web font arrives. ## Compensating when a height does change Some changes cannot be pre-sized: an author edits a post, a poll reveals its results, an estimate turns out wrong. The rule depends on where the change happens: - **Below the viewport**: nothing visible moves; just update the positions. - **Inside the viewport, caused by the reader** (they voted, they expanded a post): let it grow; the reader expects it. - **Above the viewport**: shift the scroll position by the change, so the post the reader is looking at stays exactly where it was. **Scroll anchoring** is the browser's version of that last rule: in browsers that support it, the scroll position is adjusted to minimise shifts when content above the visible area changes size, and `overflow-anchor: none` opts an element out of being an anchor. A feed built on `content-visibility` and ordinary document flow can lean on it where it is supported. A windowed list that positions posts itself from its own height table usually anchors itself — record the first visible post and its offset, apply the changes, scroll so that post is back at the same offset — and then must make sure the browser and the list never both compensate for the same change; opting the list's container out with `overflow-anchor: none` is one way. ## Edits and deletions - **An edited post** is re-measured and compensated like any other change above or inside the viewport. - **A deleted post far above or below** is removed, and its height is subtracted from the positions (compensated if above). - **A deleted post on or near the screen** becomes a "This post was removed" row at the same height, removed later when it is far from the reader. The feed pattern's guidance is to avoid inserting or removing articles in the middle of a feed; a tombstone keeps every later post in place and keeps the positions screen readers announce stable. ## What interviewers listen for - Measured heights stored by id, not re-estimated every time. - Pre-sizing media from dimensions the API returns, so most jumps never happen. - Compensating only for changes above the viewport. - One owner for scroll compensation, not two. - A tombstone rather than a hole for deletions near the reader.
- The API cannot send image dimensions for older posts. What is the fallback?Render older posts' photos in a fixed-ratio box chosen by the layout (a square or 4:3 crop) and fit the image inside it. The box is wrong for some photos but never changes height when the image arrives. Backfilling dimensions on the server later removes the compromise without any client change.
- Why store measured heights by post id instead of by position in the list?Positions shift whenever posts are added, removed or deduplicated, so a height stored by position ends up attached to the wrong post. An id stays with its post, so the measurement survives new pages, refreshes and deletions, and a scrolled-past post keeps its true height.
- A change above the viewport cannot move what the reader sees, so it needs no handling.
- Lazy-loading photos prevents the jump when they finish loading.
- Remove a deleted post immediately, even while the reader is looking at it.
- Re-estimate every height on each render rather than remembering measurements.
- Let both the browser and the list compensate the same height change.