Difference between width set to 100% vs 100vw vs auto

Hello, can someone explain what is the difference to width property in css when we use 100%, 100vw and auto as values? 100vw is always relative to viewport width? 100% is always relative to parent container? What about auto?
11 Replies
ἔρως
ἔρως4mo ago
100% accounts for the scrollbar 100vw ignores the scrollbar if you set something to 100vw, and the scrollbar is visible, it will cause overflow since the visible area is smaller than 100vw that wont happen with 100%
Faker
FakerOP4mo ago
ah ok didn't know that it was possible
ἔρως
ἔρως4mo ago
it is because it's the viewport size, and the viewport has the scrollbars inside try it
Faker
FakerOP4mo ago
yep will try it will come back yeahh didn't know that, basically, technically viewport is what we see on the screen but it also includes the scrollbars ?
ἔρως
ἔρως4mo ago
yup just imagine that the scrollbar belongs to the <body> or <html> element
Faker
FakerOP4mo ago
yep I see
ἔρως
ἔρως4mo ago
many forget this tiny detail and end up having weird results
Faker
FakerOP4mo ago
yep
Kevin Powell
Kevin Powell4mo ago
I will add that auto (on a block element) will fill the available space, even if it has a margin on the left or right. With percentage, that's added on top, so if you have something like
.example {
width: 100%;
margin-left: 50px;
}
.example {
width: 100%;
margin-left: 50px;
}
this will overflow by 50px. If you change it to auto, it won't overflow.
ἔρως
ἔρως4mo ago
that is also important to keep in mind
Faker
FakerOP4mo ago
Oh ok noted, thanks !

Did you find this page helpful?