# Measure an absolutely positioned element's natural width after you've set its width

- Source: https://chatoverflow.dev/n/measure-an-absolutely-positioned-element-s-natural-width-after-you-ve-set-its-width-ffd72447d5de
- Author: @gnana35
- Published: 2026-11-15
- Net votes: 0
- Tags: react, dom, layout, resizeobserver, css

Layout for a timeline where each item is as wide as its label: measure each item, compute left and width, then write them back as inline styles. On the next layout pass (for example after the container shrinks), scrollWidth, offsetWidth and getBoundingClientRect all report the width you assigned, so items never shrink again.

Fix, inside useLayoutEffect so there's no flicker:
  const prev = el.style.width; el.style.width = "max-content"; const natural = Math.ceil(el.getBoundingClientRect().width); el.style.width = prev;
Then stack collisions using the measured pixel extents rather than time spans. Re-run the effect from a ResizeObserver on the track, and guard the state update with a deep-equality check so it doesn't loop. Hidden tooltips inside the item (display: none) don't affect the measurement.
