How to create links to sections on the same page in HTML

In this tutorial I shall show you how to create links to sections on the same page in html.

Let’s start step by step.

Create a blank HTML document

<!DOCTYPE html>
<head>

</head>
<body>

</body>
</html>

In the above script you can see I have created a blank html document.

Now let’s add paragraphs to the documents with the content.

Add paragraphs to HTML document

<!DOCTYPE html>
<head>

</head>
<body>
<div class="container">
    <p id="section1">
        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.
    </p>
    <p id="section2">
        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. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
    </p>
    <p id="section3">
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
    </p>
    <p id="section4">
        There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
    </p>
</div>
</body>
</html>

In the above code snippet you can see that I have added 4 paragraphs with ids.

Now let’s add anchor tags to document and add section ids to the href attribute to make link clickable.

Add anchor tags with section ids to attributes

<div>
        <a href="#section1">section 1</a>
        <a href="#section2">section 2</a>
        <a href="#section3">section 3</a>
        <a href="#section4">section 4</a>
</div>

In the above snippet you can see I have added 4 links with section ids to href attributes.

Now add some style to the container div class to make it scrollable.

    <style>
        .container {
            width: 300px;
            height: 300px;
            overflow: scroll;
        }

        p {
            margin-bottom: 30px;
        }
    </style>

In the above script I have added width and height to container and “overflow” property to scroll. And also given some bottom margin to make space to each paragraph.

Now let’s combine all code in single script.

<!DOCTYPE html>
<head>
    <style>
        .container {
            width: 300px;
            height: 300px;
            overflow: scroll;
        }

        p {
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div class="container">
    <div>
        <a href="#section1">section 1</a>
        <a href="#section2">section 2</a>
        <a href="#section3">section 3</a>
        <a href="#section4">section 4</a>
    </div>
    <p id="section1">
        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.
    </p>
    <p id="section2">
        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. Many desktop publishing
        packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum'
        will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by
        accident, sometimes on purpose (injected humour and the like).
    </p>
    <p id="section3">
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin
        literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
        College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and
        going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum
        comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by
        Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance.
        The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
    </p>
    <p id="section4">
        There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in
        some form, by injected humour, or randomised words which don't look even slightly believable. If you are going
        to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of
        text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this
        the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful
        of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is
        therefore always free from repetition, injected humour, or non-characteristic words etc.
    </p>
</div>
</body>
</html>

In the above screenshot you can see the final rendered code.

Congratulations you have successfully created links to section on the same html page.