CSS Scroll Snapping, vertical not working
The major problem in the code snippet is that the displayed scrollbar belongs to the body, where no scroll-snap properties have been defined. This is why you do not have any snapping behaviour when scrolling.
To achieve your expected result, you need to
- Be sure that the displayed scrollbar belongs to the parent div
- Define the
overflow
behaviour to the parent container toscroll
Below is a working sample
As a note, consider that snapping properties (for Chrome) have evolved, and that you are using deprecated features. See the CSS scroll snap on Google developers.
Note also that this answer deals only with Chrome, without the polyfill part. It is just the main scroll concept that is involved here.
html,
body {
height: 100vh;
overflow: hidden;
}
.parent {
overflow: scroll;
height: 100vh;
scroll-snap-points-y: repeat(100vh);
scroll-snap-type: y mandatory;
}
section {
height: 100vh;
scroll-snap-align: start;
position: relative;
}
.one {
background-color: red;
}
.two {
background-color: blue;
}
.three {
background-color: grey;
}
.four {
background-color: green;
}
<div class="parent row">
<section class="one"></section>
<section class="two"></section>
<section class="three"></section>
<section class="four"></section>
</div>
How to correctly setup Vertical scroll snap css
Just reading the Mozilla docs on this makes it seem like this feature is highly inconsistent between browsers, but following their guide I managed to make your snippet work with these changes:
body .content {
height: 100vh; /* was 100% */
overflow: auto; /* added */
}
body {
width: 100%;
margin: 0;
background-color: white;
}
body .content {
width: 100vw;
height: 100vh;
scroll-snap-points-y: repeat(300px);
scroll-snap-type: y mandatory;
scroll-snap-type: mandatory;
overflow: auto;
}
body .content section {
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
border: 1px solid black;
scroll-snap-align: start;
}
<div class='content'>
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
</div>
Scroll-snap not working. I have looked up the existing questions and none of them helped
There are two mistakes you made and there are some improvements needed in your CSS
Do not apply
scroll-snap-type
to the body tag, rather make a container and apply this property to the container.scroll-snap-align
does not work on the parent container, it works on a child only
Improvements:
- Added
scroll-snap-type: y mandatory;
so, it will vertically snap the elements - Set
overflow-y: scroll
to make vertical scrolling possible - Also set max-height to the container.
So I tried it on codepen here's CSS:
.container {
scroll-snap-type: y mandatory;
overflow-y: scroll;
max-height: 100vh
}
img {
height: 100vh;
scroll-snap-align: start;
}
Here's the working pen link
I hope this clears your query.
No scroll snap with scroll-snap-type and -align / Simple Example not working Chrome/Firefox /
As it turns out, scroll-snap-type
requires that you define a height for the container. I am not exactly sure how to explain why it is required, however, defining a height
does take care of the issue.
.img {
margin: 10px 5px;
scroll-snap-align: center;
-webkit-overflow-scrolling: touch;
}
.container {
display: grid;
justify-content: center;
grid-gap: 10px;
scroll-snap-type: y mandatory;
overflow-y: scroll;
height: 100vh;
}
Scroll-snapping Dosent work when i have scroll-snap-type: y; on body tag
I'd try to make sure all elements inside the scroll-snap are the same, ie <div>[lots of other divs]</div>, alternatively, try not to use the body as a wrapper and see if that helps.
Here is a doc with better explanation
Edit: I tested it out and the size of the wrapper needs to be the same size as its children, you can disregard the rest of the stuff I said but I still wouldn't suggest using the body as a wrapper next time.
Related Topics
Elastic Easing in CSS3, Best Approach
How to Tell Which Font Chrome Is Using
What's the Difference Between a Block-Level Box and a Principal Block-Level Box
Firefox CSS Animation Smoothing (Sub-Pixel Smoothing)
Colspan Messes with Fixed Width Table
Target IE9 or IE8 But Not Both Using CSS
How to Change the Button Color When It Is Active Using Bootstrap
How to Simply Parse a CSS Like (!) File in My Qt Application
Twitter Bootstrap 3.0 Logo in the Center of Navbar
Font Face Not Working in IE8 as Expected
Center Output (Plots) in the Notebook
CSS Transition from 'Display: None' on Class Change
Best Way to Use Media Queries for Mobile Designs
Height: 100Vh; and Overflow "Content" on Smaller Screens
Sass Mixin for Animation Keyframe Which Includes Multiple Stages and Transform Property
Chrome 83: Change Colors of New Form Styling
How to Convert a Numeric Value into a Percentage (Or) Append Percentage Symbol to a Number