Dimension bindings let you easily get the width and height of an HTML element in your Svelte app. This helps you react to size changes without extra code.
Dimension bindings (clientWidth, clientHeight) in Svelte
<div bind:clientWidth={width} bind:clientHeight={height}></div>You bind clientWidth and clientHeight directly to variables.
These variables update automatically when the element's size changes.
<script> let width; let height; </script> <div bind:clientWidth={width} bind:clientHeight={height} style="background: lightblue;"> Width: {width}px, Height: {height}px </div>
clientWidth to track the width of a section element.<script> let width; </script> <section bind:clientWidth={width} style="border: 2px solid black; padding: 1rem;"> The width of this section is {width}px. </section>
clientHeight to get the height of an article element.<script> let height; </script> <article bind:clientHeight={height} style="background: #f0f0f0;"> The height of this article is {height}px. </article>
This component creates a resizable box. The clientWidth and clientHeight bindings update the width and height variables automatically as you resize the box. The current size is shown inside the box.
It uses CSS resize: both so you can drag the corner to change size. The ARIA label helps screen readers understand the box is interactive.
<script> let width = 0; let height = 0; </script> <style> .box { resize: both; overflow: auto; border: 2px solid #007acc; padding: 1rem; width: 200px; height: 150px; background-color: #e0f7ff; } </style> <div class="box" bind:clientWidth={width} bind:clientHeight={height} aria-label="Resizable box"> <p>Resize this box by dragging its corner.</p> <p>Width: {width}px</p> <p>Height: {height}px</p> </div>
Dimension bindings update reactively when the element size changes, no extra event listeners needed.
They work only on elements that can have size (not on void elements like <br>).
Use CSS resize property to allow manual resizing for testing or user control.
Dimension bindings let you track element width and height easily in Svelte.
They update automatically when the element changes size.
Use them to build responsive and interactive UI components that adapt to their own size.