'How do I link to part of a page? (hash?)
How do you link (with <a>) so that the browser goes to certain subheading on the target page as opposed to the top?
Solution 1:[1]
You use an anchor and a hash. For example:
Target of the Link:
<a name="name_of_target">Content</a>
Link to the Target:
<a href="#name_of_target">Link Text</a>
Or, if linking from a different page:
<a href="http://path/to/page/#name_of_target">Link Text</a>
Solution 2:[2]
Just append a hash with an ID of an element to the URL. E.g.
<div id="about"></div>
and
http://mysite.com/#about
So the link would look like:
<a href="http://mysite.com/#about">About</a>
or just
<a href="#about">About</a>
Solution 3:[3]
On 12 March 2020, a draft has been added by WICG for Text Fragments, and now you can link to text on a page as if you were searching for it by adding the following to the hash
#:~:text=<Text To Link to>
Working example on Chrome Version 81.0.4044.138:
Click on this link Should reload the page and highlight the link's text
Solution 4:[4]
Here is how:
<a href="#go_middle">Go Middle</a>
<div id="go_middle">Hello There</div>
Solution 5:[5]
You have two options:
You can either put an anchor in your document as follows:
<a name="ref"></a>
Or else you give an id to a any HTML element:
<h1 id="ref">Heading</h1>
Then simply append the hash #ref to the URL of your link to jump to the desired reference. Example:
<a href="document.html#ref">Jump to ref in document.html</a>
Solution 6:[6]
Provided that any element has the id attribute on a webpage. One could simply link/jump to the element that is referenced by the tag.
Within the same page:
<div id="markOne"> ..... </div>
......
<a href="#markOne">Jump to markOne</a>
Other page:
<div id="http://randomwebsite.com/mypage.html#markOne">
Jumps to the markOne element in the mypage of the linked website
</div>
The targets don't necessarily have an anchor element.
You can go check this fiddle out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Michael Aaron Safyan |
| Solution 2 | Felix Kling |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | Daniel Vassallo |
| Solution 6 | Digdarshan Kunwar |
