100vw causing horizontal overflow, but only if more than one?
As already explained by wf4, the horizontal scroll is present because of the vertical scroll. which you can solve by giving max-width: 100%
.
.box {
width: 100vw;
height: 100vh;
max-width:100%; /* added */
}
Working Fiddle
Prevent 100vw from creating horizontal scroll
Basically the answer is no, if you have a vertical scrollbar there is no way to make 100vw equal the width of the visible viewport. Here are the solutions that I have found for this issue.
warning: I have not tested these solutions for browser support
tl;dr
If you need an element to be 100% width of the visible viewport(viewport minus scrollbar) you will need to set it to 100% of the body. You can't do it with vw units if there is a vertical scrollbar.
1. Set all ancestor elements to position static
If you make sure that all of .box
's ancestors are set to position: static;
then set .box
to width: 100%;
so it will be 100% of the body's width. This is not always possible though. Sometimes you need one of the ancestors to be position: absolute;
or position: relative;
.
Example
2. Move the element outside of non-static ancestors
If you can't set the ancestor elements to position: static;
you will need to move .box
outside of them. This will allow you to set the element to 100% of the body width.
Example
3. Remove Vertical Scrollbar
If you don't need vertical scrolling you can just remove the vertical scrollbar by setting the <html>
element to overflow-y: hidden;
.
Example
4. Remove Horizontal Scrollbar
This does not fix the problem, but may be suitable for some situations.
Setting the <html>
element to overflow-y: scroll; overflow-x: hidden;
will prevent the horizontal scrollbar from appearing, but the 100vw element will still overflow.
Example
Viewport-Percentage Lengths Spec
The viewport-percentage lengths are relative to the size of the
initial containing block. When the height or width of the initial
containing block is changed, they are scaled accordingly. However,
when the value of overflow on the root element is auto, any scroll
bars are assumed not to exist. Note that the initial containing
block’s size is affected by the presence of scrollbars on the
viewport.
It appears that there is a bug because vw units should only include the scrollbar width when overflow is set to auto on the root element. But I've tried setting the root element to overflow: scroll;
and it did not change.
Example
Best solution to remove horizontal scrollbar with img width of 100vw?
The horizontal scroll had come up because this is only an issue on windows. On Mac or Android the scrollbars are placed on top of the content and disappear once you're done scrolling so they don't affect the view width.
If max-width: 100% is the width of the viewport without scrollbars, then you didn't need 100vw in the first place.
You could just have use width: 100% because the element doesn't have any positioned ancestor, so its reference is the body.
A simple solution in your case is by giving max-width: 100%.
HTML would be like:
<div class="box">
.
.
.
</div>
Use this CSS:
html, body {
margin: 0;
padding: 0
}
.box {
width: 100vw;
max-width:100%; // This is very Important.
height: 100vh;
}
Why there is a scroll on x-axis when width is 100vw?
Because setting width to 100vw
will give 100vw
width to the element + any padding or margins which results in overflow
100vw element = 100vw width + padding + margin
which is not the case with 100%;
100% element = 100% width inclusve of margin + padding
Mostly the reason is body
margin. So set body -> margin
to 0
and see it working as 100%.
100vw image inside of 1024px container causing small horizontal scroll
The simplest answer would be to implement overflow-x: hidden;
on your body tag itself. If you have no use case where the site should scroll horizontal then there shouldn't be any issue.
Related Topics
Why Does 'Overflow:Hidden' Prevent 'Position:Sticky' from Working
Position a CSS Background Image X Pixels from the Right
Does Element Width Include Padding
How to Include One CSS File in Another
Css to Prevent Child Element from Inheriting Parent Styles
Defined Edges With Css3 Filter Blur
How Does #Iefix Solve Web Fonts Loading in IE6-IE8
What Does It Mean When a CSS Rule Is Grayed Out in Chrome's Element Inspector
Any Way to Limit Border Length
Parent & Child With Position Fixed, Parent Overflow:Hidden Bug
What Does @Media Screen and (Max-Width: 1024Px) Mean in Css
How to Draw a Circle Sector in Css
Downloading a Google Font and Setting Up an Offline Site That Uses It
Css to Make an Empty Cell'S Border Appear
How to Specify Different Widths for Columns in CSS3
Z-Index Does Not Work in Internet Explorer with PDF in Iframe