How do I create a link to a section within the same page in HTML?

The most important attribute of the element is the href attribute, which indicates the link's destination.

The link text is the part that will be visible to the reader.

Clicking on the link text, will send the reader to the specified URL address.

Example

This example shows how to create a link to W3Schools.com:

Visit W3Schools.com!

Try it Yourself »

By default, links will appear as follows in all browsers:

  • An unvisited link is underlined and blue
  • A visited link is underlined and purple
  • An active link is underlined and red

Tip: Links can of course be styled with CSS, to get another look!


By default, the linked page will be displayed in the current browser window. To change this, you must specify another target for the link.

The target attribute specifies where to open the linked document.

The target attribute can have one of the following values:

  • _self - Default. Opens the document in the same window/tab as it was clicked
  • _blank - Opens the document in a new window or tab
  • _parent - Opens the document in the parent frame
  • _top - Opens the document in the full body of the window

Example

Use target="_blank" to open the linked document in a new browser window or tab:

Visit W3Schools!

Try it Yourself »


Absolute URLs vs. Relative URLs

Both examples above are using an absolute URL (a full web address) in the href attribute.

A local link (a link to a page within the same website) is specified with a relative URL (without the "https://www" part):

Example

Absolute URLs

W3C

Google

Relative URLs

HTML Images

CSS Tutorial

Try it Yourself »



To use an image as a link, just put the 0 tag inside the tag:

Example


How do I create a link to a section within the same page in HTML?

Try it Yourself »


Use 2 inside the href attribute to create a link that opens the user's email program (to let them send a new email):


To use an HTML button as a link, you have to add some JavaScript code.

JavaScript allows you to specify what happens at certain events, such as a click of a button:

Example

HTML Tutorial

Try it Yourself »


The 4 attribute specifies extra information about an element. The information is most often shown as a tooltip text when the mouse moves over the element.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.

In HTML, you use an id attribute to identify one specific element on a webpage. Just like each person has an unique Identification number, the id value you create and assign to your HTML element should be unique on that specific webpage.

Rules for creating ids

  • Each of your ids must be unique on that one web page.
  • Each of your ids must not have spaces in the name. If you want to seperate words and numbers, use hyphens and underscores instead of spaces.

in my case, I want to be able to jump to my subheadings so this is how my id’s will look like:



If you are using markdown, headings are automatically converted to their lowercase id equivalent seperated by hyphen with no special characters. If you inspect my headings, this is what you will see. See what I mean.

How do I create a link to a section within the same page in HTML?

Normally, you use links to point to different webpages and website. To make your link to point to the current webpage, you use a href="/" or the full url of the current webpage href="devpractical.com/same-page-internal-link"

To point to a specific part of a web page, you add a pound sign followed by the id name at the end of the url ie. href="/#assign-id" or href="devpractical.com/same-page-internal-link/#add-link"

If you click on the navigation items at the top, you will be able to jump to specific parts of the web page.

For you to add a link to a different page, you will need the url of that page and the id value of that section you want to link to. You will then use the full url of the web page and then add the id value at the end of the url. Example:

I want to link to the books section a different page on this website. The url is https://devpractical.com/resources/ and the heading is books. I can create a link as follows:

Code:

HTML and CSS books

Result: HTML and CSS books

If you are linking to a different website, you will have to find out if the website has assigned ids to the section you want to link to. You can do this by:

  • Inspect Q element.
  • Use the select tool.
  • Check the html code for id value.

If the element does not have an id value, you cannot link to that section. If it does then you can just add the pound sign and id to the url of the website and you are good to go.

Code:

Which insects do people eat

Result: Which insects do people eat

  • Choose the top most part of your website. Which is the logo.
  • Get it id value.
  • For me its the website’s logo with the id “logo”.

So the link to go to the top will become:

Code: Top

Result: Top

  • Choose the bottom most part of your website.
  • Get it id value.
  • For me its the footer with the id “footer-style”
  • So the link to go to the bottom will become:

Code: Go to bottom

Result: Go to bottom

How do I create a link to a section within the same page in HTML?

Hi there! I am Avic Ndugu.

I have published 100+ blog posts on HTML, CSS, Javascript, React and other related topics. When I am not writing, I enjoy reading, hiking and listening to podcasts.

Linking to an element on the same page Note: You can use href="#top" or the empty fragment ( href="#" ) to link to the top of the current page, as defined in the HTML specification.
Add the link.
Select the text or object you want to use as a hyperlink..
Right-click and then click Hyperlink ..
Under Link to, click Place in This Document..
In the list, select the heading or bookmark that you want to link to..