What is an anchor in HTML and how to create it

An anchor in HTML is used to create links within a web page or to other pages. It is defined using the <a> tag, which allows users to navigate to different sections of a document or to other web pages.

How to create an anchor in HTML

Link to another page
To create a link to another page, the href attribute is used within the <a> tag. Here’s an example:

<a href="https://faq.puntocomunica.com">Visit FAQs Puntocomunica</a>

And this would be the result:

Visit FAQs Puntocomunica

Link to a section within the same page
To link to a specific section within the same page, you first need to define an ID on the element you want to link to. Then, you can create a link that points to that ID.

Example

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Anchor Example</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p><a href="#section1">Go to Section 1</a></p>
<h2 id="section1">Section 1</h2>
<p>Content of section 1.</p>
<p><a href="#section2">Go to Section 2</a></p>
<h2 id="section2">Section 2</h2>
<p>Content of section 2.</p>
</body>
</html>

Explanation of the example

Link to another page: The first link takes the user to "https://www.example.com".
Link to a section: The link "Go to Section 1" takes the user to the part of the page where the heading with the ID section1 is located.

Anchors are useful for improving navigation on long pages or for linking related content.