My attempt to implement a sticky position is not functioning as expected

I've encountered an issue with my code using bootstrap. Despite setting the top position to 0, it's still not working. Any ideas on why this might be happening? Here's the code snippet:

<style>
    .background-nav {
        background: #58585858;
        backdrop-filter: blur(45px);
        border: 3px solid #808080;
    }
    .t-0 {
        top: 0;
    }
</style>
<div class="w-100">
    <div class="row justify-content-around position-sticky t-0 p-0 m-0">
        <div class="col-2 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Home</div>
        <div class="col-7 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Search</div>
        <div class="col-2 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Music</div>
    </div>
</div>

Here is the main portion of the code:

<div id="navbar"></div>
<section>
    <a href="http://localhost:3000/login">login</a>
</section>
<button style="background: red; color: white" id="Shutdown">SHUTDOWN</button>
<div class="meow">.</div>
<div class="meow">....</div>
<div class="meow">....</div>
<div class="meow">....</div>
<div class="meow">....</div>
<div id="footer"></div>

The top code will be replaced with #navbar and here is the CSS for the main code:

.meow {
            height: 5000px;
            width: 200px;
            display: block;
            background: red;
        }

I have attempted setting the top to 0 or changing the position to sticky with the !important tag without success.

Answer №1

To ensure proper styling, it is recommended to wrap your elements in a container before adding Bootstrap classes. Here's an example:

<style>
    #container{
      position: sticky; top: 0;
    }
    .background-nav {
        background: #58585858;
        backdrop-filter: blur(45px);
        border: 3px solid #808080;
    }
    .t-0 {
        top: 0;
    }
</style>

<div id="container">
<div class="w-100">
    <div class="row justify-content-around position-sticky t-0 p-0 m-0">
        <div class="col-2 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Home</div>
        <div class="col-7 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Search</div>
        <div class="col-2 text-center p-2 rounded-2 position-sticky t-0 mt-3 background-nav">Music</div>
    </div>
</div>
</div>

Answer №2

If you want an element to stick in place, it needs to be within a scroll container. To achieve this, wrap your navigation and content inside a div with overflow: auto, then apply position: sticky and top: 0 to the navigation. Adding z-index: 2 and 'background: white' can also help keep the navigation visible and in the forefront.

For further information, check out mdn position

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 proper way to assign a class name to an animation state in Angular 2/4?

I am currently working with Angular Animations using version 4.1.3 Check out the code snippet below: @Component({ selector : 'my-fader', animations: [ trigger('visibilityChanged', [ state('true' , style({ opaci ...

What is the best way to showcase JSON data while incorporating HTML tags in a React application?

My JSON data contains HTML tags interspersed within it and I have successfully fetched and displayed the data. See the code snippet below: componentDidMount() { this._callCake(); } _renderEachCake = () => { return ( <EachCake ...

Clicking on the checkbox will trigger an AJAX request to cache the

Encountering a problem with an old system I am currently updating: In the <div id='list'>, there is a checkbox list. Upon clicking a checkbox, it triggers an ajax request that returns JavaScript to execute. The JavaScript in the Ajax requ ...

Adding information into a separate row via JavaScript

When using XMLHttpRequest to retrieve data from a custom URL, specifically mocki.io and fake JSON, I am encountering an issue where all data elements (element.name and element.city) are being placed in one table row. Ideally, I would like each pair of name ...

What is the best way to implement CSS in this JavaScript Fetch code in order to manipulate the text's position and font style

Hello, I am just starting out with JS. Is there a way for me to customize the position and font of text in this JS Fetch function using CSS? Any help will be greatly appreciated. let file = 'art.txt'; const handleFetch = () => { fe ...

Steps to update the border color to red when a button is clicked

I have an interesting question regarding validation and forms. I decided to use my own class, but I noticed that when the button is clicked, the border color does not display anymore. I tried testing with CSS for invalid entries, and it worked. Now, I wan ...

The way Firefox interprets em values is incorrect

Currently, I am working on an adaptive website where I have used em values for everything. However, I am facing discrepancies between Chrome and Firefox when it comes to interpreting em values. The website can be found at dev.morodeer.ru/stroymoidom and ...

Design a captivating Profile Picture form using HTML and CSS styling

In my form, I am looking to include a user's Profile picture which should be displayed in either a circular or rectangular shape. Initially, the image area will show a black background or a placeholder image. When the user clicks on this area, they sh ...

Attempting to eliminate the vertical scroll on my page that matches the height of the navigation bar

I am struggling with the alignment of a login page form that needs to be centered both horizontally and vertically, while allowing for slight scrolling down to accommodate the navigation bar height. I have attempted to adjust the classes and style height ...

Mobile website menu selection

I want to create a select menu for my mobile website. I have added the select HTML code to my page. <select id="menu_mobile"> <option value="" selected="selected">Navigation</option> <option value="http://example.com/about"> Ab ...

What's the deal with inline blocks and text in the middle?

Check out this code I created for a navigation bar. HTML : <div class="header-nav"> <div class="header"> <img src="../Pictures/LifeFrame/2.jpg" width="100%" height="100px" /> </div> <div class="nav" align="c ...

Difficulty with JQuery Show and Hide Functionality

I've implemented a jQuery menu, but encountering the issue where clicking on any menu link opens all menus instead of just the one clicked. I've attempted to resolve this by using show and hide classes, however, it seems that nothing is working n ...

Modifying the color of an SVG shape with a checkbox toggle

Is there a way to dynamically change the fill color of an SVG shape by clicking on a checkbox? I have managed to achieve this functionality, but when I enclose the checkbox input in a div tag, it stops working. #circles { fill: #00ffff; } #circ-toggl ...

Retrieve the position with respect to the browser viewport

My current scenario involves two divs, with one nested within the other. The task at hand is to determine the position of the child div in relation to the browser window. Specifically, I aim to detect when the user scrolls down and have the child div fade ...

Adjusting the width of the container <div> will automatically resize the inner <div> to match

I have a main container element. .wrapper{ border:1px solid blue; position:relative; top:20%; left:30%; height: 300px; width: 350px; } When I adjust the width of the parent container, the child box does not reposition itself accor ...

Creating a custom button in PHP

Currently, I am in the process of developing a shopping cart feature. One key requirement is to send both the product ID and the desired quantity chosen by the user to another file named cart.php. Below is an excerpt from my code snippet: for($i=0;$i< ...

What is the best way to validate a particular class in javascript?

Need help checking if a specific id has a particular class. Unsure of the process? Here's the code snippet where the attempt at checking the id for a specific class is visible within the homeTransition function. function homeTransition() { ...

Storage within the Ionic framework app

Looking to develop an app that includes a folder of videos, but after hours of searching I still can't locate the path for the video ("triunghi.mp4"). Below is what I have so far: https://i.sstatic.net/r5RGz.jpg Any advice on what might be wrong or ...

Adjusting the height of the v-footer

I'm having trouble adjusting the height of the v-footer component. According to the documentation, I should be able to use the height prop, but when I try height="100px" or height="50%", the footer doesn't change. What could be causing this issue ...

What is the best method to display and conceal a table row in Angular 6?

In the midst of developing a table with multiple rows, I am seeking a way to invisibilize a particular row and then reveal it again using a reset button. Any tips on how to accomplish this task?emphasis added ...