Tip 8/72

Dynamic Viewport Height (dvh)

The `dvh` Unit: A Better Alternative to `vh` for Viewport Heights

Traditionally, when working with full-screen elements, developers use screen(vh) or the h-screen utility in TailwindCSS to set an element’s height to the viewport height. However, this approach doesn’t account for dynamic changes like mobile browser toolbars, which can alter the effective viewport height.

Enter the dvh (Dynamic Viewport Height) unit — a modern and more reliable solution.

What is dvh?

The dvh unit dynamically adjusts to changes in the viewport height caused by interface elements like mobile browser toolbars or the appearance of soft keyboards. Unlike vh, which is static, dvh ensures your layout adapts in real time.

In TailwindCSS, you can use the dvh unit like this:

<div class="h-dvh bg-blue-500"></div>

Potential issue with vh: On mobile browsers, the element may extend beyond the visible area when the toolbar or keyboard appears.

Why Use h-dvh Over h-screen?

  • Responsive to Dynamic Changes: Adjusts to browser UI changes, such as hiding or showing mobile toolbars.
  • Improves User Experience: Prevents content from being hidden or incorrectly sized on devices with variable viewport heights.
  • Future-Proof: Aligns with modern CSS standards and browser capabilities.

Final Takeaway

The dvh unit is a game-changer for creating layouts that respond to dynamic viewport changes, especially on mobile devices. Using dvh in your TailwindCSS projects ensures a smoother and more reliable user experience across all devices. Transition from vh to dvh for future-proof, adaptive designs!