How to Create a One-Page Website with Fixed Floating Bottom Navigation using HTML, CSS, and JavaScript





Hello developers, In this tutorial, we'll build a one-page website that has a fixed floating bottom navigation and that automatically adjusts each section's height to match the device's screen size using HTML, CSS, and JavaScript. We'll also include a smooth scrolling animation as we move between sections. To begin, we'll create the HTML structure. Every section will have a unique ID so that we can link the navigation bar with it. We'll then style the website using CSS. We'll make the navigation bar float at the bottom of the page by applying styles to it. We'll also apply styles to each section to make it fill the entire screen.We will now add JavaScript code to make the navigation bar highlight the active link as we scroll through the different sections of our website. We will use the offsetTop and clientHeight properties to determine which section is currently in view and add the active class to the corresponding link in the navigation bar. Finally, we will add smooth scrolling animation while navigating between sections. We will use the scrollTo method with the behavior property set to "smooth" to achieve this.

One-Page Website with Fixed Floating Bottom Navigation using HTML, CSS & JavaScript [Source Code]

To make this website, you would like to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>One Page Website</title> <link rel="stylesheet" href="style.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Aladin&display=swap" rel="stylesheet"> </head> <body> <nav> <ul> <li><a href="#home">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#services">Services</a></li> <li><a href="#contact">Contact</a></li> </ul> </nav> <section id="home"> <h1>Home Section</h1> <p>Fixed Floating Navigation Bar to navigate though sections.</p> <div class="credit">Made with <span style="color:tomato;font-size:20px;">❤ </span>by<a href="https://www.learningrobo.com/"> Learning Robo</a></div> </section> <section id="about"> <h1>About Section</h1> <p>Fixed Floating Navigation Bar to navigate though sections.</p> <div class="credit">Made with <span style="color:tomato;font-size:20px;">❤ </span>by<a href="https://www.learningrobo.com/"> Learning Robo</a></div> </section> <section id="services"> <h1>Services Section</h1> <p>Fixed Floating Navigation Bar to navigate though sections.</p> <div class="credit">Made with <span style="color:tomato;font-size:20px;">❤ </span>by<a href="https://www.learningrobo.com/"> Learning Robo</a></div> </section> <section id="contact"> <h1>Contact Section</h1> <p>Fixed Floating Navigation Bar to navigate though sections.</p> <div class="credit">Made with <span style="color:tomato;font-size:20px;">❤ </span>by<a href="https://www.learningrobo.com/"> Learning Robo</a></div> </section> <script src="script.js"></script> </body> </html>
CSS provides style to an HTML page. To make the page attractive create a CSS file with the name style.css and remember that you have to make a file with a .css extension.


* {
    margin: 0;
    padding: 0;
}

nav {
    position: fixed;
    bottom: 20px;
    width: 40%;
    background: #000000; 
    background: -webkit-linear-gradient(to right, #434343, #000000);
    background: linear-gradient(to right, #434343, #000000); 
    margin: 10px 30%;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
    border-radius: 50px;
}

nav ul {
    list-style: none;
    text-align: center;
    height: 100%;
}

nav ul li {
    display: inline-block;
    height: 100%;
}

nav ul li a {
    display: inline-block;
    color:#fff;
    text-align: center;
    padding: 16px;
    text-decoration: none;
    font-size: 20px;
    height: 100%;
    font-weight: 900;
    font-family: 'Aladin', cursive;
}
@media only screen and (max-width: 860px) {
    nav
     {
        width:60%;
        margin: 10px 20%;
    }
  }
  @media only screen and (max-width: 600px) {
    nav
     {
        width:80%;
        margin: 10px 10%;
    }
    nav ul li a{
        font-size: 17px;
        padding: 8px;
    }
  }
section {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

#home {
    background: #12c2e9;  /* fallback for old browsers */
    background: -webkit-linear-gradient(to right, #f64f59, #c471ed, #12c2e9);  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to right, #f64f59, #c471ed, #12c2e9); 
}

#about {
    background: #FDC830;  /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #F37335, #FDC830);  /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #F37335, #FDC830); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */

}

#services {
    background: #ec008c;  /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #fc6767, #ec008c);  /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #fc6767, #ec008c); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */

}

#contact {
    background: #00c3ff;  /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #ffff1c, #00c3ff);  /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #ffff1c, #00c3ff); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */

}

h1 {
    margin-bottom: 20px;
    font-size: 98px;
    color:#000;
    text-shadow: 3px 3px 0px #fff;
    font-family: 'Aladin', cursive;
}


p {
    font-size: 24px;
    color: #333;
}
@media only screen and (max-width: 860px) {
    h1
      {
         font-size: 38px;
     }
     p{
        font-size: 12px;
     }
   }
   @media only screen and (max-width: 600px) {
    h1
      {
         font-size: 38px;
     }
     p{
        font-size: 12px;
     }
   }
.credit a {
    text-decoration: none;
    color: #fff;
    font-weight: 800;
  }
  
  .credit {
      color: #fff;
    text-align: center;
    margin-top: 10px;
    font-family: Verdana,Geneva,Tahoma,sans-serif;
  }
JavaScript makes the page work functionally. At last, create a JavaScript file with the name script.js and remember that you have got need to make a file with a .js extension.


let navLinks = document.querySelectorAll('nav a');
let sections = document.querySelectorAll('section');

navLinks.forEach(link => {
    link.addEventListener('click', e => {
        e.preventDefault();
        const target = document.querySelector(link.getAttribute('href'));
        const targetTop = target.offsetTop;
        window.scrollTo({
            top: targetTop,
            behavior: "smooth"
        });
    });
});

window.addEventListener('scroll', () => {
    let current = '';
    sections.forEach(section => {
        const sectionTop = section.offsetTop;
        const sectionHeight = section.clientHeight;
        if (pageYOffset >= (sectionTop - sectionHeight / 3)) {
            current = section.getAttribute('id');
        }
    });
    navLinks.forEach(link => {
        link.classList.remove('active');
        if (link.getAttribute('href').includes(current)) {
            link.classList.add('active');
        }
    });
});

We hope you would like this One-Page Website with Fixed Floating Bottom Navigation using HTML, CSS & JavaScript.

Thank you for reading our blog. If you face any problem in creating this One-Page Website with Fixed Floating Bottom Navigation using HTML, CSS & JavaScript, then contact us or comment to us. We’ll try to provide a solution to your problem as soon as possible.

Press The Key ' p ' and say ' read article ' our voice assistant read our article.
In Our older post it doesnot work we working on that.

Thank you
Learning robo team

Post a Comment

Thank you
Learning robo team

Post a Comment (0)

Previous Post Next Post
Learning Robo says...
code copied
Welcome