What could be causing the nav bar position=sticky to not function properly?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

<nav>
    <div class="left-nav">
        <ul>
            <li><img src="" alt="logo"></li>
            <li><input type="search"></li>
            <li><img src="" alt="hit"></li>
        </ul>
    </div>

    <div class="right-nav">
        <ul>
            <li><img src="" alt="dp"></li>
            <li>Name</li>
            <li>i1</li>
            <li>i2</li>
            <li>i3</li>
            <li>i4</li>
            <li>i5</li>
        </ul>
    </div>
</nav>

<main>
    <div class="left-content">
        <p>Left Content</p>
    </div>
    <div class="center-content">
        <p>Center Content</p>
    </div>
    <div class="right-content">
        <p>Right Content</p>
    </div>
</main>


</body>
</html>

I'm having trouble with making the navigation bar sticky in my web development project. When I set the position to 'fixed', it works fine, but I want it to be sticky at the top as we scroll, similar to Facebook's navigation bar. I'm trying to create a Facebook clone as a beginner learning web development. The first code snippet represents the HTML structure, while the second one contains the CSS styles.

Answer №1

To ensure the navigation bar stays at the top of the page and is visible above other elements, you can add the following CSS properties: top:0; and z-index: 9999;

* {
  padding: 0;
  margin: 0;
}

nav {
  background-color: rgba(52, 52, 146, 0.829);
  display: inline-flex;
  justify-content: space-around;
  align-items: center;
  width: 100%;
  height: 45px;
  position: sticky;
  z-index: 9999;
  top: 0;
}

ul {
  display: inline-flex;
  list-style-type: none;
}

navl {
  border-color: red;
  border-style: dotted;
  align-items: center;
}
navr {
  border-color: red;
  border-style: dotted;
}

main {
  justify-content: space-between;
  display: flex;
}
left {
  border-color: red;
  border-style: dotted;
  width: 30%;
  height: 400px;
  display: inline-flex;
  position: sticky;
}
center {
  border-color: red;
  border-style: dotted;
  width: 35%;
  height: 5000px;
  display: inline-flex;
}
right {
  border-color: red;
  border-style: dotted;
  width: 30%;
  height: 300px;
  display: inline-flex;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <nav>
        <navl>
        <ul>
            <li><img src="" alt="logo"></li>
            <li><input type="search"></li>
            <li><img src="" alt="hit"></li>
        </ul>
        </navl>

        <navr>
            <ul>
                <li><img src="" alt=""dp></li>
                <li>Name</li>
                <li>i1</li>
                <li>i2</li>
                <li>i3</li>
                <li>i4</li>
                <li>i5</li>
            </ul>
        </navr>
    </nav>
    <main>
        <left>
            <span>left</span>
        </left>
        <center>
            <span>center</span>
        </center>
        <right>
            <span>right</span>
        </right>
    </main>


</body>
</html>

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

What is the process of inserting JavaScript information into a MySQL database?

Can anyone provide guidance on how to insert this JavaScript data into a MySQL database? <script type="text/javascript"> var count = 0; function countClicks() { count = count +1 ; document.getElementById("likes").innerHTML = coun ...

Adjust the color to a texture that is capable of casting shadows

I am currently working on setting up a lighting system for my scene to create shadows. However, I am facing an issue where changing the color of the material is not producing the desired effect of displaying shadows. Is there a way to modify the color of a ...

A continuous loop of text is displayed in a slideshow format, with different image files used for the navigation buttons "back", "stop", and "forward"

I am in need of a unique slide show that displays text exclusively attached to CSS, with image files for the navigation buttons. I have been searching for days without success, as most options available are for image slideshows and not suitable for my text ...

What is the best way to change the class name using useState in order to trigger the required CSS conditions? Updating the tab ID appears to be

Just starting out with react/typescript (my third project). I'm currently working on a custom dynamic tab component. In this component, I dynamically create a class name to determine which tab is active and which is not. It seems to be functioning pr ...

TypeScript interface with an optional parameter that is treated as a required parameter

Within my interface, I have a property that can be optional. In the constructor, I set default values for this property, which are then overridden by values passed in as the first parameter. If no properties are set, the defaults are used. I am looking fo ...

Guide on adjusting the darkness of a MaterialUI Avatar component image and adding text to it

I'm currently working with the Avatar component from Material UI along with Tailwind CSS. I found that by simply adding the image URL to the src, it displays the avatar successfully. <Avatar alt="Cindy Baker" src="https://mui.com/sta ...

The heights of the dropdown menu and button vary in size within the Dash Plotly Python framework

I'm currently working on creating a dashboard using dash in Python and I need help aligning 2 dropdowns and a button horizontally. This is the html.Div code snippet that I have been working on: html.Div([ html.Div( dcc.Dropdown( ...

What is the best way to choose the next adjacent element using a CSS selector with Python Selenium?

The structure of the DOM is as shown below: <ul> <li> <a href="#" role="button" class="js-pagination link" data-page="1">1</a> </li> <li> <a href="#" role="button" class="js-pagination link active" data ...

Can the Automator tool capture a specific section of a website in a screenshot?

Is it possible to use automator to navigate through a store's website category and open each product in a new tab? Alternatively, is there a software that can extract all the product URLs from a specific website? For example, this website has 14 prod ...

Angular 7 Material: How to perfectly center align your cards side by side

When I have an abundance of material cards, they tend to align vertically. Is there a way to align the cards horizontally from left to right instead? ...

Delay loading all images on the page except for the first one until the page is completely loaded

My website has a slider feature that displays images in a sequence, but currently all the images load at once and then get hidden by a script after the page is fully loaded. I tried using CSS to set a fixed height for the slider, which worked on desktop bu ...

Troubleshooting Intel Edison application update issues

After setting up the IoT XDK, I decided to try out the blink example. Excitedly, I saw that the LED on pin 13 was blinking just as expected! But when I attempted to change the interval from 1000ms to 100ms and 3000ms, there was no difference in the blinkin ...

Issue with Orgchart JS: The requested resource does not have the 'Access-Control-Allow-Origin' header present

Currently, I am developing a program to create organization charts using orgchart.js and simple PHP. This project does not involve any frameworks, but unfortunately, I encountered the following error: CORS policy is blocking access to XMLHttpRequest at & ...

Improve the organization of my JavaScript code

This code snippet is specifically designed for a shoe website, where it automatically adds the shoe in the desired size to your cart as soon as the page loads. The "skuAndSize" variable represents the shoe size used by the website. In this case, a selecte ...

Using the <hr> tag in HTML to create a horizontal line is proving to be a challenge for me

.yellow-line{ position: absolute; top: 60px; overflow: auto; border: 10px solid red; } <img class="vox-logo" src="images/vox_logo.JPG" alt="Vox Logo"> <div class="header"> <h5 class="menu-0">EXPLAINERS</h5> ...

A mesh from Maya that has been exported with its skin duplicated within a Three.js environment

After exporting an animated skinned mesh from Maya using the Three.js exporter, I noticed that there is always a duplicate mesh combined with it that remains static and not animated. Despite trying various export settings and ensuring that no other mesh i ...

Require the header text to appear black on a white background and white on a dark background

As someone who is not experienced in coding, I have tried multiple methods without success. It seems like none of the solutions available are compatible with my Squarespace theme. Any assistance you can provide would be greatly appreciated! Here is a link ...

Tips on integrating Next/Previous functionality for a modal Ajax popup

I have been utilizing a bPopup jQuery plugin to display ajax HTML files in a modal popup on my website. However, I am interested in enhancing this functionality to allow users to seamlessly navigate to the next slide within the modal without having to exit ...

Attempting to retrieve data from an object by utilizing an external URL, however

Upon starting the bot console, the console displays: Online with undefined/5 After 10 seconds, an error is thrown: undefined:1 [object Promise] ^ SyntaxError: Unexpected token o in JSON at position 1 This is the code snippet being used: let players clie ...

"Unraveling the Mystery: In Javascript Vue, what is the secret behind the origin of the variable 'e' found in the function

Where does the mysterious variable e in onFileChange(e) come from? In the code snippet below, we see a variable e being used in onFileChange(e). However, this variable is not declared or imported anywhere in the code. How is it possible for this to be val ...