Recently, I’ve noticed that using 100vw adds extra space to the right of the viewport and additionally a scrollbar to visit that extra space. It doesn’t happen with 100% so that’s what I’m currently using.
Why does this happen? I’d like to know what the under the hood implementations of the vw and vh tags are and if they really are better for responsive design.
Top comments (5)
This happens because 100% will make the element fit into it's contained or available space.
100vw will attempt to fit the available screen including the document margin, and so you'll need to remove the body margin:
body { margin: 0; }
100vh will behave the same way.
Problem solved.
Interesting. Very simple solution. Thanks. In that case, is 100vw a better responsive solution than %?
I don't think there is a right or wrong answer to that question. I've always used 100% without any dramas, but I'd imagine that if I changed all instances of 100% to 100vw it would be fine.
If i dont specify my body to 100vh and 100vw why some width: 100%; and height: 100%; wont work?
Is there a way to make my body always 100% height and 100% width?
You've probably got padding or margins on the element involved (or it's parents), and those have to be rendered as well, so they cause spill-over off the side of the viewport.
The only other implementation detail likely to cause you any grief with these is that they shouldn't include the width/height of any scrollbar. This means that:
vw
unit to avoid the possibility of that bouncing behavior.As a general rule, I'd recommend against using either percentages or viewport units when you can avoid it for responsive design. You're much more likely to get consistent results by using REM's for sizing everything and changing sizes based on media queries (for example, like Bootstrap does for it's various responsive elements).