title: Scroll element into view within container, but not in browser window
body: <p>I have a container with tags that line up horizontally. See example below.</p>
  
  <p>The container has <code>overflow:hidden</code> and <code>white-space:nowrap</code>, so the tags can potentially go out to the right forever. I'd like to programatically horizontal scroll to a given tag, provided I have its HTML element in Javascript.</p>
  
  <p>In my test component, I'd been using <code>el.scrollIntoView()</code>, which seemed to be working perfectly. But then I integrated the component into a larger HTML page that scrolled vertically, and I noticed each time I called <code>el.scrollIntoView</code>, it would still scroll into view horizontally within its container, but the whole page would also jump to where the container was right in line with the top of the browser window. It's this second behavior that I obviously don't want.</p>
  
  <p>Is there some way to scroll into view only within a container, and not have the browser window also jump? I'd like to use vanilla JS, if possible.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function scrollToLast() {
    var container = document.getElementById('container');
    var tags = container.getElementsByTagName('span');
    tags[tags.length-1].scrollIntoView()
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    text-align: center;
  }
  
  #container {
    border: 1px solid;
    margin: 100px auto 20px;
    overflow: hidden;
    padding: 10px;
    text-align: left;
    white-space: nowrap;
    width: 75%
  }
  
  #container &gt; span {
    border: 1px solid grey;
    display: inline-block;
    margin-right: 5px;
    padding: 3px;
  }
  
  button {
    margin-bottom: 100px;
  }
  
  .extra-content-on-page:before {
    content: 'Simulates more content';
  }
  
  .extra-content-on-page {
    background: #f2f2f2;
    height: 100px;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  
  &lt;div id="container"&gt;
    &lt;span&gt;Tag 1&lt;/span&gt;
    &lt;span&gt;Tag 2&lt;/span&gt;
    &lt;span&gt;Tag 3&lt;/span&gt;
    &lt;span&gt;Tag 4&lt;/span&gt;
    &lt;span&gt;Tag 5&lt;/span&gt;
    &lt;span&gt;Tag 6&lt;/span&gt;
    &lt;span&gt;Tag 7&lt;/span&gt;
    &lt;span&gt;Tag 8&lt;/span&gt;
    &lt;span&gt;Tag 9&lt;/span&gt;
    &lt;span&gt;Tag 10&lt;/span&gt;
    &lt;span&gt;Tag 11&lt;/span&gt;
    &lt;span&gt;Tag 12&lt;/span&gt;
    &lt;span&gt;Tag 13&lt;/span&gt;
    &lt;span&gt;Tag 14&lt;/span&gt;
    &lt;span&gt;Tag 15&lt;/span&gt;
    &lt;span&gt;Tag 16&lt;/span&gt;
    &lt;span&gt;Tag 17&lt;/span&gt;
    &lt;span&gt;Tag 18&lt;/span&gt;
    &lt;span&gt;Tag 19&lt;/span&gt;
    &lt;span&gt;Tag 20&lt;/span&gt;
    &lt;span&gt;Tag 21&lt;/span&gt;
    &lt;span&gt;Tag 22&lt;/span&gt;
    &lt;span&gt;Tag 23&lt;/span&gt;
    &lt;span&gt;Tag 24&lt;/span&gt;
    &lt;span&gt;Tag 25&lt;/span&gt;
    &lt;span&gt;Tag 26&lt;/span&gt;
    &lt;span&gt;Tag 27&lt;/span&gt;
    &lt;span&gt;Tag 28&lt;/span&gt;
    &lt;span&gt;Tag 29&lt;/span&gt;
    &lt;span&gt;Tag 30&lt;/span&gt;
    &lt;span&gt;Tag 31&lt;/span&gt;
  &lt;/div&gt;
  
  &lt;button onClick="scrollToLast()"&gt;Scroll to Tag 31&lt;/button&gt;
  
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;
  &lt;div class="extra-content-on-page"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>