How to fix scrollIntoView() causing the whole page to move with JavaScript?

Sometimes, we want to fix scrollIntoView() causing the whole page to move with JavaScript.

In this article, we’ll look at how to fix scrollIntoView() causing the whole page to move with JavaScript.

How to fix scrollIntoView() causing the whole page to move with JavaScript?

To fix scrollIntoView() causing the whole page to move with JavaScript, we can call scrollIntoView with a few options.

For instance, we write

element.scrollIntoView({
  behavior: "smooth",
  block: "nearest",
  inline: "start",
});

to call scrollIntoView with an object that jumps to the nearest block element with block set to 'nearest'.

And we jump to the start of the inline element with inline set to 'start'.

Conclusion

To fix scrollIntoView() causing the whole page to move with JavaScript, we can call scrollIntoView with a few options.