# useElementSize (/docs/hooks/use-element-size)





`useElementSize` returns an element's `{ width, height }` in pixels and keeps it current as the element is resized. Reach for it when a component has to compute something from its own size: a column count, how many items fit before a "+N", a canvas resolution. `Gallery`'s masonry columns and the pill controls' overflow counter both measure this way.

```tsx
import { useElementSize } from '@42-central/ui-react';
import { useRef } from 'react';

const ref = useRef<HTMLDivElement>(null);
const size = useElementSize(ref);

<div ref={ref}>{size ? `${size.width} × ${size.height}` : null}</div>;
```

If you only need to restyle something at a size threshold, a CSS container query does it without JavaScript. If you need a boolean for a width threshold, [`useContainerQuery`](/docs/hooks/use-container-query) gives you one directly.

## Usage [#usage]

Drag the box below to resize it. The content box is what's left inside the padding and border; the border box includes them.

<Preview code="&#x22;const ref = useRef(null);\nconst content = useElementSize(ref);\nconst border = useElementSize(ref, { box: 'border' });\n\n<div ref={ref} className=\&#x22;resize overflow-auto border p-4\&#x22;>\n  content {content?.width} × {content?.height}\n  border {border?.width} × {border?.height}\n</div>&#x22;">
  <ElementSizeDemo />
</Preview>

## Server rendering [#server-rendering]

The hook returns `undefined` on the server and until the first observation. A server can't know the element's size, so `undefined` is the only value both sides agree on during hydration. `ResizeObserver` reports the current size as soon as it starts observing, so the real value arrives right after mount. Render a fallback for `undefined`, the layout you'd show at an unknown width.

## Content box or border box [#content-box-or-border-box]

The default `box: 'content'` measures inside the padding and border. It's the box a CSS size container query measures, and the one to use when you lay out children. `box: 'border'` includes the padding and border: use it when you place the element itself, such as stacking cards by their outer height. The observer watches the box you pick, so with `'content'` a padding-only change doesn't update the size.

Sizes are physical. In a vertical writing mode, `width` is still the horizontal extent.

## Don't measure what you set [#dont-measure-what-you-set]

Don't feed the size back into the measured element's own size: if a padding or a width depends on the measurement, the measurement changes, and the layout can oscillate. Measure a wrapper and size its children instead. CSS container queries follow the same rule: a query can't style its own container.

## Measuring conditionally [#measuring-conditionally]

`enabled: false` mounts no observer and returns `undefined`. Use it when only some configurations need a measurement, like `Gallery`, which observes only for responsive masonry columns.

```tsx
const size = useElementSize(ref, { enabled: layout === 'masonry' });
```

The ref is read when the hook's effect runs, so the element must be mounted in the same render that mounts the hook or turns `enabled` on.

## API [#api]

<TypeTable
  type="{
  ref: {
    type: 'RefObject<Element | null>',
    required: true,
    description: 'Ref of the element to measure.',
  },
  'options.box': {
    type: &#x22;'content' | 'border'&#x22;,
    default: &#x22;'content'&#x22;,
    description: 'The box to measure: inside the padding and border, or including them.',
  },
  'options.enabled': {
    type: 'boolean',
    default: 'true',
    description: 'Observe only while true. When false, no observer is mounted and the hook returns undefined.',
  },
}"
/>

Returns `{ width: number; height: number } | undefined`, in CSS pixels. A resize that leaves both numbers unchanged returns the same object, so memos keyed on it don't recompute.
