<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rou Shan</title>
    <description>The latest articles on DEV Community by Rou Shan (@roushannn).</description>
    <link>https://dev.to/roushannn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1494322%2Fba855feb-b53b-4df0-8fb6-d77fea60c98f.png</url>
      <title>DEV Community: Rou Shan</title>
      <link>https://dev.to/roushannn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roushannn"/>
    <language>en</language>
    <item>
      <title>Understanding the different CSS viewport units (dvh, svh, lvh)</title>
      <dc:creator>Rou Shan</dc:creator>
      <pubDate>Sun, 26 May 2024 08:23:25 +0000</pubDate>
      <link>https://dev.to/roushannn/understanding-the-different-css-viewport-units-dvh-svh-lvh-9eo</link>
      <guid>https://dev.to/roushannn/understanding-the-different-css-viewport-units-dvh-svh-lvh-9eo</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;While working on my current project, I received a request from the client mentioning the masthead is too tall which ended up covering the content when user is viewing the pages on shorter screens.&lt;br&gt;
This is an important concern as it affects accessibility for all users so we got to work on it immediately (by that I meant adding the ticket to the next sprint)!&lt;/p&gt;

&lt;p&gt;So I changed the height of the masthead component from the original implementation of 2/3 of viewport's height to 50% of viewport's height:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Old implementation
height: 66.7%;
// New implementation
height: 50vh;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, when I did this change and checked it on my localhost on&lt;br&gt;
both desktop and mobile view, it did not resemble 50% of the screen height. Instead, it seemed closer to 60% of viewport's height. So I decided to investigate more into this issue, and alas, I came across an article about &lt;em&gt;dynamic viewport height&lt;/em&gt;. &lt;br&gt;
After reading the article, I had a much better understanding on why the masthead was not appearing as I had thought it should. &lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 1
&lt;/h2&gt;

&lt;p&gt;For regular viewport height, &lt;code&gt;100vh&lt;/code&gt; would meant from the bottom of the top toolbar to the bottom of the screen while a dynamic viewport height at &lt;code&gt;100dvh&lt;/code&gt; would mean the height of the content viewable not being blocked by the top and bottom toolbars as depicted in this referenced image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://university.webflow.com/lesson/small-large-and-dynamic-viewport-units?topics=layout-design" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fns5er4iiwz5oya1l4i8t.png" alt="Comparison between 100vh and 100dvh" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I immediately went to test it out and indeed, the masthead image now appeared smaller than when i used &lt;code&gt;50vh&lt;/code&gt;. However, to my horror, as I scrolled down the page, the masthead jumped. I found that this is due to the toolbar minimizing as the user scrolls down on the browser and since I'm using dynamic viewport height units, the viewable content height changes as the user scrolls. Hence, causing this unusual effect. So, I decided to explore the other viewport units.&lt;/p&gt;

&lt;p&gt;On Safari browser, the image jumps to it's new height after scrolling as the bottom toolbar hides:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz3689kv0w6zajcvcinj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frz3689kv0w6zajcvcinj.gif" alt="Jumping effect on Safari" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/dliemowfd/image/upload/v1716399673/dvh_safari_5_vlcsw5.gif" rel="noopener noreferrer"&gt;Link to image&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On Chrome browser, the image stretches to it's new height when scrolled as the bottom toolbar hides:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkp5ui40ytmoylhgv6mp.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvkp5ui40ytmoylhgv6mp.gif" alt="Stretching effect on Chrome" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/dliemowfd/image/upload/v1727633825/dvh_chrome_2_c0kaoz_compressed_waagzh.gif" rel="noopener noreferrer"&gt;Link to image&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2
&lt;/h2&gt;

&lt;p&gt;There are several viewport height units available to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;dvh&lt;/code&gt;: Dynamic Viewport Height&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;svh&lt;/code&gt;: Small Viewport Heigh&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lvh&lt;/code&gt;: Large Viewport Height&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;vh&lt;/code&gt;: Viewport Height&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are also true for viewport width!&lt;br&gt;
With &lt;code&gt;svh&lt;/code&gt;, it refers to the bottom of top toolbar to the top of the bottom toolbar while &lt;code&gt;lvh&lt;/code&gt; refers to the top of the top toolbar to the bottom of the bottom toolbar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/pulse/understanding-dvh-lvh-svh-css-prabhath-senadheera-wmvkc/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosrdsgbdkf0qbjttguvv.png" alt="Comparison between 100svh, 100lvh and 100dvh" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this newfound understanding, I decided to go with &lt;code&gt;svh&lt;/code&gt; and indeed, the jumping bug no longer exists since &lt;code&gt;svh&lt;/code&gt; remains consistent and the client is also satisfied with this height improvement! &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In the end, deciding which viewport units to use ultimately depends on your requirements but do be careful when using &lt;code&gt;dvh&lt;/code&gt; if you have any height dependent elements on your page. &lt;/p&gt;

&lt;p&gt;This purpose of this post is to document my thought process while resolving a bug and also the research put into it. Since this is also my first post, I would love to know any feedback and points that I can improve on, thank you!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>css</category>
    </item>
  </channel>
</rss>
