Inoperable Sticky Navigation Bar in Bootstrap

I am in need of assistance with my navbar for a small test site project I am currently working on. The site is utilizing Bootstrap, and I am struggling to get the navbar to stick to the top of the screen as users scroll down. My goal is to have the navbar positioned beneath the jumbotron until it reaches the point where it would typically disappear off-screen. Once it starts moving off-screen, I want it to remain at the top of the page but return to its original position when necessary. I've attempted various JQuery code snippets like:

$('.navbar-default').affix({
  offset: {
    top: 50
  }
});

Any help with this issue would be greatly appreciated.

Below you can find the HTML Code:

<!DOCTYPE html>
<html lang="en">
<head>

  <title>Hope's Seed</title>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="JS/style.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link href="CSS/style.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">

</head>
<body>
<div class = "header">
  <div class="jumbotron text-center">
    <h1>Hope's Seed</h1>
    <p>You're Not Alone</p>
  </div>
</div>  

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>               
      </button>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Sponsors</a></li>
        <li><a href="#">Donate</a></li>
        <li><a href="#">Photos</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">Events</a></li>
        <li><a href="#">Blog</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="bg">
  <div class="container-fluid">
    <div class="row">
      <img class="img-responsive" src="Images/plantgrow.jpg">
    <div>
  </div>
</div>

</body>
</html>

This is my CSS Code (note that there may be some redundancies which haven't been cleaned up yet):

.header .jumbotron {
    color:#fff;
    background-color:#a1ff7c;
    font-family:Roboto Condensed;
    margin-bottom:0px;
}

.navbar-default {
    background-color: #78c45a;
    border: 0px;
    font-family:Roboto Condensed;
    margin-bottom:0px;
}

.navbar-default .navbar-nav > .active > a, .navbar-default .navbar-nav > .active > a:focus {
    color: white; /*BACKGROUND color for active*/
    background-color: #78c45a;
}

.navbar-default .navbar-nav > .active > a:hover{
    background-color: #5a9344;
    color: #fff;
}

.navbar-default .navbar-nav > li > a {
  color: #fff;
}

.navbar-default .navbar-nav > li > a:hover {
  color: #fff; 
  background-color: #5a9344;
}

.navbar-default .navbar-brand {
    color: #fff;
}

.navbar-default .navbar-brand:hover {
    background-color:#5a9344;
    color: #fff;
}

.navbar-default .navbar-toggle .icon-bar {
    background-color: #fff;
}

.navbar-default .navbar-toggle {
    border: 0px;
}

.navbar-default .navbar-toggle:hover {
    background-color:#5a9344;;
}

p {
    font-family: Roboto Condensed;
}

.bg .img-responsive {
    width:100%;
    height:auto;
}

.jumbotron {
    padding-top:1%;
    padding-bottom:1%;
}

.jumbotron h1 {
    margin-top:0px;
}

.jumbotron p {
    margin-bottom:0px;
}

.navbar-nav {
    display:table;
    width:100%;
    margin: 0;
}
.navbar-nav > li {
    float:none;
    display:table-cell;
    text-align:center;
}

.container-fluid{
    padding-left:0px;
    padding-right:0px;
}

#myNavbar{
    padding-left:0px;
    padding-right:0px;
}

Answer №1

If you want to create a fixed Bootstrap navbar, simply add the class navbar-fixed-top to your navigation bar:

<nav class="navbar navbar-default navbar-fixed-top">

It seems like you also want your custom green header to stay at the top of the screen, above the navbar. To achieve this, give the same class to the header and make sure to set a top offset equal to the height of the custom header:

<div class="header navbar-fixed-top">

.navbar.navbar-default {
  top: 155px;
}

You can see an example of this setup in action on this JSFiddle.

I hope this explanation helps! :)

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

Displaying a PHP variable on the console through the power of jQuery and AJAX

I'm attempting to display the value of a PHP variable in the browser's console using jQuery and AJAX. I believe that the $.ajax function is the key to achieving this. However, I am unsure about what to assign to the data parameter and what should ...

Google fonts are being blocked by Chrome version 37.0.2062.102m

I encountered the following issue while using the latest version of Chrome: Redirect at origin '' has been blocked from loading by Cross-Origin Resource Sharing policy: The request was redirected to a URL ('about:blank') which ha ...

Comparing jQuery and Yahoo UI API Design

I find myself puzzled by the contrasting design approaches of jQuery and Yahoo UI APIs. I must confess, I have a strong aversion to the jQuery API, but my knowledge in web programming and JavaScript is limited, so I could be completely mistaken and end up ...

Align the input labels to the right

In the demonstration below, an example is provided for aligning labels to the right of an input field. Is there a way to ensure that the input field takes up 100% of the width? Additionally, can an input be replaced with a textarea or another div element w ...

How can I incorporate multiple JSX files into plain HTML without using npm?

I have a question regarding importing JSX files in an HTML file without using npm and directly running it with LiveServer. I have two files, index.js and app.jsx, that I want to connect within my index.html script. How can I achieve this? Code: index.html ...

Issue with date not syncing properly when using a countdown with jquery.countdown

I've encountered a challenge with the following code snippet: $(document).ready(function() { $('.clocker').countdown("2018-09-30 01:00:00", function(event) { var totalHours = event.offset.totalDays * 24 + event.offset.hours; ...

The height of the div element is automatically adjusted to zero

I am dealing with a simple layout where I have a main div that floats to the left. Within this main div, I nest other divs using the clear both style. Below is a simplified version of my setup: <div id="wrapper" class="floatLeft"> <div id="ma ...

Rod running from one end to another within a receptacle

I'm facing a challenge with my container (CardContainer) and its child elements. I want to create a bar that fills the space between the card image and the chevron, but I'm having trouble figuring out the best approach. How can I ensure that this ...

Is it possible to delete all anchors except for their contents?

Occasionally, I have encountered an issue where this code generates the following error message: NotFoundError: An attempt was made to reference a Node in a context where it does not exist. $('a').replaceWith(function() { return $.text([this ...

The PHP code embedded within the HTML document and triggered by an AJAX request failed to

Here is an example of my ajax call: function insertModal(link) { $.ajax({ url: link, cache: false, dataType: "text", success: function(data) { $("#modalInput").html(data); }, error: function (request, status, error) { ...

Reactstrap: Is it necessary to enclose adjacent JSX elements within a wrapping tag?

While working on my React course project, I encountered an issue with my faux shopping website. The error message that keeps popping up is: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...& ...

Using AJAX to fetch data for Highcharts illustration

Hello there! I am attempting to create a chart based on data retrieved via AJAX. The values for the chart are coming from the AJAX return. Here is the data that I have: "tgl":["2016-07-12 00:00:00.000","2016-07-01 00:00:00.000"],"total":[1283947,12346185 ...

Interested in integrating Spring MVC with React Bootstrap or Material UI?

Can a Spring MVC application be used to serve React Bootstrap or Material UI webpages alongside the Spring application? If yes, which specific aspects should I focus on to make this work seamlessly? ...

Submitting PDF to server using AJAX and converting it from binary data using jsPDF

I've been trying to pass a PDF generated using jsPDF on frontend JavaScript to a Spring Framework MVC backend. Below is the code I've used on the frontend: var filename = "thefile"; var constructURL = '/daas-rest-services/dashboard/pdfPrint ...

Dominating React Components with Unique CSS Styles

Currently, I have developed a NavBar component. I've implemented some JavaScript code that changes the navbar's background color once it reaches 50px. However, I am facing an issue in applying this scroll effect to only one specific file and not ...

Why is the response from this HTTP request displaying array lengths instead of actual content?

I am currently working on a project involving fetching data asynchronously from an API. Everything seems to be working fine, except for when I attempt to add the correct answer to the incorrect answers array. Instead of displaying the content, only the len ...

ASP.NET can have issues with the functionality of jQuery AJAX

I'm currently working on an asp.net webform project and I'm looking to implement jQuery ajax functionality. Below is the code snippet I have so far: <asp:Button ID="btn_comment" runat="server" CssClass="contact_btn pull-right" Text="send" OnC ...

Locate grandchildren elements using getElementById

My task is to modify the content and attributes of the table. The DOM structure is generated by a third-party tool, and I am using JavaScript to make changes in various sections. <div id="myId"> <div> <div> <table&g ...

Increase the size of a collapsible div without affecting the navbar

I have a complex layout consisting of a navbar, a collapsible div, and a table within a container. My goal is to make the collapse div appear when clicking on a row in the table. However, I'm facing an issue where the entire structure of the page brea ...

Swapping elements using jQuery

My PHP script generates a table with multiple rows of items. Each row displays different content, including an image of emptybox.jpg in the first column, and a link to select the item in the last column: output .= '<a href="./select_item.php id=se ...