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

what's the difference between href="articles/2017/article.html#vr-article"/ and href="articles/2017/article.html".

If I remove #vr-article"/ , it makes no difference and links to the same page. Can anyone please help me to understand how # works in linking to different sections of a webpage

1 Answer

Jacob Tollette
Jacob Tollette
27,864 Points

href="articles/2017/article.html#vr-article"/

href="articles/2017/article.html

They do both go to the same page; however, the first link, with the "#" (octothorpe/hashtag/poundsign/tic-tac-toe-thing) is linking to a specific anchor within that page. For example, if you have a "div" or other element on the page with an ID of "vr-article", it will link to that section of that page. You could also just set an anchor element with a name attribute equivalent to "vr-article" and it will link just the same. Here's an example:

Let's assume this is "index.html"

<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<a href="/articles/2017/article.html#vr-article">View the VR Article</a>
</body>
</html>

Now let's assume this is "article.html"

<!DOCTYPE html>
<html>
<head>
<title>Article Page</title>
</head>
<body>
<a href="#vr-article">View the VR Article</a>
<article id = "vr-article">
<h2>Super-Amazing VR article</h2>
<p>Some stuff</p>
</article>
</body>
</html>

Here's an alternate version with the anchor tag I mentioned:

<!DOCTYPE html>
<html>
<head>
<title>Article Page</title>
</head>
<body>
<a href="#vr-article">View the VR Article</a>
<article>
<h2><a name="vr-article">Super-Amazing VR article</a></h2>
<p>Some stuff</p>
</article>
</body>
</html>

In all three cases, the link points to the article container that has the "Super-Amazing VR article"

Thank you Jacob, that was really helpful.