Anchor links, also known as bookmarks, are used in HTML to navigate to specific sections within the same webpage or to other sections of a website. They are particularly useful for lengthy pages where quick navigation between sections improves user experience. Anchor links are created using the <a>
tag with href
attributes that target specific id
attributes within the page.
To create an anchor link, you need two parts:
id
attribute, which acts as the destination for the link.<a>
tag with an href
attribute pointing to the target id
.Here is a basic example:
In this example, clicking the "Go to Section 1" link will scroll the page to the element with id="section1"
.
Anchor links are often used to create navigation menus that quickly move to different sections on a page. Here’s an example of a simple navigation menu using anchor links:
This code creates links that jump to specific sections on the same page. The id
attributes act as bookmarks, and the href
values point to them.
You can link to a specific section on another page by using the URL of that page followed by #id
of the target section. For example:
This link will open page.html
on example.com
and scroll to the section with id="section2"
if it exists on that page.
Modern browsers often support smooth scrolling behavior for anchor links by default, but you can also enable smooth scrolling with CSS:
With this CSS, the page will smoothly scroll to the anchor link’s target location.
id
on a page should be unique to avoid confusion when creating anchor links.id
attributes, such as introduction
or contact
, for clarity.Anchor links and bookmarks in HTML allow for smooth navigation within a single page or between pages, creating a better user experience, especially for long or complex web pages. By using the <a>
tag with href
attributes linked to id
attributes, you can create efficient navigation paths, enhancing readability and accessibility for users.