Difference between Width:100% and width:100vw?

HtmlCss

Html Problem Overview


I have to fit an iframe in screen height. Obviously, I wanted 100% as in width but, since that doesn't work, I used 100vh. But vh like vw is not exactly 100%. In my laptop through chrome while the 100% width renders perfectly without the need for a horizontal scroll bar, vw has about a centimeter extra.

Html Solutions


Solution 1 - Html

vw and vh stand for viewport width and viewport height respectively.

The difference between using width: 100vw instead of width: 100% is that while 100% will make the element fit all the space available, the viewport width has a specific measure, in this case the width of the available screen, including the document margin.

If you set the style body { margin: 0 }, 100vw should behave the same as 100% (for an element that is a child to body).

Additional notes

Using vw as unit for everything in your website, including font sizes and heights, will make it so that the site is always displayed proportionally to the device's screen width regardless of it's resolution. This makes it super easy to ensure your website is displayed exactly the same in both workstation and mobile.

You can set font-size: 1vw (or whatever size suits your project) in your body CSS and everything specified in rem units will automatically scale according to the device screen, so it's easy to port existing projects and even frameworks (such as Bootstrap that already uses rem as unit for everything) to this concept.

Solution 2 - Html

Havenard's answer doesn't seem to be strictly true. I've found that vw fills the viewport width, but doesn't account for the scrollbars. So, if your content is taller than the viewport (so that your site has a vertical scrollbar), then using vw results in a small horizontal scrollbar. I had to switch out width: 100vw for width: 100% to get rid of the horizontal scrollbar.

Solution 3 - Html

You can solve this issue be adding max-width:

#element {
   width: 100vw;
   height: 100vw;
   max-width: 100%;
}

When you using CSS to make the wrapper full width using the code width: 100vw; then you will notice a horizontal scroll in the page, and that happened because the padding and margin of html and body tags added to the wrapper size, so the solution is to add max-width: 100%

Solution 4 - Html

@Havenard's answer provides the perfect explanation for the question. Adding to that, this provides a visual representation of the difference.

You'll be able to notice the key difference between 100vw and 100% when you have a site with scrollbars and an element that is supposed to fit the entire width of the screen.

Option 1

Below is an example of the same.
All i'm doing in the code below is changing the width of <h1> tag from 100vw to 100% when you hover over it.

body{
  /* margin: 0; */
}

.scroll{
  height: calc(110vh);
}

h1{
  width: 100vw;
  /* width: 100%;*/
  text-align:right;
  outline: 5px solid black
}

h1:hover{
  width: 100%;
}

h1:before{
  content: "100vw "
}

h1:hover:before{
  content: "100% "
}

<div class = "scroll">
  <h1>Width</h1>
</div>

If you run the above code snippet and hover the text, you'll notice 2 things:

  1. the horizontal scrollar disappears
  2. the entire text will be visible to you

Note: after running the above snippet, you can play around with above code in browser devtools to see how it affects the elements


Option 2 (Chrome and Edge)

.scroll{
height: calc(110vh);
display: flex;
align-items: baseline;
}

h1{
width: 100vw;
/* width: 100%; */
text-align:right;
outline: 10px solid black
}

<div class="scroll">
    <h1>Test</h1>
  </div>

Another way to visually see the difference in your own project is by setting a display:flex style to an element with 100vw.

When you highlight this elements in browser devtools, You can notice the a leftward point arrow at the right end of the element. Also you can see than the shading of the highlighted elements spans across the scroll-bar, indicating that it is considering the entire screen-width (including scroll-bar width)

100vw vs 100% visual difference


Other questions, that address similar issue are:

  1. https://stackoverflow.com/questions/23367345/100vw-causing-horizontal-overflow-but-only-if-more-than-one?noredirect=1&lq=1
  2. https://stackoverflow.com/questions/31039979/css-units-what-is-the-difference-between-vh-vw-and?noredirect=1&lq=1
  3. https://stackoverflow.com/questions/30489594/prevent-100vw-from-creating-horizontal-scroll

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalokrajivView Question on Stackoverflow
Solution 1 - HtmlHavenardView Answer on Stackoverflow
Solution 2 - HtmlJames FilbyView Answer on Stackoverflow
Solution 3 - HtmlTahseen AlaaView Answer on Stackoverflow
Solution 4 - HtmlGangulaView Answer on Stackoverflow