How to create links to sections within the same page in HTML?


Crafting internal links within a web page in HTML has a positive impact on user experience, as it enhances the navigation for the website guests. By exploiting the id attribute and the a tag, one can effortlessly establish connections to certain portions of a web page, facilitating speedy access to required information without the tedious task of scrolling through the entire page. In this write-up, we will take you through the necessary procedures to devise such links that exist within a single webpage, using HTML.

Syntax

<element id=”value”>…</element>

Id Attribute

The identifier attribute in HTML is leveraged to recognize an element in a webpage. The value of the identifier attribute must be distinct within the HTML document, which indicates that no two elements can have the same identifier value

Whence the element represents the specific HTML label for which you wish to assign an id, and value denotes the distinct identifying token for said element.

Approach

Step 1 − Identify the Target Section

The primary action to initiate an internal link is to recognize the specific area that you desire to link to. This can be accomplished by incorporating an identifier attribute to the HTML element that designates the destination section.

Step 2 − Create the Link

Once the target section has been identified, the next step is to create the link itself. This is done using the a tag with the href attribute set to the target id preceded by a “#” symbol.

Step 3 − Repeat for Additional Links

Repeat these two steps for each additional link you want to create, making sure to give each target section a unique id attribute and creating a corresponding link for each one.

Example

The following code contains tags and attributes for creating links within the same webpage. The document specifies the file type and includes a "head" division for the webpage title and a "style" division for CSS coding. The "body" section has a heading, two paragraphs with anchor links to sections within the same page, and "div" tags defining two sections with different "id" attributes. Each section has a heading and a paragraph with text, and "br" tags insert line breaks between elements.

<!DOCTYPE html>
<html>
<head>
   <title>How to create links to sections within the same page in HTML?</title>
   <style>
      #section1, #section2{
         margin: 5px;
         padding: 3px;
      }
      #section1{
         background-color: lightblue;
      }
      #section2{
         background-color: lightcoral;
      }
   </style>
</head>
<body>
   <h4>How to create links to sections within the same page in HTML?</h4>
   <p><a href="#section1">Go to Section 1</a></p>
   <p><a href="#section2">Go to Section 2</a></p>
   <br/>
   <br/>
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br /> 
   <br />
   <br />
   <br />
   <br />
   <div id="section1">
      <h2>Section 1</h2>
      <p>Some text in section 1.</p>
   </div>
   <br/>
   <br/>
   <br/>
   <br/>
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <br />
   <div id="section2">
      <h2 >Section 2</h2>
      <p>Some text in section 2.</p>
   </div>
</body>
</html>

Conclusion

To summarize, generating interconnections to partitions within the identical webpage in HTML is a facile progression that can significantly enhance the consumer acquaintance on your site. By availing oneself of the identification quality and the a symbol, you can readily link to explicit portions of a webpage, rendering it simpler for your visitors to pinpoint the intelligence they necessitate. With this methodology, you can offer direction to users as they maneuver through your website, yielding a more methodical and efficient piloting experience. Regardless of whether you are a neophyte or an adept web developer, assimilating internal links into your HTML pages is a sine qua non skill for originating productive and user-friendly websites.

Updated on: 13-Apr-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements