0
0
Svelteframework~20 mins

Dimension bindings (clientWidth, clientHeight) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dimension Binding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte component when resizing the window?

Consider this Svelte component that binds clientWidth and clientHeight to a div. What will be displayed inside the <main> element after resizing the browser window?

Svelte
  <script>
    let width;
    let height;
  </script>

  <main bind:clientWidth={width} bind:clientHeight={height} style="border: 2px solid black; padding: 1rem;">
    <p>Width: {width}</p>
    <p>Height: {height}</p>
  </main>
AThe <main> element shows undefined for width and height because clientWidth and clientHeight are not valid bindings.
BThe <main> element shows the width and height of the entire document, not the <main> element.
CThe <main> element shows the width and height of the <main> element itself, updating on resize.
DThe <main> element shows the current width and height of the viewport window.
Attempts:
2 left
💡 Hint

Think about what clientWidth and clientHeight measure in the DOM.

state_output
intermediate
2:00remaining
What is the value of width after this code runs?

Given this Svelte snippet, what will be the value of width after the component mounts?

Svelte
<script>
  let width = 0;
  let height = 0;
</script>

<div bind:clientWidth={width} bind:clientHeight={height} style="width: 300px; height: 150px;"></div>
A300, because the <div> has a fixed width of 300px.
B0, because the bindings update only after user interaction.
C150, because the height is 150px and width is not set.
DUndefined, because clientWidth binding does not work on <div> elements.
Attempts:
2 left
💡 Hint

Remember that clientWidth reflects the element's inner width in pixels.

📝 Syntax
advanced
2:00remaining
Which option correctly binds both clientWidth and clientHeight in Svelte?

Choose the correct syntax to bind clientWidth and clientHeight of a section element to variables w and h.

A<section bind:clientWidth={w} bind:clientHeight={h}></section>
B<section bind:clientWidth=w bind:clientHeight=h></section>
C<section bind:clientWidth="w" bind:clientHeight="h"></section>
D<section bind:clientWidth={(w)} bind:clientHeight={(h)}></section>
Attempts:
2 left
💡 Hint

Recall how Svelte binds variables using curly braces.

🔧 Debug
advanced
2:00remaining
Why does this Svelte component not update width on resize?

Examine this Svelte component. It binds clientWidth but the width variable never updates when resizing the window. What is the cause?

Svelte
<script>
  let width = 0;
</script>

<div bind:clientWidth={width} style="width: 100%; height: 100px; border: 1px solid black;"></div>
AThe variable <code>width</code> is not declared with <code>let</code>.
BThe style must use pixels instead of percentages for clientWidth to update.
CclientWidth binding only works on <main> elements, not <div>.
DThe <div> has 100% width but its parent has no fixed width, so clientWidth does not change.
Attempts:
2 left
💡 Hint

Think about how percentage widths depend on parent elements.

🧠 Conceptual
expert
2:00remaining
What is the main difference between binding clientWidth and using window.innerWidth in Svelte?

Choose the best explanation for the difference between binding clientWidth of an element and reading window.innerWidth in a Svelte component.

A<code>clientWidth</code> includes scrollbar width, but <code>window.innerWidth</code> excludes it.
BBinding <code>clientWidth</code> tracks the size of a specific element, while <code>window.innerWidth</code> gives the viewport width regardless of element size.
CBinding <code>clientWidth</code> works only on block elements, while <code>window.innerWidth</code> works only on inline elements.
D<code>window.innerWidth</code> updates automatically on resize, but <code>clientWidth</code> does not update unless manually refreshed.
Attempts:
2 left
💡 Hint

Think about what each measurement represents in the browser.