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.
Jul 16th, 2020. If you use width: 100vw on a website, there's a good chance the horizontal scrollbar is visible for many users. Setting an element's width to 100vw does tell the browser to span the entire viewport width, but what is considered the viewport? Many assume that width: 100vw is the same as width: 100% .
For example, height: 100%; applied to an element is relative to the size of its parent. In contrast, height: 100vh will be 100% of the viewport height regardless of where the element resides in the DOM.
if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border. So, next time you find yourself setting the width of a block level element to 100% to make it occupy all available width, consider if what you really want is setting it to auto.
First, width define the width of specific element while max-width define the maximum size the element is allow to have.
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
).
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.
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.
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With