Sub topic 4
The scroll container, the spacer, and positioned rows
This chapter assumes you know React function components and props, and basic CSS positioning (relative, absolute) and overflow. Here you will build the three-layer DOM structure that every virtual list relies on. Computing which rows to render comes in the next chapter.
The problem the structure solves
A virtual list renders only a few dozen rows, but the user must still see a scrollbar sized for thousands. The scroll thumb size and position come from the browser's layout, not from React. So the DOM has to look as if all rows exist, even though most do not.
Three elements achieve this:
- Scroll container. A fixed-height element with
overflow: auto. It is the viewport the user scrolls. - Spacer. A single child with
position: relativeand a height equal to the full list (items.length * rowHeight). It makes the scrollbar behave as if every row were rendered. - Positioned rows. The few rows you do render, each with
position: absoluteand atopoffset inside the spacer.
Why each layer is needed
The scroll container sets how much the user sees at once. Without a fixed height, it grows with its content and never scrolls.
The spacer gives the container something tall to scroll. Absolutely positioned children do not add to their parent's height, so without an explicit height the container would have nothing to scroll. The spacer is also position: relative, which makes it the reference point for the rows' top values.
Each row's top is its index times the row height. Row 40 with a 40px row height sits at top: 1600, wherever the scroll position is. Rows are not moved as you scroll. The browser scrolls the spacer, and the rows you render simply sit at the correct offsets within it.
A static version
This component renders a fixed slice of rows and has no scroll logic. It shows the structure on its own.
type FrameProps = {
items: string[];
height: number;
rowHeight: number;
start: number;
count: number;
};
export function Frame({ items, height, rowHeight, start, count }: FrameProps) {
const end = Math.min(items.length, start + count);
const rows = [];
for (let i = start; i < end; i++) {
rows.push(
<div
key={i}
style={{
position: "absolute",
top: i * rowHeight,
left: 0,
right: 0,
height: rowHeight,
}}
>
{items[i]}
</div>
);
}
return (
<div style={{ height, overflow: "auto" }}>
<div style={{ height: items.length * rowHeight, position: "relative" }}>
{rows}
</div>
</div>
);
}With start={40} and count={10}, the DOM holds 10 rows, yet the scrollbar reflects the full list. The container opens at the top, so you will see blank space until you scroll down to row 40. That blank space shows the layout is correct. The next chapter replaces the fixed start with a value computed from scroll position.
Common errors
- Forgetting
position: relativeon the spacer. The rows then position against some ancestor further up, and they appear in the wrong place or outside the container. - Giving the container no fixed height. It grows to fit its content, so it never scrolls and the window effect disappears.
- Setting the spacer height to the rendered rows only. The scrollbar then covers a few dozen rows instead of the whole list.
- Positioning rows with
topbut usingmarginor padding on rows. Offsets stop matchingindex * rowHeight. Keep row boxes exactlyrowHeighttall, and put spacing inside the row.
Exercise
A list has 2,000 rows at 30px each, shown in a 300px container. What height should the spacer have, and what top value does row 120 get?
Show answer
The spacer height is 2,000 × 30 = 60,000px. Row 120 gets top: 120 * 30 = 3600 pixels, inside the spacer.