Linking to a specific part of a web page is an incredibly useful skill for web developers and content creators. Whether you want to create a “Table of Contents” style navigation on your site, guide users directly to the most important content, or enable sharing of specific sections, anchor links make it possible.
In this comprehensive guide we’ll cover everything you need to know about linking to specific parts of a web page using HTML anchor links.
What Are Anchor Links?
Anchor links allow you to link directly to a specific section on a web page rather than just the top. This creates a smoother user experience by enabling quick navigation and neatly sharing specific portions of content
The specific part being linked to is marked with an HTML anchor element The anchor is given a unique ID attribute that acts as an identifier The link references that ID in its href attribute to jump directly to that section,
For example:
<!-- Anchor in the target document --><h2 id="section1">Section 1</h2> <!-- Link pointing to the anchor --><a href="page.html#section1">Go to Section 1</a>
When clicked, the link will scroll the page to bring the element with id="section1"
into view.
This is the basic concept behind linking to a specific part of a page. Now let’s look at how to implement anchor links in more detail.
How To Create an HTML Anchor Link
Creating an anchor link involves just a few simple steps:
-
Decide what content you want the link to jump to. This will be the anchor target.
-
Add an
id
attribute to markup the anchor point. For example:html<h2 id="section1">Section 1</h2>
-
Create a link to that ID by appending
#id
to the URL. For example:html<a href="page.html#section1">Jump to Section 1</a>
And that’s all there is to it! When clicked, the link will scroll to the element with the corresponding id
.
Now let’s look at some best practices for working with anchor links.
Choosing Anchor Targets
You can turn nearly any HTML element into an anchor target by adding an id
attribute. However, some make more semantic sense than others:
-
Headings – Headings naturally divide content into sections, making them sensible targets.
-
Sections – The
<section>
element is perfect for denoting anchors for page sections. -
Articles – For content broken into multiple articles, use
<article>
elements as anchors. -
Divs and Spans – Generic
<div>
and<span>
elements work if no other element fits.
Naming Anchors
-
IDs must be unique on each page.
-
Descriptive, semantic names help identify anchors. For example,
section-intro
rather thans1
. -
Use dashes to separate words, like
section-intro
. -
Avoid spaces, special characters, and purely numeric IDs.
Linking to Anchors
-
Append the anchor ID to the page URL with a hash
#
, likepage.html#section1
. -
Use relative links like
#section1
to link within the same page. -
Ensure anchor links are clickable by using actual
<a>
elements rather than just referencing IDs.
Styling Anchor Links
Anchor links work right out of the box, but you can also leverage CSS to style links and enhance the scrolling experience.
Link Styles
Anchor links are just normal links, so you can style them using general link CSS:
/* Unvisited link */a:link { color: blue; text-decoration: none;}/* Visited link */ a:visited { color: purple;}/* Hover state*/a:hover { text-decoration: underline;}
This allows you to customize colors, typography, and other styling.
Smooth Scrolling
By default, anchor links jump directly to the target section. For a smoother experience, use CSSScroll Behavior:
html { scroll-behavior: smooth; }
This animates scrolling to the anchor ID, rather than jumping instantaneously.
Scroll Offset
At times anchor links can scroll too far, obscuring part of the target element behind the fixed header.
Add scroll padding to offset the scroll position:
:target { scroll-margin-top: 100px; }
Now anchor links will stop short of the target to account for the header height.
Table of Contents Navigation
Anchor links are commonly used to create linked table of contents menus that quickly navigate to different sections of a page.
To implement this:
-
Create anchor targets at the start of each section. For example:
html<h2 id="section1">Section 1</h2><h2 id="section2">Section 2</h2>
-
Make links to those anchors in your menu or sidebar. For example:
html<ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li></ul>
Now clicking the links will smoothly scroll to each section!
Link Sharing on Mobile
Anchor links also enable easily sharing specific parts of a page.
On iOS Safari and Chrome for Android, users can highlight content and share a link that automatically scrolls to that portion of the page.
To use this:
-
Highlight the desired text on a mobile device.
-
Tap the share button.
-
Copy the generated link containing an anchor ID.
When shared, the link will open directly to the highlighted content.
Anchor Link Best Practices
Here are some key tips for working with anchor links:
-
Place anchors at logical locations like headings and section starts.
-
Use unique, semantic ID names like
section-intro
. -
Implement smooth scrolling for better UI.
-
Add scroll offsets to account for fixed headers.
-
Keep links clickable – use
<a>
elements instead of just referencing IDs. -
Style links using CSS for design freedom.
-
Build navigation with table of contents menus.
-
Share specific sections using mobile highlight links.
Following these best practices will ensure your anchor links provide the best possible user experience!
Common Anchor Link Questions
Here are some common questions about working with anchor links:
Can I link to an element without an ID?
No – an ID is required to create an anchor target.
Do anchor IDs have to be unique across pages?
No – IDs only need to be unique within each standalone page.
Can I link to an anchor on another page?
Yes! Just add the full URL before the hash like page2.html#section1
.
Do name
attributes work instead of IDs?
No – name
is obsolete. Always use id
for anchors.
Where can I use anchor links?
Anchor links work on static HTML sites as well as sites built with frameworks like React, Angular, and Vue.
Anchor links provide an easy yet powerful way to connect users to specific content on a page.
By following the steps in this guide, you can implement smooth anchor link navigation and seamless content sharing using just HTML IDs and URLs.
Leverage anchors links to improve site usability and let users deep link directly to your most valuable page sections!
Sign up or log in Sign up using Google Sign up using Email and Password
Required, but never shown
9 Answers 9 Sorted by:
Just append a #
followed by the ID of the tag (or other HTML tag, like a
You could use the link Link
.
Create a “jump link” using the following format:
Where anchor is the id of the element you wish to link to on that page. Use browser development tools / view source to find the id of the element you wish to link to.
If the element doesnt have an id and you dont control that site then you cant do it.
That is only possible if that site has declared anchors in the page. It is done by giving a tag a name or id attribute, so look for any of those close to where you want to link to.
And then the syntax would be
In case the target page is on the same domain (i.e. shares the same origin with your page) and you dont mind creation of new tabs (1), you can (ab)use some JavaScript:
see tenth paragraph on another page
Trivia:
Working example of such exploit you can try right now could be:
javascript:(function(url,sel,w,el){w=window.open(url);w.addEventListener(load,function(){w.setTimeout(function(){el=w.document.querySelector(sel);el.scrollIntoView();el.style.backgroundColor=red},1000)})})(https://stackoverflow.com/questions/45014240/link-to-a-specific-spot-on-a-page-i-cant-edit,footer)
If you enter this into location bar (mind that Chrome removes javascript:
prefix when pasted from clipboard) or make it a href
value of any link on this page (using Developer Tools) and click it, you will get another (duplicate) SO question page scrolled to the footer and footer painted red. (Delay added as a workaround for ajax-loaded content pushing footer down after load.)
- Tested in current Chrome and Firefox, generally should work since it is based on defined standard behaviour.
- Cannot be illustrated in interactive snippet here at SO, because they are isolated from the page origin-wise.
- MDN: Window.open()
- (1)
window.open(url,_self)
seems to be breaking theload
event; basically makes thewindow.open
behave like a normala href=""
click navigation; havent researched more yet.
The upcoming Chrome “Scroll to text” feature is exactly what you are looking for….
You basically add #targetText=
at the end of the URL and the browser will scroll to the target text and highlight it after the page is loaded.
It is in the version of Chrome that is running on my desk, but currently it must be manually enabled. Presumably it will soon be enabled by default in the production Chrome builds and other browsers will follow, so OK to start adding to your links now and it will start working then.
Edit: Its been implemented in Chrome. See https://chromestatus.com/feature/4733392803332096
As of Chrome release 81 (Feb 2020), there is a new feature called Text Fragments. It allows you to provide a link that opens at the precise text specified (with that text highlighted).
At the moment, it works in Edge, Chrome and Opera but not in Firefox, Safari or Brave. (See note 6 at bottom for more)
You create the link to your desired text by appending this string to the end of the URL:
and providing the percent-encoded search string thus:
Here is a working example:
- Test the above link in Chrome or Opera only
- In the above example, note that the text string is in a div that is normally hidden on page load – so in this example it is being displayed despite what would normally happen. Useful.
- Recent versions of Chrome also include a new option when you Right-Click on selected text: Copy link to highlight. This will auto-create the direct-to-text link for you (i.e. it automatically appends the
/#:~:text=
to the text you highlighted) and place it in the clipboard – just paste it where desired. - Suppose you want to highlight an entire block of text? The Text Fragments feature allows specifying a
starting phrase
and anending phrase
(separated by a comma), and it will highlight all text in between: https://newz.icu/#:~:text=Dr. Mullis,before now Note the comma betweenMullis
andbefore
- web.dev article about Text Fragments
- CanIUse status of Text Fragments
First off target refers to the BlockID found in either HTML code or chromes developer tools that you are trying to link to. Each code is different and you will need to do some digging to find the ID you are trying to reference. It should look something like div class="page-container drawer-page-content" id"PageContainer"
Note that this is the format for the whole referenced section, not an individual text or . To do that you would need to find the same piece of code but relating to your target block. For example dv id="your-block-id"
Anyways I was just reading over this thread and an idea came to my mind, if you are a Shopify user and want to do this it is pretty much the same thing as stated. But instead of
For example, I am setting up a disclaimer page with links leading to a newsletter signup and shopping blocks on my home page so I insert https://mystore-classifier.com/#shopify-section-1528945200235
for my hyperlink. Please note that the -classifier is for my internal use and doesnt apply to you. This is just so I can keep track of my stores. If you want to link to something other than your homepage you would put
I hope someone found this useful, if there is something wrong with my explanation please let me know as I am not an HTML programmer my language is C#!
Its now possible to create an “anchor” link that goes to a specific part of any webpage in most browsers in a few different ways. All of them will create a link with an #anchor at the end, where “anchor” is the thing that you want to navigate to. The browser will interpret the part of the URL after the # to scroll to a specific part of the page.
Here are 3 ways to create a url like this:
- Using an existing anchor. Perhaps there will be one in the URL as you scroll down the page. If not, look around the page for a header that has a little link icon to the left of it and click it to update the browsers navigation url.
- Using any html elements id property or the name or id on an (“anchor”) element. The other answers explain this quite well. You will have to open the developer console and inspect the part of the page to find an id (and you may not find one). Its a little different on each browser, but heres how to inspect an element in Chrome.
- Using a text snippet to highlight part of the page.
Basically, html tag can have id=”abc” as shown below:
And, “” tag can also have name=”abc” as shown below:
Then, you can use the id
and name
values “abc” with “#” in urls as shown below to go to the specific part of a page:
Then, you can put the urls above in “” tag to create the links to id=”abc” and name=”abc” as shown below:
And, if you want to go to the specific part of the same page, you can only put the id
and name
values “abc” with “#” in “” tag to create the links to id=”abc” and name=”abc” as shown below:
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers. Draft saved Draft discarded
How to Link to a Specific Part of a Page (HTML anchor link)
How to link to a specific part of a webpage?
Chrome browser offers a native feature to link to a specific part of a webpage and share it. You can use it on any computer (Windows or Mac) and even Android or iOS. 1. In the Chrome browser, go to the website text to which you want to link. 2. Select the text by clicking and holding the mouse button. 3.
How do I link a part of a page?
To link this part of page use following link, stackoverflow.com/questions/15481911/… Just append a # followed by the ID of the tag (or other HTML tag, like a
How to create links to a specific part of the same page?
And, if you want to go to the specific part of the same page, you can only put the id and name values “abc” with “#” in “” tag to create the links to id=”abc” and name=”abc” as shown below:
How do I link to the top of a page?
To link to the top of a page: Add text and an anchor link to the bottom of the page or the footer as described in Step 2, but don’t include the URL for your site or a slash mark (/) before the unique ID. You may want to use Back to topas the text, and #topas your unique ID. Open the Code Injection panel.