Javascript scrollIntoView() middle alignment?

Dhruv Bansal picture Dhruv Bansal · Jan 19, 2012 · Viewed 76.1k times · Source

Javascript .scrollIntoView(boolean) provide only two alignment option.

  1. top
  2. bottom

What if I want to scroll the view such that. I want to bring particular element somewhere in middle of the page?

Answer

hakuna picture hakuna · May 21, 2018

try this :

 document.getElementById('myID').scrollIntoView({
            behavior: 'auto',
            block: 'center',
            inline: 'center'
        });

refer here for more information and options : https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView