Issue with Bootstrap Scroll Spy and Sticky Navbar integration not functioning correctly

Encountering an issue with the Bootstrap Scrollspy component.

Using a sticky-top navbar and when the nav button is clicked, it scrolls to the correct element;

However, the problem lies in the fact that the sticky navbar overlays this element.

Tried utilizing data-offset = "50" in the body tag, but it had no effect.

Body tag snippet:

<body data-spy="scroll" data-target="#sectionsNav" data-offset="50">

CSS for the body tag:

body {
  position: relative;
  overflow-y: auto;
}

The navbar:

<nav class="navbar navbar-light bg-light sticky-top">
        <div id="sectionsNav">
            <ul class="nav nav-pills text-center">
                <li class="nav-item">
                    <a class="nav-link" href="#wihe">What is Home Eats</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#hiw">How it Works</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#pws">Problems we Solve</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#jhen">Join Now!</a>
                </li>
            </ul>
        </div>
    </nav>

Section where How it works div is located:

..
<div id="hiw" class="container">
...
..
.
</div>
..

Expected result when clicking on How it works button in the navigation bar:

https://i.sstatic.net/JaL9e.png

Current behavior observed:

https://i.sstatic.net/Y5ztW.png Note: The navbar appears above the How it Works header.

Edit

Following @SMAKSS solution, the scroll worked perfectly. However, another issue arose where the highlighted element in the navbar becomes the previous one.

In the screenshot below, after clicking on How it Works, the correct scroll occurred, but the highlighted navbar element remained as What is Home Eats which is the previous selection. https://i.sstatic.net/4PkaL.png For example, if I click on Problems we Solve, How it Works gets selected instead. It consistently highlights the previous element.

Edit Solution

Solved the secondary issue by doubling the value of the data-offset attribute to 100. Updated code snippet looks like this:

<head>
 <style>
    html {
    scroll-padding-top: 70px;
    }

    body {
    position: relative;
    overflow-y: auto;
    }
 </style>
</head>

<body data-spy="scroll" data-target="#sectionsNav" data-offset="100">
...
.
</body>

Answer №1

In this scenario, a challenge arises when a element with a position of fixed is introduced. Despite still navigating to the desired id, it fails to acknowledge the presence of the fixed element. Consequently, it disregards its height and lands wherever the element matching the specific id aligns with the window's top content.

To address this issue, it is crucial to incorporate padding into your scroll transitions as demonstrated below:

html {
  scroll-padding-top: 70px; /* height of sticky header */
}

Explore more about scroll-padding on CSS-Tricks.

UPDATE

Upon further exploration of this solution, challenges arose in terms of spying behavior as the scroll spy solely identifies our location within a section with a specific ID. To tackle this, various approaches can be adopted such as incorporating padding based on navbar height for each section or following @Raamyy's suggestion of defining data-offset on the body tag to accommodate the height of the fixed navbar. Refer to bootstrap scrollspy options for detailed insights on data-offset.

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

Is there a way to update my profile picture without having to constantly refresh the page after uploading it?

Here is the code for my profile page. I am considering using a callback along with another useEffect function, but I'm unsure. Please help me in finding a solution. For now, let's ignore all the code related to deleting, saving, and handling ingr ...

Exploring ways to style font families for individual options within ng-options

I am looking to display a combobox where each option has a different font. While using the ng-options directive in AngularJS to populate the options for a <select> tag, I am struggling to set the font-family for individual options. $scope.reportFon ...

Angular material tables are displaying borders that do not extend the full length

I am currently working on designing a visually appealing table. Due to the upcoming content in the matrix section, I am unable to make the table smaller for mobile devices. Therefore, I have added overflow:auto to enable a scroll-bar. The issue I am facin ...

What is the best way to ensure that Bootstrap columns are spacious enough to fit advertisements?

Using Bootstrap 5, I have created a page layout with 3 columns: <div class="container"> <div class="row"> <div class="col-md-3">LEFT</div> <div class="col-md-6">CENTER</div& ...

Retrieve the blob value from outside the canvas by using the ToBlob() function asynchronously

I have encountered an issue with the HTMLCanvas element where it returns a blob object outside of the async toBlob() function. Since this function does not return an output value, I have attempted to declare a variable outside and then access it through th ...

Trouble obtaining AJAX result using onClick event

As a newbie to AJAX, I am still trying to grasp the concept. My task involves using an AJAX call to extract specified information from an XML file. Even after carefully parsing all tag elements into my JavaScript code, I encounter a problem when attempting ...

What is the process for printing a page in plain text format on a matrix-dot printer?

I've come across many similar questions on SO about this issue, and I've tried various solutions without success. I'm using an Epson LX300+II printer and I want to print pages from my web application in a plain format, like how it would look ...

Customize bootstrap cards to have a full height and add a bottom margin to enhance the

My current project involves creating a grid of Bootstrap 4 cards that need to meet specific requirements: All cards must be contained within a single class="row" div. This is because the number of cards is unknown and I want the layout to adjust well acr ...

Utilize the JMX HTML adapter in conjunction with Apache Camel's JMX capabilities

I'm currently utilizing the Sun HTML JMX Adapter to display Camel's exposed JMX beans in an HTML format. I've been working with Spring XML to achieve this, but I'm struggling to establish a connection between the adapter and the JMX age ...

Dynamically updating the scroll area in Ionic using real-time data

Hello, I am currently working on an Ionic project and have created a code pen for it. At the moment, the code pen just displays an image with two buttons for zooming in and out. However, I have encountered an issue. When I click on zoom in and scroll to ...

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...

How can I create a popup window that meets standards using XHTML and Javascript?

As I adhere to writing standards compliant XHTML Strict 1.0, I refrain from using the HTML "target" attribute on anchor elements. I have come across 2 distinct methods involving Javascript: One approach involves locating all links with rel='exter ...

Use jQuery to dynamically add a css class to a Bootstrap Card element only after the last checkbox inside has been checked

I've successfully implemented a jQuery script that adds one class and toggles another when a checkbox is checked. It functions as expected with single checkboxes, but I'm encountering issues when multiple checkboxes are placed inside. I want the ...

Spinning Circle with css hover effect

I have recently developed a simple website: Within this website, there is an interesting feature where a circle rotates above an image on hover. However, despite my best efforts, I couldn't make it work as intended. Here's the code snippet I use ...

Default value not chosen in PHP dropdown

Here is my PHP code snippet: <?php require_once('../Connections/baglanti.php'); $settings = $db -> prepare("SELECT * FROM mestan_settings"); $settings-> execute(array()); $settingswrite = $settings->fetchAll(); foreach($sett ...

Limiting style changes to specific labels in Highcharts

Can someone assist me in bolding only the middle elements of a highcharts x axis? I have tried to apply a solution in the provided fiddle but it doesn't work as expected. Specifically, I am looking to bold the x-axis labels for June or July. http://j ...

Building a 3D Earth model using three.js for WebGL applications

I found a code online, but it doesn't seem to work. Can someone provide me with the HTML code needed to make it run in my browser? The original code was sourced from this website (github repository) Github code repository https://github.com/turban/ ...

How can you create a table cell that is only partially editable while still allowing inline JavaScript functions to work?

Just a few days back, I posted a question similar to this one and received an incredibly helpful response. However, my attempt at creating a table calculator has hit a snag. Each table row in a particular column already has an assigned `id` to transform t ...

There seems to be a problem with the while loop in the PHP code while creating a forum

Encountered a new error that says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND type='o'' at line 1. Can someone assist in fixing this issu ...

Adjust the size of dynamic images based on varying widths and heights

Currently, I am facing an issue with scaling images in the header sourced from a database. The dimensions of these images can vary but are limited to a maximum size of 2000x2000px. When attempting to scale these images, particularly those over 1000px, they ...