Link within Page

You are already aware that with HTML element <a>, we can link different HTML documents on a page.

For example, from this HTML document, we can add a link to by providing its URL in the href attribute.

<a href="">Go to</a>

But HTML provides a technique through which we can link an HTML document's internal sections or elements.

Such links within a page are created with help of id attributes of HTML tags.

Linking within the same document is helpful and creates a better user experience when a page is very long.

Let's create a link for a sub-section within the page.

Step 1

First, we will define an HTML element with attribute id.

The value of the id attribute will be a unique name.

<h4 id="william">About William Shakespeare</h4>

By unique, we mean that no other id will have the same name in that HTML document.

This is like we have created a bookmark for William Shakespeare with the id william.

Step 2

Now we can create a link for the bookmark we created as follows:

<a href="#william">William Shakespeare</a>

Here we have written the id value after the # (hash) symbol in the href attribute.

Below is a complete example. Click on 'Result on Editor' to view the output of the code.

<!DOCTYPE html>
<title>HTML Link within Page Example</title>

<h1>Famous Writers</h1>

<p>Below are some famous English writers. Click on William Shakespeare to know more about him.</p>
<a href="#william">William Shakespeare</a>

<h4>Jane Austen</h4>
<p>This pargraph tells about Jane Austen.</p>

<h4>Charles Dickens</h4>
<p>This pargraph tells about Charles Dickens.</p>

<h4>George Orwell</h4>
<p>This pargraph tells about George Orwell.</p>

<h4>George Eliot</h4>
<p>This pargraph tells about George Eliot.</p>

<h4>Virginia Woolf</h4>
<p>This pargraph tells about Virginia Woolf.</p>

<h4>J. R. R. Tolkien</h4>
<p>This pargraph tells about J. R. R. Tolkien.</p>

<h4>Geoffrey Chaucer</h4>
<p>This pargraph tells about Geoffrey Chaucer.</p>

<h4>William Wordsworth</h4>
<p>This pargraph tells about William Wordsworth.</p>

<h4>J K rowling</h4>
<p>This pargraph tells about J K rowling.</p>

<h4>William Blake</h4>
<p>This pargraph tells about William Blake.</p>

<h4>Khushwant Singh</h4>
<p>This pargraph tells about Khushwant Singh.</p>

<h4>John Donne</h4>
<p>This pargraph tells about John Donne.</p>

<h4>Arthur Conan Doyle</h4>
<p>This pargraph tells about Arthur Conan Doyle.</p>

<h4>Thomas Hardy</h4>
<p>This pargraph tells about Thomas Hardy.</p>

<h4 id="william">William Shakespeare</h4>
<p>This pargraph tells about William Shakespeare.</p>

<h4>Arundhati Roy</h4>
<p>This pargraph tells about Arundhati Roy.</p>


From other HTML documents we can link to this bookmark as follows:

<a href="url#william">Go to William Shakespeare</a>

The url will be replaced with the actual URL of the document where the id william is specified.

If a page is very long and divided into different sections, providing links to such sections within a page is helpful to users.

It creates a better user experience because they can directly navigate to the sections they are more interested in.

On this page, we have talked about the HTML id attribute. Don't worry if you don't know about it. You will learn about it later in the tutorial.

  • Disclaimer: is an independent private website and it is in no way affiliated to any Indian Government official website. The sole purpose of this website is to provide notifications regarding the latest results published. Though utmost care is being taken, we can not guarantee 100% correctness of information. Using this website confirms that you agree to all the terms and conditions.