Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,884 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.