Elements · @vueuse/core v15.0.0 · Reviewed
useElementSize
Read an element’s width and height reactively when its container changes size.
Edit the code and the preview updates as you type.
What it does
A chart or card needs to adapt to its actual panel width, which can change independently of the browser window.
The playground is a small, runnable example. Change the code and try the interaction before copying it into an app.
The code
const panel = ref(null)
const { width, height } = useElementSize(panel)
// Base a canvas or chart layout on the measured container.Where you'd use it
- Charts in resizable dashboard panels
- Responsive editor sidebars
- Elements whose dimensions drive a canvas
In Nuxt (and other gotchas)
The ref is null during SSR and the first render, so dimensions begin at their initial values. Keep initial markup independent of measured dimensions or defer size-specific rendering until mount.
When not to use it
Use CSS container queries when only styling changes. Measuring in JavaScript is useful when behavior or drawing calculations need the actual dimensions.
Links
- Official useElementSize docs for every option and type
- Source code on GitHub