Creating a Scrollable Section Navigation in HTML and CSS

creating-a-scrollable-section-navigation-in-html-and-css

Creating a Scrollable Section Navigation in HTML and CSS

When designing a webpage with multiple sections of content, it’s essential to provide a seamless user experience for navigating through these sections. One effective way to achieve this is by implementing a scrollable section navigation. In this article, we’ll guide you through creating a simple scrollable section navigation using HTML, CSS, and a sprinkle of JavaScript.

Setting Up the Structure

Let’s start by setting up the basic structure of our webpage. We’ll have a navigation bar at the top with clickable links for each section, and below that, the corresponding sections of content.

HTML Structure


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>Scrollable Section Navigation<span class="nt">
     rel="stylesheet" href="styles.css">


     class="nav">
        
  • href="#section1">Section 1
  • href="#section2">Section 2
  • href="#section3">Section 3
id="section1" class="section">

Section 1

Lorem ipsum dolor sit amet, consectetur adipiscing elit.

id="section2" class="section">

Section 2

Nulla facilisi. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

id="section3" class="section">

Section 3

Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui.

CSS Styles (styles.css)

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
}

.nav {
    background-color: #333;
    color: #fff;
    padding: 10px 0;
}

.nav ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    text-align: center;
}

.nav ul li {
    display: inline-block;
    margin-right: 20px;
}

.nav ul li a {
    color: #fff;
    text-decoration: none;
    padding: 5px 10px;
}

.nav ul li a:hover {
    background-color: #555;
}

.main {
    padding: 20px;
}

.section {
    padding: 20px;
    margin: 20px;
    background-color: #f9f9f9;
    border-radius: 5px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

Adding Functionality with JavaScript (script.js)

To make our navigation interactive, we’ll use JavaScript to handle smooth scrolling when clicking on the navigation links.

document.addEventListener("DOMContentLoaded", function() {
    const links = document.querySelectorAll('.nav a');

    links.forEach(link => {
        link.addEventListener('click', clickHandler);
    });

    function clickHandler(e) {
        e.preventDefault();
        const href = this.getAttribute("href");
        const offsetTop = document.querySelector(href).offsetTop;

        scroll({
            top: offsetTop,
            behavior: "smooth"
        });
    }
});

How It Works

  1. HTML Structure: We have a simple HTML structure with a navigation bar (

  • When a navigation link is clicked (clickHandler function), it prevents the default link behavior (e.preventDefault()).
  • The script then retrieves the target section’s ID from the href attribute of the clicked link.
  • It calculates the offsetTop position of the target section relative to the top of the document.
  • Finally, it smoothly scrolls to the target section using the scroll method with behavior: "smooth".

Conclusion

With this scrollable section navigation, users can easily navigate between different sections of content on your webpage. The smooth scrolling effect enhances the user experience, making it more pleasant and engaging to explore the content. Customize the styles and content to fit your design needs, and feel free to expand upon this concept to create even more interactive and user-friendly web experiences!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
why-dimensional-air-gages-work-so-well

Why Dimensional Air Gages Work So Well

Next Post
podcast:-create-a-culture-of-excellence

Podcast: Create a Culture of Excellence

Related Posts