The fixed navigation bar is causing the content to shift downward

I am currently working on designing a two-layered navbar structure. The top layer contains essential company information such as phone number, email address, social media links, and more.

The second layer serves as the main navbar, designed to be sticky and transparent.

However, I am facing an issue where the second layer is not overlaying on top of the following div but is instead pushing it down, creating a blank space. Despite using Bootstrap 4, I am unable to identify the root cause of this problem.

#top-nav{
  position: relative;
  z-index: 1000;
}

.top-nav{
  background-color: #FFF;
  height: 40px;
}

.top-nav a{
  color:#696969;
}

.top-nav a:hover{
  color:#FF6700;
}

.top-nav p{
  margin-left: 50px;
  position: relative;
  top:5px;
  color: #696969;
}
.top-nav .social-links a{
  padding-right: 20px !important;
  position: relative;
  top: 5px;
  color: #696969;
}

.top-nav .social-links a:hover{
  color: #ff6700;
}

#content{
  height:100%;
  width:100%;
}
.navbar-brand img{
  width: 300px ;
  height: auto ;
}

@media screen and (max-width:500px){
  .navbar-brand img{
    width:250px;
  }
  #content{
    /* top: - */
  }
}

@media screen and (min-width: 500px){
  .navbar-brand img{
    margin-left: 20px;
  }
}

.first{
  height: 100vh;
  background-color: #000;
  position: relative;
  z-index:0;
  /* position:relative;
  top:-115px; */
}
.second{
  min-height: 100vh;
  background-color: #676767;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="top-nav" class="container-fluid text-center">
      <div class="container row top-nav justify-content-between ml-auto mr-auto">
        <div class="call .col-auto mr-auto">
          <p><b>Contact: </b><a href="tel:+11111111">+111111111</a></p>
        </div>
        <div class="call .col-auto mr-auto">
          <p><b>Email: </b><a href="emailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3e3233293c3e291d252427733e3230">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284b47465c494b5c6849505152064b4745">[email protected]</a></a></p>
        </div>
        <div class="col-auto social-links">
          <a href="#"><i class = "fab fa-facebook-f"></i></a>
          <a href="#"><i class = "fab fa-skype"></i></a>
          <a href="#"><i class = "fab fa-linkedin-in"></i></a>
        </div>
      </div>
    </div>
    <nav class="navbar navbar-expand-md navbar-dark bg-transparent sticky-top">
      <a class="navbar-brand" href="index.html">Logo</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Services</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Career</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </div>
    </nav>

    <div class="first"></div>

    <div class="second"></div>

Answer №1

When an element is sticky positioned, it initially occupies space like a relatively positioned element until you scroll past it and it becomes sticky (but what happens to the space isn't specified).

One way to address this issue is by moving the sticky class to a separate container div with a height of 0 and overflow visible, ensuring that it doesn't take up any extra space.

#top-nav{
  position: relative;
  z-index: 1000;
}

.top-nav{
  background-color: #FFF;
  height: 40px;
}

.top-nav a{
  color:#696969;
}

... (more CSS styles here) ...

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="top-nav" class="container-fluid text-center">
      <div class="container row top-nav justify-content-between ml-auto mr-auto">
        ... (more HTML markup here) ...
    </div>

    <div class="sticky-top">
        ... (more HTML markup here) ...
    </div>
    <div class="first"></div>
    <div class="second"></div>

https://developer.mozilla.org/en-US/docs/Web/CSS/position

Answer №2

Try using fixed-top instead of sticky-top to avoid maintaining space in the normal flow of the element.

#top-nav{
  position: relative;
  z-index: 1000;
}

.top-nav{
  background-color: #FFF;
  height: 40px;
}

.top-nav a{
  color:#696969;
}

.top-nav a:hover{
  color:#FF6700;
}

.top-nav p{
  margin-left: 50px;
  position: relative;
  top:5px;
  color: #696969;
}
.top-nav .social-links a{
  padding-right: 20px !important;
  position: relative;
  top: 5px;
  color: #696969;
}

.top-nav .social-links a:hover{
  color: #ff6700;
}

#content{
  height:100%;
  width:100%;
}
.navbar-brand img{
  width: 300px ;
  height: auto ;
}

@media screen and (max-width:500px){
  .navbar-brand img{
    width:250px;
  }
  #content{
    /* top: - */
  }
}

@media screen and (min-width: 500px){
  .navbar-brand img{
    margin-left: 20px;
  }
}

.first{
  height: 100vh;
  background-color: #000;
  position: relative;
  z-index:0;
  /* position:relative;
  top:-115px; */
}
.second{
  min-height: 100vh;
  background-color: #676767;
}
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700%7CRoboto%7CJosefin+Sans:100,300,400,500" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="css/fa-svg-with-js.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="css/normalise.css">
    <link rel="stylesheet" href="css/style.css">

    <div id="top-nav" class="container-fluid text-center">
      <div class="container row top-nav justify-content-between ml-auto mr-auto">
        <div class="call .col-auto mr-auto">
          <p><b>Contact: </b><a href="tel:+11111111">+111111111</a></p>
        </div>
        <div class="call .col-auto mr-auto">
          <p><b>Email: </b><a href="emailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c7cbcad0c5c7d0e4dcddde8ac7cbc9">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2919d9c86939186b2938a8b88dc919d9f">[email protected]</a></a></p>
        </div>
        <div class="col-auto social-links">
          <a href="#"><i class = "fab fa-facebook-f"></i></a>
          <a href="#"><i class = "fab fa-skype"></i></a>
          <a href="#"><i class = "fab fa-linkedin-in"></i></a>
        </div>
      </div>
    </div>
    <nav class="navbar navbar-expand-md navbar-dark bg-transparent fixed-top">
      <a class="navbar-brand" href="index.html">Logo</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarCollapse">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Services</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Career</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Contact</a>
          </li>
        </ul>
      </div>
    </nav>

    <div class="first"></div>

    <div class="second"></div>
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  <script src="js/fontawesome-all.min.js"></script>

Answer №3

To achieve a transparent background for the 2nd navbar, ensure that the content color contrasts well against various background shades...

.sticky-top .navbar-nav .nav-link,
.sticky-top .navbar-nav .active .nav-link {
    color: #00ccff;
}

https://www.example.com/code-sample

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 best way to ensure that a component remains the highest layer on a react-leaflet map?

I am encountering an issue where the table on my page only shows for a brief second when I refresh the page, and then it disappears. The Map Component being used is a react-leaflet map. I would like to have a component always displayed on top of the map wi ...

Understanding the Intrinsic Dimensions of Replaced Elements in CSS

I'm currently facing some challenges with the CSS Intrinsic & Extrinsic Sizing Module Level 3, specifically chapter 4.1 on Intrinsic Sizes. This section is proving to be quite difficult for me: When a block-level or inline-level replaced element h ...

"Having trouble getting `justify-between` to work in a tailwind CSS on an absolute

I'm currently using Tailwind CSS and I'm trying to position my navbar on top of an image. The image is set in a relative position and the navbar is set in an absolute position. Surprisingly, when the navbar is not set in the absolute position, I ...

The div is inexplicably shifting downward once the first three rows are shown correctly

After displaying the first 3 rows properly, the div mysteriously moves down. Could I be making a silly mistake? All images have the same width and height. Please see the attached image for reference. <?php // start first row echo "<hr class='m ...

Creating nested tabs with Bootstrap: A step-by-step guide on adding tabs within tabs

I am encountering a challenge with nested tabs. I am utilizing Bootstrap and not using custom CSS. Any assistance would be greatly appreciated. <div class="container-fluid"> <!-- Tabs --> <ul class="nav nav-t ...

Creating a Parent Container to Maintain the Height of the Tallest Offspring

I've been searching online for solutions, but so far I haven't found one that meets all the requirements. <div class="parent"> <div class="child1">I am 60px tall and always visible; my parent is 100px tall</div> <div ...

Drag and Drop: Unable to Return Item to Its Original Position

I'm facing an issue with an HTML code that Drag Drop feature, everything works fine except for dragging it back to its original position. Below is the snippet of the HTML code: function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ...

The event Onchange of my dropdownlist is not functioning properly in aspx

Hello, I am facing an issue with a page that I am trying to reset. The page is quite old and it is built using vb.net. The problem I'm encountering is that when I change the item in a dropdownlist, the change is not reflected on the page. Aspx code ...

Tips for increasing the height of a div as rows are dynamically added to a table

I am facing an issue with a div main containing a table grid. Rows are dynamically added to the table, causing it to increase in height. However, the height of the div main remains unchanged, resulting in the table overlapping the footer content. What chan ...

Translate CSS into JavaScript while maintaining selector understanding

I am interested in developing a tool that can read a .css file and generate a function that will accept an array of class names as input and output the corresponding styles for an element with those classes, represented as a JavaScript object. This tool wo ...

The WebForms feature is failing to include the email input type in the form after autopostback

On my page, I have an input field using asp:TextBox: <asp:TextBox ID="edEmail" runat="server" /> The form submission is done using a standard <asp:Button>: <asp:TextBox ID="edEmail" runat="server" /> <asp:Button ID="bbOK" Text="Save ...

Issue: Inadequate Scope Permissions

Currently, I am working on a web application designed to post on the stocktwits wall. However, I have encountered an issue stating "Insufficient Scope Permissions." Does anyone have insight into where this error may be coming from? Here is a snippet of my ...

Is it possible to access a file on the client's PC without transferring the file to the server?

Is there a way to read an excel file directly from a user's PC in a web app and insert the cell values into a MySQL database cell by cell? Or should the file be uploaded to the server first before being read? (The web app is built using JSP) ...

Incorporate PHP functionality into an HTML document

I'm looking to incorporate a PHP code into my phoneGap application (HTML). I've attempted the following code, but unfortunately it's not functioning as expected. <html> <head> <title>PHP Integrated with HTML&l ...

JavaScript's local storage feature seems to be failing in both saving and retrieving data

I'm working on a fun math game where two random numbers and a sign (+ or -) are generated using Math.random(). The user needs to input their answer in the provided input box. However, I am facing an issue with saving and retrieving the score and high ...

Dialog overflowing due to Material-UI Dropdown

Is there a way to make the dropdown select options in a Dialog (modal) extend past the bottom border instead of getting cut off? I'm currently using MUI v5. <Dialog open={open}> <DialogContent> <Autocomplete disablePortal ...

Sending information to a dialog box

I'm facing a challenge with my button that triggers a modal. Although the button works fine, I now need to figure out how to pass data from the button to a specific field within the modal. Here is the HTML code: <input type=button value='Add ...

Nav bar breaching the Bootstrap threshold

I am new to using bootstrap and currently in the learning process. I am facing an issue with the navigation bar I created. All the contents are aligning to the right, but the search bar and tabs are breaking onto a new line. I have attempted various soluti ...

What could be causing my CSS to go unnoticed in my R Shiny application?

Explore the tooltip configuration I've implemented for the info icon in the code snippet below: library(shiny) ui <- fluidPage( span( `data-toggle` = "tooltip", `data-placement` = "auto", `data-trigger` = "cli ...

Take out a text field from a cell within a table

I am looking for a way to dynamically remove a textarea when the 'x' button next to it is clicked. Each 'x' button has an id that corresponds to the id of the associated textarea. For example, if I click the 'x' button under t ...