Interview Questions

How do I link to a location in the middle of an HTML document?

HTML Interview Questions and Answers


(Continued from previous question...)

45. How do I link to a location in the middle of an HTML document?

First, label the destination of the link. The old way to label the destination of the link was with an anchor using the NAME attribute. For example:
<h2><a name="section2">Section 2: Beyond Introductions</a></h2>

The modern way to label the destination of the link is with an ID attribute. For example:
<h2 id="section2">Section 2: Beyond Introductions</h2>

Second, link to the labeled destination. The URL is the URL of the document, with "#" and the value of the NAME or ID attribute appended. Continuing the above examples, elsewhere in the same document you could use:
<a href="#section2">go to Section 2</a>

Similarly, in another document you could use:
<a href="thesis.html#section2">go to Section 2 of my thesis</a>

(Continued on next question...)

Other Interview Questions