"Sticky navigation bar that remains fixed while scrolling, even when the navigation is not at the top of the

My webpage includes a navigation element that initially is not positioned at the top of the page. However, I want it to stick to the top of the page when I start scrolling down and return to its previous position when scrolling up. Unfortunately, I am unsure how to achieve this functionality. Below are the relevant sections of my code:

<div class="container-fluid">
    <div class="row">
        <header id="Home" class="header header-top">
            <div class="header-top-p col-lg-4 col-md-4 col-sm-4 hidden-xs">
                <p>text</p>
                <p>text</p>
            </div>
            <div id="logo-header" class="col-lg-4 col-md-4 col-sm-4">
                <a href="#Home"><img src="Content/images/logo.png" width="260" height="240"></a>
            </div>
            <div class="header-top-p col-lg-4 col-md-4 col-sm-4 hidden-xs">
                <p>text</p>
                <p>text</p>
            </div>

        </header>
        <nav id="header-nav">
            <div class="openMenu"><i class="fa fa-bars"></i></div>
            <ul class="mainMenu">
                <li><a href="#Home">item1</a></li>
                <li><a href="#AboutMe">item2</a></li>
                <li><a href="#Resume">item3</a></li>
                <li><a href="#Skills">item4</a></li>
                <li><a href="#Contact">item5</a></li>
                <li><a href="#Comment">item6</a></li>
                <div class="closeMenu"><i class="fa fa-times"></i></div>
                <span class="icons">
                    <a href="#"><i class="fab fa-instagram"></i></a>
                    <a href="#"><i class="fab fa-twitter"></i></a>
                    <a href="#"><i class="fab fa-github"></i></a>
                    <a href="#"><i class="fab fa-linkedin"></i></a>
                </span>
            </ul>
        </nav>
    </div>
</div>

<style>
.header {
    text-align: center;
}

.header-top {
    font-size: 4.7rem;
    font-weight: 500;
    height: 260px;
    background: url("../images/bg1.jpg") no-repeat;
    background-size: 100% 260px;
}

.header-top-p {
    padding: 60px 0 40px;
    animation: names-header var(--names-header-time) ease-out;
}

#logo-header {
    height: 240px;
    margin: 0 auto;
    animation: var(--logo-header-time) ease-in-out 900ms 1 logo-header;
    animation-fill-mode: backwards;
}

#header-nav {
    background-color: yellow;
    color: #333300;
    justify-content: space-between;
    height: 70px;
    background-image: linear-gradient(to right, #ffffb3, #ffff66, #ffff00, #b3b300);
    width: 100%;
    margin: 0 auto;
    transition: .4s ease-out;
    animation: nav-load var(--nav-load-time) ease-in;
    align-items: center;
    z-index: 100000;
}

nav .mainMenu {
    list-style: none;
    display: flex;
}
#header-nav ul li {
    position: relative;
    zoom: 1;
    -webkit-backface-visibility: hidden;
    vertical-align: middle;
    margin: 0 auto;
    transition: .4s ease-out;
}
nav .mainMenu li a {
    font-weight: bold;
    color: #333300;
    padding: 22px;
    display: inline-block;
    text-decoration: none;
    font-size: 2rem;
}
nav .mainMenu .closeMenu, .icons i {
    font-size: 2rem;
    display: none;
    cursor: pointer;
}
</style>

I am utilizing Bootstrap 3 in my project which might be helpful information. Essentially, I need the navigation to become fixed at the top of the page as I scroll down, hiding the 'header top' section, and reappear as I scroll back up. Any assistance would be highly appreciated.

Answer №1

position: sticky;

If you want to experiment with this property, check out this resource for more information here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the text of the chosen option, rather than the value

Having a multiple select, my goal is to retrieve the selected text rather than the value. For example, if I have this: <option value="1100"> MH-03-P-9429</option> I want to extract MH-03-P-9429 as the selected text. However, when I use $(" ...

Having trouble turning octet-stream response data into a downloadable Zip file

I've been attempting to download a Zip file that contains JSON files through an AJAX GET request. Here is the format of the response headers: Connection: keep-alive content-disposition: attachment;filename=exportedFiles_ 1.6.0_20200731160652.zip Cont ...

Unable to pass data using $.post() method

I am trying to send some data as a variable using jQuery's $.post() method to a PHP file and then display the result in a div after clicking a button. Unfortunately, I'm facing an issue where the data isn't being retrieved in the PHP file. ...

Is implementing an API necessary for handling this basic event in Express JS?

Lately, I've been encountering challenges with the backend, particularly when working with Express JS. I realize that part of the problem might be my own misunderstanding - assuming that Express JS is a framework generator for MVC or solely for authen ...

Error message 23000: The integrity constraint has been violated. The column "page_id" cannot have a null value in Laravel version 5

Currently, I am using Laravel 5.2 along with jQuery AJAX to work on an application where I need to pass a large amount of data through AJAX requests. However, when dealing with larger datasets, I encountered the following error: SQLSTATE[23000]: Integrity ...

"Unexpected discrepancy: Bootstrap Glyphicon fails to appear on webpage, however, is visible

I am having some trouble getting the glyphicon to display properly in my side nav. The arrow head should rotate down, which is a pretty standard feature. Here is the link to the page: The glyphicon should be visible on the "Nicky's Folders" top leve ...

Keep deciphering in a loop until the URI string matches

I have a task to decode a string URI until there are no more changes. The string URI I am working with typically has around 53,000 characters, so the comparison needs to be fast. For demonstration purposes, I have used a shortened version of the string. B ...

Using Vue 2 with Webpack works seamlessly for one application but encounters issues when used for another

I've been struggling with this problem for days now. I'm working on a project that consists of two single-page Vue applications or a two-page application as some might call it. These apps have common functionalities which have been extracted into ...

What's the best location to input a script that will trigger instantly when a DropDown value is altered?

In my web application built on asp.net using vb.net, I have encountered an issue on a UI Screen where a drop down triggers a post back on onSelectedIndexChange. The dilemma at hand is that I need to prevent user interaction with the entire UI when onSelec ...

Place the outcome of the function into the div element's attribute

As a newcomer to HTML and JavaScript, I recently dove into using 3Dmol.js. Following a tutorial, I was able to create this code snippet that actually works: <script src="http://3Dmol.csb.pitt.edu/build/3Dmol-min.js"></script> <div id="el ...

Harnessing the Power of Material UI to Revolutionize Amplify Theming

I am currently developing a React application with Material-UI and Amplify UI Components. I am looking to customize the Amplify theming. By following the guidance provided on Amplify UI Components Customization, I have been able to override CSS variables ...

Is there a way to check the existence of an email in the database without having to refresh the PHP page?

Currently, I am using the following method to validate my inputs: <form method="post"> <label>Email</label> <input type="email" name="user_email"> <input type="submit" name="reg"> </form> if(isset($_POST['reg' ...

Using the '&' character in an AJAX request parameter: A step-by-step guide

When passing form data to PHP using jQuery, the PHP code saves the data to a MySQL database. However, if the title is "how to save & to mysql database using PHP", it gets stored in the database as "how to save". How can I resolve this issue? Below is my J ...

What causes the excess spacing in flexbox containers?

Is there a way to remove the padding around the container and between the two columns? I attempted setting margin and padding to 0, but it was not successful. https://i.sstatic.net/i5XJJ.jpg body, html { width: 100%; height: 100%; } main { disp ...

jQuery causing YouTube object template to not display when links are added

After storing an object template in a variable, I am using jQuery to add video links. This is being done in preparation for adding the links into the object template through ajax in the future. However, the video is not appearing as expected and I'm ...

Ensure the most recently expanded item appears at the top of the TreeView by updating to Mui version

I'm working on a feature where I want to scroll the expanded TreeItem to the top when it has children and is clicked on for expansion. Any suggestions on how to make this happen? ...

Updating data in a table using identifiers from another table

I would greatly appreciate any assistance in advance! Within my database, I have two tables - var table and val table. var table varid image vartype val table valid sig winner varid These tables are related in a one-to-many relationship, indicating t ...

When a div is added, the alignment of Bootstrap 4 elements becomes skewed

Desired Appearance: https://i.sstatic.net/jYCiq.png Undesired Appearance: https://i.sstatic.net/m3emV.png I recently encountered an issue with my registration form where it started to look different from my login form even though I only added extra fie ...

Struggling with a Javascript problem where a button appears to be unresponsive when clicked. How can I fix this issue

I'm facing an issue with a web interface I am designing for configuring remotes to control ESP32 functions. The problem seems to be related to the Javascript part of the interface, as the code on the ESP32 is working fine. Here's how it works: th ...

Exploring the Latest PHP Popup Upgrade

Hey everyone, I'm currently facing an issue with updating my MySQL database. I have a popup window that is supposed to update it without any errors, but for some reason, it's not actually updating the database. Can anyone help me figure out what ...