Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

HTML HTML Links Anchors

Shams Nelson
Shams Nelson
2,888 Points

How can I get a link to the same page to scroll to the content

When something like <a href="#bottom_content"> is used to link to a div at the bottom of the same page, how can I have that page scroll to that location instead of teleport (for lack of a better word) there?

4 Answers

Kevin Korte
Kevin Korte
28,109 Points

You need Javascript to do this.

I have never used, nor did I know much about this plugin, but it came up first on a google search, so use at your own risk.

https://github.com/cferdinandi/smooth-scroll#readme

Ana Cristina Patiño Aguirre
Ana Cristina Patiño Aguirre
4,644 Points

Ok, If I have understood correctly, try addeding an id to the div and use the id's name as value to the href attribute like this

 <a href="#nameofthediv">Link to the div</a>

<div id="nameofthediv">Content</div>
Kevin Korte
Kevin Korte
28,109 Points

As I understood it, it sounds like he already has that working, because he asked how to make the page scroll there, instead of just jump right to that div, as this basic set up would.

Shams Nelson
Shams Nelson
2,888 Points

Perfect, thanks!

And sorry bout the confusion :)

Shams Nelson
Shams Nelson
2,888 Points

Just used this and it works great. Very simple and clean to set up as well. Thanks again.

~Shams