Unlocking the Possibilities of scroll-snap-type

My goal is to effectively utilize the scroll-snap-type property


I'm struggling to understand why this code functions as intended

html,
body {
  scroll-snap-type: y mandatory;
}

.child {
  scroll-snap-align: start none;
  border: 3px dashed black;
  height: 100vh;
}
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>

However, the following code does not produce the desired outcome

.slider {
  scroll-snap-type: y mandatory;
}

.child {
  scroll-snap-align: start none;
  border: 1px dashed black;
  height: 100vh;
}
<div class="slider">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
</div>

I have attempted:

  • Adding a height to the slider
  • Changing the overflow-y

But none of these options seem to resolve the issue, and I am unsure why


The objective is to create a page with regular scrolling followed by scroll-snapping

.slider {
  scroll-snap-type: y mandatory;
}

.child {
  scroll-snap-align: start none;
  border: 1px solid black;
  height: 100vh;
}
<h2>Normal Scrolling page</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus odit laudantium esse saepe, minus eos rem! Dolor nobis saepe, exercitationem, quasi est, quia reiciendis excepturi culpa tempora perspiciatis dolorum delectus. Lorem ipsum dolor sit
  amet consectetur adipisicing elit. Repellendus odit laudantium esse saepe, minus eos rem! Dolor nobis saepe, exercitationem, quasi est, quia reiciendis excepturi culpa tempora perspiciatis dolorum delectus. Lorem ipsum dolor sit amet consectetur adipisicing
  elit. Repellendus odit laudantium esse saepe, minus eos rem! Dolor nobis saepe, exercitationem, quasi est, quia reiciendis excepturi culpa tempora perspiciatis dolorum delectus.</p>
<h2>This section below is scroll-snapping ⬇️ (or should be)</h2>
<div class="slider">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
</div>

Answer №1

To prevent the content from overflowing, be sure to include overflow-y: scroll in your CSS for the container.

* { padding: 0; margin: 0}

.slider {
  scroll-snap-type: y mandatory;
  height: 100vh;
  overflow-y: scroll;
}

.child {
  scroll-snap-align: start none;
  border: 1px dashed black;
  height: 100vh;
}
<div class="slider">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
</div>


Additionally, for a smoother scrolling experience on each slide, add scroll-snap-stop: always; to the CSS for the .child elements.

* { padding: 0; margin: 0}

.slider {
  scroll-snap-type: y mandatory;
  height: 100vh;
  overflow-y: scroll;
}

.child {
  scroll-snap-align: start none;
  scroll-snap-stop: always;
  border: 1px dashed black;
  height: 100vh;
}
<div class="slider">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
</div>

Answer №2

Since the scrollbar (or overflown element) in both scenarios is <body>

By placing the scrollbar on .container, it alters the behavior:

.container {
  scroll-snap-type: y mandatory;
  max-height: 150px;
  overflow: auto;
}

.item {
  scroll-snap-align: start none;
  border: 2px dotted gray;
  height: 90vh;
}
<div class="container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item">D</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switching the cursor to the following input after a paste event using JQuery

I am currently working on an HTML form that consists of multiple input boxes. I'm looking to focus on the next input box after pasting content into one of them. Below is the code snippet I have been using: $("input").bind('paste', function( ...

What causes the button to move down when adjusting the font in a textarea?

I'm intrigued by the reasons behind this css snippet: select, input, textarea, button { font:99% sans-serif; } that causes the 'submit' button to be pushed below the textarea and positioned at the bottom left corner on this particular ...

Show a clear box over an HTML table row upon mouseover, without highlighting the row

I'm looking to implement a transparent box, with a button, that surrounds a table row when the user hovers over it. I've searched on Google but all the pages only talk about how to highlight rows on hover. I am currently using JavaScript to add ...

What is the best way to specifically target header elements within article elements using a CSS selector?

As someone who is not well-versed in CSS, I am looking to target specifically the header elements within article elements. I have some understanding of selectors such as #, ., or ,. article header article, header article.header article#header I believe t ...

Tips for implementing SVG in background images on Internet Explorer

Having a similar issue to the one discussed in this thread. My background images are functioning properly on all browsers except for versions lower than IE10. After reading through that post, I created a background image in SVG format specifically for IE10 ...

Content is not centered with Bootstrap's flexbox

The alignment of content is not centered using bootstrap flex <div class="d-flex flex-row bd-highlight mb-3"> <div class="p-2 bd-highlight text-left"><i class="fa fa-chevron-left" aria-hidden="true"></i></div> <div cla ...

How can one HTML form be used to request a unique identifier from the user, retrieve a record from the database, parse it into JSON format, and then populate an HTML page form using the

As someone who isn't an expert in any specific coding language, I have a knack for piecing things together to make them work. However, my current challenge is stretching my technical abilities a bit too far. Here's what I'm trying to achieve ...

What method sits snugly between prepend() and append()?

Just a quick question I have. I am attempting to align an element with my target. Usually, we use prepend (before the target) and append (after the target). Is there something similar that allows us to position that element directly on top of what we are ...

What is the best way to hide the input field when there are multiple parent classes present?

I am currently implementing dynamic fields with jQuery and everything is functioning correctly. The problem arises when I attempt to remove these fields. After searching on Google and browsing past questions on StackOverflow, I noticed that everyone seems ...

What is the best way to extract a specific year and month from a date in a datatable using

My dataset includes performance scores of employees across various construction sites. For instance, on 2020-03-15, ALI TAHIRI was at the IHJAMN site. I need to filter this dataset by year and month. If I input 2020 for the year and 03 for the month, the d ...

I am attempting to make the fade in and out effect function properly in my slideshow

I've encountered an issue where the fading effect only occurs when the page initially loads and solely on the first image. Subsequently, the fading effect does not work on any other images displayed. This is the CSS code I have implemented by adding ...

Is it possible to apply a margin to <details><summary> elements?

I am struggling with adding a margin to each of the children in my nested <details> elements .container { margin: 20px; } .parent, .child { outline: none; border: none; user-select: none; cursor: pointer; } <div class="container"> ...

Expand the <div> by clicking on it, then hover away to return it to its normal size

One interesting feature I have on my website is a <div> that expands when clicked, and returns to normal size with another click. However, I am looking for something a bit different... What I want is for the <div> (with the class name .topHead ...

Exploring the integration of PostgreSQL into JavaScript

As a beginner in JavaScript, I am looking to create a web page that can store data in a database. Can anyone provide guidance on what resources or materials I should study to learn more about this process? ...

"Exclusive Mui sx styles will be applied only when a specific breakpoint

As I update my old mui styling to utilize the sx attribute, I've noticed the ability to specify styles for different breakpoints using sx = {{ someCssProp: { xs: ..., md: ... and so on. Prior to this, I had been using theme.breakpoints.only for some ...

Another drop-down is hiding the bootstrap-select drop-down from view

What could be causing some parts of the first drop-down menu to be hidden by another drop-down menu below in the code snippet provided? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv= ...

Error: Angular encountered an unexpected token in the syntax

I am encountering an issue while trying to incorporate a user's profile updater on my website. Whenever I attempt to access a user's profile on the site, I run into this Angular error: main.ts:6 ERROR SyntaxError: Unexpected token 'e', ...

What steps can I take to ensure that this list remains fixed at the bottom of the page?

Looking to have this content positioned at the very bottom of my webpage without any empty space below it. I'm having trouble applying the usual CSS to achieve this. .footer li{ display:inline; } <footer> <div cla ...

Shaky parallax movement

I recently created a website with a parallax effect, but I'm experiencing some performance issues with jittery movement. The page uses CSS3 transform scale to zoom in and out, and automatically resizes on page resize with JavaScript/jQuery. For the ...

Content within iframe is failing to load correctly on both Firefox and Internet Explorer browsers

On my website, I have 4 iframes embedded in 4 different tabs. I've noticed that the content is being cropped when viewed on Firefox, but it loads properly when using Chrome. Strangely, if I manually reload the iframe, the content displays correctly. T ...