<p>I have a link on a long HTML page. When I click it, I wish a <code>div</code> on another part of the page to be visible in the window by scrolling into view.</p>

<p>A bit like  <code>EnsureVisible</code> in other languages.</p>

<p>I've checked out <code>scrollTop</code> and <code>scrollTo</code> but they seem like red herrings.</p>

<p>Can anyone help?</p>

[{'answer_id': 68168, 'author': 'Mike Becatti', 'author_id': 6617, 'author_profile': 'https://Stackoverflow.com/users/6617', 'pm_score': 2, 'selected': False, 'text': '<p>Why not a named anchor?</p>\n'}, {'answer_id': 68175, 'author': 'mjallday', 'author_id': 6084, 'author_profile': 'https://Stackoverflow.com/users/6084', 'pm_score': 4, 'selected': False, 'text': '<pre><code>&lt;a href="#myAnchorALongWayDownThePage"&gt;Click here to scroll&lt;/a&gt;\n\n&lt;A name=\'myAnchorALongWayDownThePage"&gt;&lt;/a&gt;\n</code></pre>\n\n<p>No fancy scrolling but it should take you there.</p>\n'}, {'answer_id': 68177, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Answer posted <a href="https://stackoverflow.com/questions/66964/how-do-i-create-a-link-to-a-footnote-in-html">here</a> - same solution to your problem.</p>\n\n<p>Edit: the JQuery answer is very nice if you want a smooth scroll - I hadn\'t seen that in action before.</p>\n'}, {'answer_id': 68185, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 5, 'selected': False, 'text': '<p>How about the <a href="http://demos.flesler.com/jquery/scrollTo/" rel="noreferrer">JQuery ScrollTo - see this sample code</a></p>\n'}, {'answer_id': 68188, 'author': 'Scott Swezey', 'author_id': 9439, 'author_profile': 'https://Stackoverflow.com/users/9439', 'pm_score': 2, 'selected': False, 'text': '<p>The property you need is location.hash. For example:<br /><br/>\n     location.hash = \'top\'; //would jump to named anchor "top</p>\n\n<p>I don\'t know how to do the nice scroll animation without the use of dojo or some toolkit like that, but if you just need it to jump to an anchor, location.hash should do it.</p>\n\n<p>(tested on FF3 and Safari 3.1.2)</p>\n'}, {'answer_id': 68197, 'author': 'Benjamin Autin', 'author_id': 1440933, 'author_profile': 'https://Stackoverflow.com/users/1440933', 'pm_score': 0, 'selected': False, 'text': "<p>scrollTop (IIRC) is where in the document the top of the page is scrolled to. scrollTo scrolls the page so that the top of the page is where you specify.</p>\n\n<p>What you need here is some Javascript manipulated styles. Say if you wanted the div off-screen and scroll in from the right you would set the left attribute of the div to the width of the page and then decrease it by a set amount every few seconds until it is where you want.</p>\n\n<p>This should point you in the right direction.</p>\n\n<p>Additional: I'm sorry, I thought you wanted a separate div to 'pop out' from somewhere (sort of like this site does sometimes), and not move the entire page to a section. Proper use of anchors would achieve that effect.</p>\n"}, {'answer_id': 68671, 'author': 'levik', 'author_id': 4465, 'author_profile': 'https://Stackoverflow.com/users/4465', 'pm_score': 3, 'selected': False, 'text': '<p>The difficulty with scrolling is that you may not only need to scroll the page to show a div, but you may need to scroll inside scrollable divs on any number of levels as well.</p>\n\n<p>The scrollTop property is a available on any DOM element, including the document body. By setting it, you can control how far down something is scrolled. You can also use clientHeight and scrollHeight properties to see how much scrolling is needed (scrolling is possible when clientHeight (viewport) is less than scrollHeight (the height of the content).</p>\n\n<p>You can also use the offsetTop property to figure out where in the container an element is located.</p>\n\n<p>To build a truly general purpose "scroll into view" routine from scratch, you would need to start at the node you want to expose, make sure it\'s in the visible portion of it\'s parent, then repeat the same for the parent, etc, all the way until you reach the top.</p>\n\n<p>One step of this would look something like this (untested code, not checking edge cases):</p>\n\n<pre><code>function scrollIntoView(node) {\n  var parent = node.parent;\n  var parentCHeight = parent.clientHeight;\n  var parentSHeight = parent.scrollHeight;\n  if (parentSHeight &gt; parentCHeight) {\n    var nodeHeight = node.clientHeight;\n    var nodeOffset = node.offsetTop;\n    var scrollOffset = nodeOffset + (nodeHeight / 2) - (parentCHeight / 2);\n    parent.scrollTop = scrollOffset;\n  }\n  if (parent.parent) {\n    scrollIntoView(parent);\n  }\n}\n</code></pre>\n'}, {'answer_id': 69042, 'author': 'Josh', 'author_id': 10902, 'author_profile': 'https://Stackoverflow.com/users/10902', 'pm_score': 5, 'selected': False, 'text': "<p>If you don't want to add an extra extension the following code should work with jQuery.</p>\n\n<pre><code>$('a[href=#target]').\n    click(function(){\n        var target = $('a[name=target]');\n        if (target.length)\n        {\n            var top = target.offset().top;\n            $('html,body').animate({scrollTop: top}, 1000);\n            return false;\n        }\n    });\n</code></pre>\n"}, {'answer_id': 69803, 'author': 'Vitaly Sharovatov', 'author_id': 6647, 'author_profile': 'https://Stackoverflow.com/users/6647', 'pm_score': -1, 'selected': False, 'text': '<p>Correct me if I\'m wrong but I\'m reading the question again and again and still think that Angus McCoteup was asking how to set an element to be position: fixed. </p>\n\n<p>Angus McCoteup, check out <a href="http://www.cssplay.co.uk/layouts/fixed.html" rel="nofollow noreferrer">http://www.cssplay.co.uk/layouts/fixed.html</a> - if you want your DIV to behave like a menu there, have a look at a CSS there</p>\n'}, {'answer_id': 71726, 'author': 'Andrey Fedorov', 'author_id': 10728, 'author_profile': 'https://Stackoverflow.com/users/10728', 'pm_score': 1, 'selected': False, 'text': '<p>There is a <a href="http://demos.flesler.com/jquery/scrollTo/" rel="nofollow noreferrer">jQuery plugin</a> for the general case of scrolling to a DOM element, but if performance is an issue (and when is it not?), I would suggest doing it manually. This involves two steps:</p>\n<ol>\n<li>Finding the position of the element you are scrolling to.</li>\n<li>Scrolling to that position.</li>\n</ol>\n<p><a href="http://www.quirksmode.org/js/findpos.html" rel="nofollow noreferrer">quirksmode</a> gives a good explanation of the mechanism behind the former. Here\'s my preferred solution:</p>\n<pre><code>function absoluteOffset(elem) {\n    return elem.offsetParent &amp;&amp; elem.offsetTop + absoluteOffset(elem.offsetParent);\n}\n</code></pre>\n<p>It uses casting from null to 0, which isn\'t proper etiquette in some circles, but I like it :) The second part uses <code>window.scroll</code>. So the rest of the solution is:</p>\n<pre><code>function scrollToElement(elem) {\n    window.scroll(0, absoluteOffset(elem));\n}\n</code></pre>\n<p>Voila!</p>\n'}, {'answer_id': 2368393, 'author': 'futtta', 'author_id': 237449, 'author_profile': 'https://Stackoverflow.com/users/237449', 'pm_score': 9, 'selected': False, 'text': '<p>old question, but if anyone finds this through google (as I did) and who does not want to use anchors or jquery; there\'s a builtin javascriptfunction to \'jump\' to an element;</p>\n\n<pre><code>document.getElementById(\'youridhere\').scrollIntoView();\n</code></pre>\n\n<p>and what\'s even better; according to the great compatibility-tables on quirksmode, this is <a href="http://www.quirksmode.org/dom/w3c_cssom.html#t23" rel="noreferrer">supported by all major browsers</a>!</p>\n'}, {'answer_id': 2421027, 'author': 'Lelando', 'author_id': 290994, 'author_profile': 'https://Stackoverflow.com/users/290994', 'pm_score': 0, 'selected': False, 'text': "<p>I personally found Josh's jQuery-based answer above to be the best I saw, and worked perfectly for my application... of course, I was <em>already</em> using jQuery... I certainly wouldn't have included the whole jQ library just for that one purpose.</p>\n\n<p>Cheers!</p>\n\n<p>EDIT: OK... so mere seconds after posting this, I saw another answer just <em>below</em> mine (not sure if still below me after an edit) that said to use:</p>\n\n<p>document.getElementById('your_element_ID_here').scrollIntoView();</p>\n\n<p>This works perfectly and in so much less code than the jQuery version!  I had no idea that there was a built-in function in JS called .scrollIntoView(), but there it is!  So, if you want the fancy animation, go jQuery.  Quick n' dirty... use this one!</p>\n"}, {'answer_id': 34277133, 'author': 'Swarnendu Paul', 'author_id': 1531473, 'author_profile': 'https://Stackoverflow.com/users/1531473', 'pm_score': 0, 'selected': False, 'text': "<p>For smooth scroll this code is useful</p>\n\n<pre><code>$('a[href*=#scrollToDivId]').click(function() {\n    if (location.pathname.replace(/^\\//,'') == this.pathname.replace(/^\\//,'') &amp;&amp; location.hostname == this.hostname) {\n      var target = $(this.hash);\n      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');\n      var head_height = $('.header').outerHeight(); // if page has any sticky header get the header height else use 0 here\n      if (target.length) {\n        $('html,body').animate({\n          scrollTop: target.offset().top - head_height\n        }, 1000);\n        return false;\n      }\n    }\n  });\n</code></pre>\n"}, {'answer_id': 34788466, 'author': 'funnyfish', 'author_id': 5763789, 'author_profile': 'https://Stackoverflow.com/users/5763789', 'pm_score': 2, 'selected': False, 'text': '<p>I can\'t add a comment to futtta\'s reply above, but for a smoother scroll use:</p>\n\n<pre><code>onClick="document.getElementById(\'more\').scrollIntoView({block: \'start\', behavior: \'smooth\'});"\n</code></pre>\n'}, {'answer_id': 41236967, 'author': 'Xcoder', 'author_id': 1181017, 'author_profile': 'https://Stackoverflow.com/users/1181017', 'pm_score': 3, 'selected': False, 'text': "<p>This worked for me</p>\n\n<pre><code>document.getElementById('divElem').scrollIntoView();\n</code></pre>\n"}, {'answer_id': 49916889, 'author': 'Smelino', 'author_id': 9515260, 'author_profile': 'https://Stackoverflow.com/users/9515260', 'pm_score': 4, 'selected': False, 'text': '<p>You can use <code>Element.scrollIntoView()</code> method as was mentioned above. If you leave it with no parameters inside you will have an <strong><em>instant ugly scroll</em></strong>. To prevent that you can add this parameter -  <code>behavior:"smooth"</code>.</p>\n\n<p>Example:</p>\n\n<pre><code>document.getElementById(\'scroll-here-plz\').scrollIntoView({behavior: "smooth", block: "start", inline: "nearest"});\n</code></pre>\n\n<p>Just replace <code>scroll-here-plz</code> with your <code>div</code> or element on a website. And if you see your element at the bottom of your window or the position is not what you would have expected, play with parameter <code>block: ""</code>. You can use <code>block: "start"</code>, <code>block: "end"</code> or <code>block: "center"</code>. </p>\n\n<p>Remember: Always use parameters inside an object {}.</p>\n\n<p>If you would still have problems, go to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView</a></p>\n\n<p>There is detailed documentation for this method.</p>\n'}, {'answer_id': 69253570, 'author': 'Nagev', 'author_id': 5362795, 'author_profile': 'https://Stackoverflow.com/users/5362795', 'pm_score': 1, 'selected': False, 'text': '<p>As stated already, <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView" rel="nofollow noreferrer">Element.scrollIntoView()</a> is a good answer. Since the question says &quot;I have a link on a long HTML page...&quot; I want to mention a relevant detail. If this is done through a functional link it may not produce the desired effect of scrolling to the target div. For example:</p>\n<p>HTML:</p>\n<pre><code>&lt;a id=&quot;link1&quot; href=&quot;#&quot;&gt;Scroll With Link&lt;/a&gt;\n</code></pre>\n<p>JavaScript:</p>\n<pre><code>const link = document.getElementById(&quot;link1&quot;);\nlink.onclick = showBox12;\n\nfunction showBox12()\n{\n  const box = document.getElementById(&quot;box12&quot;);\n  box.scrollIntoView();\n  console.log(&quot;Showing Box:&quot; + box);\n}\n</code></pre>\n<p>Clicking on <em>Scroll With Link</em> will show the message on the console, but it would seem to have no effect because the <code>#</code> will bring the page back to the top. Interestingly, if using <code>href=&quot;&quot;</code> one might actually <em>see</em> the page scroll to the div and jump back to the top.</p>\n<p>One solution is to use the standard JavaScript to properly disable the link:</p>\n<pre><code>&lt;a id=&quot;link1&quot; href=&quot;javascript:void(0);&quot;&gt;Scroll With Link&lt;/a&gt;\n</code></pre>\n<p>Now it will go to box12 and <em>stay</em> there.</p>\n'}, {'answer_id': 70434427, 'author': 'Lekens', 'author_id': 7575288, 'author_profile': 'https://Stackoverflow.com/users/7575288', 'pm_score': 2, 'selected': False, 'text': "<pre><code>&lt;button onClick=&quot;scrollIntoView()&quot;&gt;&lt;/button&gt;\n&lt;br&gt;\n&lt;div id=&quot;scroll-to&quot;&gt;&lt;/div&gt;\n\n\nfunction scrollIntoView() {\n document.getElementById('scroll-to').scrollIntoView({\n      behavior: 'smooth'\n });\n}\n</code></pre>\n<p>The scrollIntoView method accepts scroll-Options to animate the scroll.</p>\n<p>With smooth scroll</p>\n<pre><code>document.getElementById('scroll-to').scrollIntoView({\n          behavior: 'smooth'\n });\n</code></pre>\n<p>No animation</p>\n<pre><code>document.getElementById('scroll-to').scrollIntoView();\n</code></pre>\n"}, {'answer_id': 71981754, 'author': 'Kay', 'author_id': 14943717, 'author_profile': 'https://Stackoverflow.com/users/14943717', 'pm_score': 1, 'selected': False, 'text': '<p>I use a lightweight javascript plugin that I found works across devices, browsers and operating systems:  <a href="https://github.com/zengabor/zenscroll" rel="nofollow noreferrer">zenscroll</a></p>\n'}]
['https://Stackoverflow.com/questions/68165', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']