What are the bare necessities for CSS snap scroll to work?











up vote
3
down vote

favorite












I've been trying to implement a super simple horizontal snap scroll (as shown here: https://css-tricks.com/practical-css-scroll-snapping ) yet I am consistently failing at that. I've probably searched through all the questions and answers here but none was of help.



My code is very simple, I am only declaring scroll-snap-type: y mandatory; on the parent and scroll-snap-align: center; on the child. And I am pretty sure it's not a browser issue (since I've tried it with many different browsers). What am I missing here? Or what do I not understand?



Here's my (not working) CodePen: https://codepen.io/anon/pen/XyNNGY



html:



<div class='parent'>
<div class='child'>Section 1</div>
<div class='child two'>Section 2</div>
<div class='child'>Section 3</div>
</div>


css:



  body {
margin: 0;
}

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

.child {
scroll-snap-align: center;
width: 100vw;
height: 100vh;
background-color: pink;
}

.two {
background-color: crimson;
}


Thanks a million already.










share|improve this question




























    up vote
    3
    down vote

    favorite












    I've been trying to implement a super simple horizontal snap scroll (as shown here: https://css-tricks.com/practical-css-scroll-snapping ) yet I am consistently failing at that. I've probably searched through all the questions and answers here but none was of help.



    My code is very simple, I am only declaring scroll-snap-type: y mandatory; on the parent and scroll-snap-align: center; on the child. And I am pretty sure it's not a browser issue (since I've tried it with many different browsers). What am I missing here? Or what do I not understand?



    Here's my (not working) CodePen: https://codepen.io/anon/pen/XyNNGY



    html:



    <div class='parent'>
    <div class='child'>Section 1</div>
    <div class='child two'>Section 2</div>
    <div class='child'>Section 3</div>
    </div>


    css:



      body {
    margin: 0;
    }

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

    .child {
    scroll-snap-align: center;
    width: 100vw;
    height: 100vh;
    background-color: pink;
    }

    .two {
    background-color: crimson;
    }


    Thanks a million already.










    share|improve this question


























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I've been trying to implement a super simple horizontal snap scroll (as shown here: https://css-tricks.com/practical-css-scroll-snapping ) yet I am consistently failing at that. I've probably searched through all the questions and answers here but none was of help.



      My code is very simple, I am only declaring scroll-snap-type: y mandatory; on the parent and scroll-snap-align: center; on the child. And I am pretty sure it's not a browser issue (since I've tried it with many different browsers). What am I missing here? Or what do I not understand?



      Here's my (not working) CodePen: https://codepen.io/anon/pen/XyNNGY



      html:



      <div class='parent'>
      <div class='child'>Section 1</div>
      <div class='child two'>Section 2</div>
      <div class='child'>Section 3</div>
      </div>


      css:



        body {
      margin: 0;
      }

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

      .child {
      scroll-snap-align: center;
      width: 100vw;
      height: 100vh;
      background-color: pink;
      }

      .two {
      background-color: crimson;
      }


      Thanks a million already.










      share|improve this question















      I've been trying to implement a super simple horizontal snap scroll (as shown here: https://css-tricks.com/practical-css-scroll-snapping ) yet I am consistently failing at that. I've probably searched through all the questions and answers here but none was of help.



      My code is very simple, I am only declaring scroll-snap-type: y mandatory; on the parent and scroll-snap-align: center; on the child. And I am pretty sure it's not a browser issue (since I've tried it with many different browsers). What am I missing here? Or what do I not understand?



      Here's my (not working) CodePen: https://codepen.io/anon/pen/XyNNGY



      html:



      <div class='parent'>
      <div class='child'>Section 1</div>
      <div class='child two'>Section 2</div>
      <div class='child'>Section 3</div>
      </div>


      css:



        body {
      margin: 0;
      }

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

      .child {
      scroll-snap-align: center;
      width: 100vw;
      height: 100vh;
      background-color: pink;
      }

      .two {
      background-color: crimson;
      }


      Thanks a million already.







      html css scroll-snap-points






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 18:40









      BoltClock

      509k12511451190




      509k12511451190










      asked Nov 12 at 17:29









      selmanbey

      537




      537
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The element you designate as the scroll snap container needs to be the one that the scrollbar is attached to. In your case, the parent element doesn't have a scrollbar — the scrollbar belongs to the viewport and the parent is extending past the size of the viewport without generating its own scrollbar. The scroll-snap-type property should therefore be applied to body (or html), not the parent:






          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer





















          • Thank you for the great explanation! That was really helpful.
            – selmanbey
            Nov 13 at 9:59


















          up vote
          1
          down vote













          Try it like this.






          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer























          • Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
            – selmanbey
            Nov 13 at 9:50











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267227%2fwhat-are-the-bare-necessities-for-css-snap-scroll-to-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          The element you designate as the scroll snap container needs to be the one that the scrollbar is attached to. In your case, the parent element doesn't have a scrollbar — the scrollbar belongs to the viewport and the parent is extending past the size of the viewport without generating its own scrollbar. The scroll-snap-type property should therefore be applied to body (or html), not the parent:






          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer





















          • Thank you for the great explanation! That was really helpful.
            – selmanbey
            Nov 13 at 9:59















          up vote
          2
          down vote



          accepted










          The element you designate as the scroll snap container needs to be the one that the scrollbar is attached to. In your case, the parent element doesn't have a scrollbar — the scrollbar belongs to the viewport and the parent is extending past the size of the viewport without generating its own scrollbar. The scroll-snap-type property should therefore be applied to body (or html), not the parent:






          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer





















          • Thank you for the great explanation! That was really helpful.
            – selmanbey
            Nov 13 at 9:59













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The element you designate as the scroll snap container needs to be the one that the scrollbar is attached to. In your case, the parent element doesn't have a scrollbar — the scrollbar belongs to the viewport and the parent is extending past the size of the viewport without generating its own scrollbar. The scroll-snap-type property should therefore be applied to body (or html), not the parent:






          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer












          The element you designate as the scroll snap container needs to be the one that the scrollbar is attached to. In your case, the parent element doesn't have a scrollbar — the scrollbar belongs to the viewport and the parent is extending past the size of the viewport without generating its own scrollbar. The scroll-snap-type property should therefore be applied to body (or html), not the parent:






          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>





          body {
          margin: 0;
          scroll-snap-type: y mandatory;
          }

          .child {
          scroll-snap-align: center;
          width: 100vw;
          height: 100vh;
          background-color: pink;
          }

          .two {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child two'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 18:45









          BoltClock

          509k12511451190




          509k12511451190












          • Thank you for the great explanation! That was really helpful.
            – selmanbey
            Nov 13 at 9:59


















          • Thank you for the great explanation! That was really helpful.
            – selmanbey
            Nov 13 at 9:59
















          Thank you for the great explanation! That was really helpful.
          – selmanbey
          Nov 13 at 9:59




          Thank you for the great explanation! That was really helpful.
          – selmanbey
          Nov 13 at 9:59












          up vote
          1
          down vote













          Try it like this.






          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer























          • Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
            – selmanbey
            Nov 13 at 9:50















          up vote
          1
          down vote













          Try it like this.






          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer























          • Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
            – selmanbey
            Nov 13 at 9:50













          up vote
          1
          down vote










          up vote
          1
          down vote









          Try it like this.






          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          share|improve this answer














          Try it like this.






          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>








          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>





          body {
          margin: 0;
          }

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

          .child {
          scroll-snap-align: center;
          height: 100vh;
          background-color: pink;
          }

          .child:nth-child(even) {
          background-color: crimson;
          }

          <div class='parent'>
          <div class='child'>Section 1</div>
          <div class='child'>Section 2</div>
          <div class='child'>Section 3</div>
          </div>






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 18:15

























          answered Nov 12 at 17:39









          Carle B. Navy

          768216




          768216












          • Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
            – selmanbey
            Nov 13 at 9:50


















          • Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
            – selmanbey
            Nov 13 at 9:50
















          Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
          – selmanbey
          Nov 13 at 9:50




          Thank you very much! I've accepted @BoltClock's answer for it helped me better to understand how scroll-snap works, but yours was also very helpful in reminding that you can make the parent scrollable (and thus snap-scrollable) by setting a max-height on it. So thanks a lot!
          – selmanbey
          Nov 13 at 9:50


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267227%2fwhat-are-the-bare-necessities-for-css-snap-scroll-to-work%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents