Looking to troubleshoot a white background issue while attempting to create a transparent navbar

I am trying to create a full-page landing with a transparent navbar using bootstrap4. My goal is to have a sticky navbar that remains at the top as the user scrolls down the page.

When I apply the class 'fixed-top', the navbar stays at the top but doesn't stick as I scroll. However, when I use 'sticky-top', the background of the navbar turns white.

<nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
    <div class="container-fluid">
         <a href="#" class="navbar-brand">Brand</a>
         <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggler-1" aria-controls="navbarToggler-1" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
         </button>

.bg-dark{
    background-color: transparent !important;
}

Answer №1

I created it in this way

html

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="#">Page 1</a></li>
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>
  </div>
</nav>

css

.navbar-fixed {
  top: 0;
  z-index: 100;
  position: fixed;
  width: 100%;
}
.navbar{
  background-color: transparent !important;
}

js

$(window).scroll(function () {
  console.log($(window).scrollTop())
  if ($(window).scrollTop() > 63) {
    $('.navbar').addClass('navbar-fixed');
  }
  if ($(window).scrollTop() < 64) {
    $('.navbar').removeClass('navbar-fixed');
  }
});

Hopefully, this solution is useful to you

Answer №2

To remove the class "bg-dark" when it becomes sticky, you can achieve this through JavaScript or by using a combination of CSS and body classes. For CSS-only solution, you can add the following code: ( body .bg-dark{background:none;} header .bg-dark{background:none;} ) You can also use the !important keyword if needed.

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

    if (scroll >= 300) {
        $(".navbar").addClass("sticky_nav");
        $(".navbar").removeClass("bg-dark");
    } else {
        $(".navbar").removeClass("sticky_nav");
        $(".navbar").addClass("bg-dark");
    }
});
.navbar.sticky_nav{
    position: fixed;
    top: 0;
    width: 100%;
    left: 0;
    z-index: 99;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
    <div class="container-fluid">
         <a href="#" class="navbar-brand">Brand</a>
         <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggler-1" aria-controls="navbarToggler-1" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
         </button>
         </div>
  </nav>
  <div style="height:200px; width:100%; background:#f00; display:block;"></div>
   <div style="height:300px; width:100%; background:#000; display:block;"></div>

Answer №3

.navbar.bg-dark{
    background-color: transparent !important;
}

.navbar.navbar-dark .navbar-brand{
color: #000}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
    <div class="container-fluid">
         <a href="#" class="navbar-brand">Brand</a>
         <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggler-1" aria-controls="navbarToggler-1" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
         </button>

Answer №4

Your navigation bar receives its background color from the class bg-dark

To remove the background color, simply delete the bg-dark class from the <nav class="">

<nav class="navbar navbar-expand-md navbar-dark sticky-top">
    <div class="container-fluid">
         <a href="#" class="navbar-brand">Brand</a>
         <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggler-1" aria-controls="navbarToggler-1" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
         </button>
    </div>
</nav>

Reason to Remove bg-dark

The bg-dark class has the following attribute:

.bg-dark {
   background-color: #343a40 !important;
}

Let's see a DEMO here!

<body style="background-color: gray;">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

  <nav class="navbar navbar-expand-md navbar-dark sticky-top">
    <div class="container-fluid">
      <a href="#" class="navbar-brand">Brand</a>
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggler-1" aria-controls="navbarToggler-1" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
         </button>
    </div>
    <div class="collapse navbar-collapse" id="navbarToggler-1">
      xxx
    </div>
  </nav>

  <img src="https://cdn.pixabay.com/photo/2016/02/19/11/19/computer-1209641_960_720.jpg">
</body>

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 could be causing my AngularJS to malfunction on this particular website?

I am currently learning AngularJs and practicing some coding. However, I am facing an issue where the javascript code does not work when I run it on my browser (Chrome/IE). The "{{product.like}}" code is not functioning as expected. Is there any specific d ...

Is there a way to eliminate the 'All Files' option from an HTML file input field?

I have implemented a feature to allow users to upload custom files using . Currently, I am restricting the allowed file types to only ".Txt, .SVG, .BMP, .JPEG" by using accept=".Txt,.SVG,.BMP,.JPEG". This setting causes the browser's file-select dial ...

Tailwind CSS - when it comes to styling, larger screen media queries are taking precedence over smaller media queries

Currently tackling screen responsiveness issues in a Nextjs + Tailwind CSS project. All Tailwind breakpoints are functioning correctly except for "sm" and below. Snippet of the problematic code: <div className="bg-red-200 sm:bg-white md:bg-gray-70 ...

Start a new typescript project from scratch

Seeking assistance in setting up a blank TypeScript project with a package.json, TypeScript and HTML file. I am looking for something akin to the Stackblitz blank-typescript project. If anyone could provide me with a step-by-step guide on how to create su ...

Designing a layout with one box on top and two beneath it, covering the entire page, can be achieved by following these steps

https://i.sstatic.net/A1eUh.png I am currently working on a project where I want to divide the screen into three sections. One section will cover half of the screen to display an image slider, and the remaining half will have two sections which is already ...

Struggling to place buttons below each row in a Bootstrap DataTable, unfortunately, they are only appearing under a specific column rather than the entire row

I have developed a system for managing event expenses that includes an option for multiple installments of a single payment for each event. My goal is to display these installments when a user hovers over an event in the table, with each row representing a ...

CSS rotation causing issues with absolute positioning

In the example below, I have demonstrated the current behavior I am experiencing and the desired outcome. // Rotated div rotated.style.left = "50px"; rotated.style.top = "100px"; // Original "untouched" div original.style.left = "50px"; original.style.t ...

Embed a javascript tag to print a PDF document

I have been struggling with printing a PDF file using JavaScript. I attempted to use the embed trick suggested in this Silent print a embedded PDF but unfortunately, the print function remained undefined. Then, I tried another approach using an Iframe and ...

Utilizing Bootstrap 4 for a responsive layout: positioning a <div> element adjacent to a centered image

I am currently working on a project involving an image gallery. My goal is to showcase the images in a specific layout: https://i.sstatic.net/cXcql.png Essentially, the image should always be centered. If there is sufficient space to the right, I want to ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...

PHP version 7.2.1 is throwing an error indicating an undefined index for the file_upload

Encountering an error message: Notice: Undefined index: file_upload in C:\MAMP\htdocs\basic_files\upload.php on line 3 Each time I attempt to upload a file using the form. While many attribute this issue to problems with enctype or ...

Retrieve the ID of the image element using Jquery from a collection of images within a div container

I'm encountering a simple issue that I can't seem to solve. I am working on a basic slider/gallery with the following functionalities: 1) "If button 1 is clicked, image one will appear." 2) "Clicking on button 2 will make IMAGE 1 slide left and I ...

Create a sleek and functional navigation bar for your Angular 4 project by incorporating the latest features

After reviewing the ng-bootstrap documentation, I came across a crucial point: Should I add bootstrap.js or bootstrap.min.js to my project? According to ng-bootstrap, it is recommended not to include any JavaScript files such as bootstrap.js or bootstrap. ...

When floating a heading 4 (h4) and two divs to the left, and a single div to the right,

When floating a h4 and 2 divs to the left, and another div to the right, how can I place a block inside the remaining space that perfectly fills it? The background color of this space should not spread outside. [h4][div][div][remaining space][div] Thank ...

Is there a speed advantage in Angular 2+ when using [disabled] instead of "disabled" in a template?

Let's analyze the differences between these two HTML snippets: <input formControlName="address" [disabled]=isDisabled/> <input formControlName="address" disabled={{isDisabled}}/> While it seems that the first option is more readable, I ...

Having trouble with jQuery's recursive animation functionality

I have developed a function to scroll multiple images horizontally in the header of my website. Inside my header, I have implemented code that looks like this: <div class='images'> <!-- this div is 150% wide with overflow hidden --> ...

Creating an undo feature for a dynamically generated checklist of checkboxes

I am using a combination of javascript/jquery and html to dynamically populate the page with checkboxes. When you click "add", a new checkbox is created. I am now looking for a way to add an undo button that would delete the last checkbox created. Here is ...

Transfer a PHP variable between pages to retrieve the appropriate item from a button

In my project, I have two pages named content.php and pagecontent.php. In content.php, I am fetching product information from a database and displaying it in table format. The table has a grid layout with 2 rows and 3 columns. Each cell contains the descri ...

What steps can I take to resolve the CSP errors I am experiencing?

I am currently working with NextJs@12 and I am attempting to set up CSP for my application. Unfortunately, I keep encountering errors in my console and I cannot figure out where I am going wrong. Below is the current policy that I have in my next.config fi ...

Can you explain the function of .modal('dispose') in Bootstrap 4?

As stated in the documentation, the .modal('dispose') method is used to destroy a modal. .modal('dispose') This method destroys the specified modal element. However, after creating an eventListener like this: $('#myModal') ...