Download Article
Easy ways to use HTML to link to another page
Download Article
While a menu or table of contents can help, it's tiresome to visit the top of a long webpage, then scroll down to find what you're looking for. Save your visitors a trip by linking directly to an anchor instead. An anchor can appear anywhere on the page and has a short "fragment identifier" from the id attribute. Add the # symbol followed by the fragment identifier to the end of the URL, and you can link directly to the anchor.
Steps
-
Create an anchor element. The "anchor" element <a></a> defines a place on the page that you can link to. Anything inside the <a> and </a> tags, typically text or an image, can be the destination of the link.
-
Place something inside the anchor element. Although it is valid HTML to leave the anchor element empty, some browsers will fail to find it if there's nothing between the <a> and </a> tags. [1] X Research source Simply type in the text you would like to link to:
- <a>My Lasagna Recipe</a>
- The a tag does not typically change the style of the text. [2] X Research source In this example, "My Lasagna Recipe" will appear as ordinary text.
Advertisement -
Add an id attribute to your anchor element. The id attribute gives the anchor a unique identifier so you can link to it. Place it inside the <a> tag as follows:
- <a id="anchor-name-1">My Lasagna Recipe</a>
-
Choose a value for your id. The example above used "anchor-name-1," but it's best to give your anchors a descriptive value, such as "lasagna" in this case. This value must be unique to this id. If another id in the same document has the same value, the browser cannot identify the single anchor you're trying to link to.
- In HTML4, the value must begin with a letter. Can use letters, digits, hyphens, underscores, colons, and periods. [3] X Research source
- In HTML5, you can use any character except for spaces. [4] X Research source
- Be careful with your cases. "Polish" and "polish" are considered the same value, and should not appear in the same document. [5] X Research source
-
Insert id into any element instead. You don't need to use the <a> tag every time you want to create an anchor. The id attribute can actually go into any HTML element. [6] X Research source [7] X Research source All modern browsers (going back quite a while) should be able to interpret this. Here are a few examples:
- Anchor in a header: <h2 id="biblio">Bibliography</h2>
- Anchor in an image: <img id="logo" src="/images/logo.png" />
- Anchor in a paragraph: <p id="introparagraph">(introductory paragraph)</p>
- Remember that each id can only appear once per page.
Advertisement
-
Link to the anchor from elsewhere on the same page. This is similar to any link, using the <a href=" "> </a> format. However, instead of a URL as the value of the href attribute, use the # symbol followed by the anchor value. To link to the lasagna recipe in the example above, you would type:
- <a href="#lasagna">Click here to see my lasagna recipe.</a>
- Use exactly the same case as you did when creating the anchor. Some browsers will not recognize "#Lasagna" as a link to "lasagna." [8] X Research source
-
Link to the anchor from another webpage. You can also link to your anchor from any other website. Just include the URL followed by # and the anchor value. Here are a couple examples:
- Linking from another page in the same domain:
<a href="recipes.html#lasagna">Go to my recipes page to see my lasagna recipe.</a>
- Linking from another page in the same domain:
-
Turn an image into a link. As with an ordinary URL, you can link to an anchor using an image:
- <a href="#lasagna"><img src="chickenlasagna.png" /></a>
Advertisement
Community Q&A
Search
-
QuestionHow can I create a page?Community AnswerYou can create a web page or website by signing on to Google, selecting Google Sites, and selecting New Google Sites. Then go to Create New Website and Create Web Page.
-
QuestionMy links don't go anywhere, why?Chaukor StudioCommunity AnswerHyperlink accurately. Check your anchor text is properly linked with 200 url.
-
QuestionIs it possible to include the enclosed text of the target anchor as the text of the hyperlink so that a change to the target contained text will change the hyperlink text?GreenJarsWithSmallLabelsCommunity AnswerI assume "enclosed text of the target anchor" refers to the id of the target element, and that "hyperlink text" is the text displayed in the <a> element. The best way of doing this would be either to generate the table of contents on the server side using a language such as PHP, or with Javascript that sets the text by reading the value in "href" attribute of the <a> element, then setting the "innerText" property accordingly. A simple CSS workaround could also be used with the ::after selector by setting it's "content" property to "attr(href)," though this would leave a "#" symbol in the displayed text.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
Tips
- If your anchor links are just taking you to the top of the page, check whether your website isn't redirecting the browser to a slightly different URL. For example, some browsers will (incorrectly) drop the anchor after redirecting from http://www.example.com to http://example.com. [9] X Research source You can fix this by making sure all your anchor links point to http://example.com/#anchor-name, so the redirect doesn't happen.Thanks
- This is a good way to make footnotes. The convention is to link to footnotes with a number in superscript and square brackets. [10] X Research source For example:
Julius Caesar<sup><a href="#ftn1">[1]</a></sup>
links to:
<a id="ftn1">Famous Roman and close friend of mine.</a>Thanks
Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!
Advertisement
Warnings
- A common mistake is to use the # symbol when creating the destination anchor. Only use # when linking to the anchor, inside an href attribute.Thanks
- Underscores in id values may cause trouble when using CSS to style your document.Thanks
- HTML5 and XHTML do not support the name attribute in anchor elements. [11] X Research source [12] X Research source Use the id attribute instead as described in this article. If you expect visitors to your site to use very out-dated browsers (well over ten years old), you may include two identical id and name attributes (<a id="example-anchor" name="example-anchor">Example Text</a>). [13] X Research source HTML4 officially supports this only for the elements a, applet, form, frame, iframe, img, and map. [14] X Research sourceThanks
Advertisement
References
- ↑ https://www.w3.org/TR/html4/struct/links.html
- ↑ https://www.w3.org/TR/html4/struct/links.html#anchors-with-id
- ↑ https://www.w3.org/TR/html4/types.html#type-cdata
- ↑ https://www.w3.org/TR/html5/dom.html#the-id-attribute
- ↑ https://www.w3.org/TR/html4/struct/links.html
- ↑ https://www.w3.org/TR/html4/struct/links.html
- ↑ https://www.w3.org/TR/html5/dom.html#the-id-attribute
- ↑ https://www.w3.org/TR/html4/struct/links.html
- ↑ https://www.w3.org/TR/cuap#uri
About This Article
Thanks to all authors for creating a page that has been read 234,167 times.
Advertisement