Implement a top bar above the navigation bar using Bootstrap

Attempting to construct a webpage from scratch for the first time, without relying on a template. I aim to implement a fixed top bar above the navigation bar. Referencing this example:

This is my current code:

window.onscroll = function() {
  scrollFunction();
};

var first = true;

function scrollFunction() {
  if (document.body.scrollTop > 40 || document.documentElement.scrollTop > 40) {
    document.getElementById("navbar").classList.add('fixed-navbar');
  } else {
    document.getElementById("navbar").classList.remove('fixed-navbar');
  }
}
.navbar {
    position: absolute !important;
    width: 100%;
    transition: all .5s;
}

.navbar-nav>li {
    padding-left: 10px;
    padding-right: 10px;
}

... (remaining CSS code omitted for brevity)

I am looking to achieve a similar effect as seen in the example provided. How can I create a transparent top bar above the nav utilizing Bootstrap functionalities?

Edit:

The addition of the following code above the nav results in a non-transparent appearance, while placing it inside the nav does not yield the desired effect:

<div class="d-block">
    <div class="container">
        ... (additional HTML code omitted for brevity)
    </div>
</div>

Answer №1

I have put together a demonstration for you to view at the following link: https://codepen.io/shabbirvaghela/pen/OJbEeEp

The updated code is displayed below, separating the top bar and menu list into two rows as shown in the code snippet.

header {
  position: absolute !important;
  width: 100%;
  transition: all 0.5s;
}

.top-bar {
  padding: 15px;
  color: #fff;
}
.navbar {
  transition: 0.3s all;
}
header {
  position: absolute !important;
  width: 100%;
  transition: all 0.5s;
}

.top-bar {
  padding: 15px;
  color: #fff;
}
.navbar {
  transition: 0.3s all;
}

<header>
  <div class="top-bar">
    top bar details here
  </div>
  <nav id="navbar" class="navbar navbar-expand-sm">
    <a class="navbar-brand ml-4" href="#">
      <img src="http://via.placeholder.com/30x30" alt="" width="30" height="90" class="d-inline-block align-top logo-light" id="navlogo">
      <img src="http://via.placeholder.com/30x30" alt="" width="30" height="90" class="d-none align-top logo-dark" id="navlogo">
    </a>
    <button class="navbar-toggler collapsed border-0" id="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample04" aria-controls="navbarsExample04" aria-expanded="false" aria-label="Toggle navigation">
      <span> </span>
      <span> </span>
      <span> </span>
    </button>
    <div class="navbar-collapse collapse text-right" id="navbarsExample04">
      <ul class="navbar-nav ml-auto mr-4 text-uppercase">
        <li class="nav-item active">
          <a class="nav-link" href="#">one</a>
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="#">two</a>
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="#">three</a>
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="#">four</a>
        </li>
        <li class="nav-item active">
          <a class="nav-link" href="#">five</a>
        </li>

      </ul>

    </div>
  </nav>
</header>

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

Creating a responsive single webpage using HTML/CSS

How can I make some div or img responsive for mobile format? <meta name="viewport" content="width=device-width, initial-scale=1" /> <div class="container"> <h1 class="title"> ...

The Safari browser displays the mobile-friendly version of the website

Everything appears correctly when looking at the website in Firefox and Chrome. However, Safari is displaying the mobile version of the site instead. I did not make any CSS changes specifically for desktop screens while working on the responsive design for ...

What is the best way to stack divs within a parent div that has a height set to auto?

Looking to stack two divs on top of each other within a parent div to create a tab system for an applet. The challenge is that the parent div has auto height and the exact height of the child divs is unknown. Absolute positioning allows me to stack the div ...

Searching for HTML table cells with JUnit and Selenium

Currently, I am running tests on a website using Selenium IDE that features a table. My objective is to confirm text in a table cell (row, column) through a CSS selector. The design of the HTML table is quite straightforward. Below is an excerpt from the t ...

Could you provide some advice for creating a Single Page website?

I am working on a simple HTML setup with various elements such as a navbar and content blocks. <html> <head> <title>My HTML5 App</title> <meta charset="UTF-8"> <meta name="viewport" content="wid ...

jQuery ajax response html not being updated in div on webpage

I have been struggling with this issue for days and I can't seem to find a solution. My goal is to update a link on a web page with a new file. Even though the post request and new file are visible in the Google Chrome development network, the actual ...

How to Display Full Lightbox Image on both Tall and Short Height Browsers

Hey there! I'm showcasing my portfolio using lightbox, but I've run into a little issue. When the browser height is full, everything looks great - you can see the entire image, along with the paragraph and buttons. https://i.stack.imgur.com/UCJG ...

Tips for adjusting height responsively with Bootstrap 4

Utilizing bootstrap to create div elements. Check out the fiddle link below: fiddle On my page, I have 4 divs - two on top and two at the bottom. The height of all 4 card divs should remain the same and not change. In the last div, there could be on ...

Insert data into a drop-down menu and organize it

When I choose a value in one Select, the other options are removed and the previous value is added to them. How can I use JQuery to add the previous value to Select and then sort it alphabetically? ...

Is it possible to use JQuery ScrollTop while in the midst of an animation?

I am facing an issue with resizing containers and scrolling to elements. I have 2 containers that change width dynamically, each containing clickable elements. When a container is clicked, it animates the resize. However, when a clickable element is clicke ...

Could someone assist me in resolving the issue of receiving empty emails from my online form submission?

Every day, I receive 6 blank emails from my contact form at the same time. Even though the form on the website is filled out correctly and all the information goes through, I still get these mysterious blank emails. I have implemented validation checks in ...

Creating a unique identifier in JavaScript: the ultimate guide

Hi, I'm new to JavaScript and have been working hard to create a script that copies the text inside <p></p> elements. However, my current script requires different IDs for each <p></p>. Here is the code: function copyToClipboar ...

Troubleshooting: Page Unable to Import/Execute Linked JavaScript on WebStorm with Node.js Backend

I've been following W3School's jQuery tutorial, but I'm encountering some issues with importing scripts to an HTML document hosted on a Node server in WebStorm. I have properly installed and enabled the jQuery libraries under Preferences &g ...

Enhancing your website's design with dynamic CSS variables using JavaScript

Is there a way to update CSS variables specifically scoped under a certain CSS class (or even other complex CSS selectors) that are predefined in a stylesheet? This question extends beyond just CSS variables and includes other CSS properties as well, quest ...

What is the most effective way to extract data from a .dpbox table using selectorgadget in R (rvest)?

Recently, I've been experimenting with web scraping data from different websites using selectorgadget in R. One successful example was when I extracted information from . My usual approach involves utilizing the selectorgadget Chrome extension to choo ...

What is the best way to prevent a single cell within a table from hovering?

Within a table cell, I have another table. The outer table has a hover effect applied to it, but I want to prevent the inner table with the id "table_I_don_not_want_to_hover" from inheriting this hover behavior like the rest of the cells and rows. I need t ...

Viewing MSQL data using PHP. Interested in transferring specific rows to a different SQL table and removing them from the current table

I am currently in the process of creating a mobile helpdesk system for my department. The system involves an HTML form that sends data to a MySQL table, which is then displayed on a separate page with checkboxes next to each entry. At this stage, I have se ...

I possess three stylish CSS buttons, however, the other one is accompanied by <br />

I'm struggling to ensure that the three CSS buttons are the same size (using padding) and vertically aligned, as the second button includes a "<br />" which throws off the alignment. I was advised to use flexbox, but I haven't been able to ...

Tips on aligning two divs horizontally on the same line

doc.html .column { background-color: orange; width: 75%; vertical-align: top; display: inline-block; height: 200px; } .nav { vertical-align: top; display: inline-block; width: 25%; background-color: lightgreen; height: 200px; } * { ...

Trigger a function when clicking outside the element, similar to how a Bootstrap modal is closed

I have successfully created a popup box using angular in my project. It appears when I click on an icon and disappears when I click on the close button. However, I would like it to also close if someone clicks outside of the popup. Can anyone help me with ...