The header that sticks on scroll is annoyingly blocking the content

I managed to create a sticky on-scroll header/navigation bar successfully.

Here is how it looks now

However, I encountered an issue. When it reaches the top, it automatically 'covers' the top part of my content, which has text on it, and I don't want it that way.

This is how I want it to be. Please notice the space between the header and the photo (content).

I tried adding padding/margin to the content, but it didn't work. It still scrolls down right to the content, causing it to be covered.

Is there any way I could solve this so there is a noticeable 'space' between the header and the content?

Below is the snippet of my code:

var stickyOffset = $('.nav').offset().top;
    $(window).scroll(function(){
        var sticky = $('.nav'),
            scroll = $(window).scrollTop();
        if (scroll >= stickyOffset) sticky.addClass('nav-fixed');
        else sticky.removeClass('nav-fixed');
});
.top p, .bottom p {
    font-size: 18px;
}
.nav {
    box-shadow: 0 4px 8px 0 rgba(121, 121, 121, 0.1);
    z-index: 99;
    display: table;
    height: 100px;
    width: 100%;
    background-color: #fff672;
}
#home_btn_header {
    margin: 0;
    width: 90px; 
    height: 85px;
    background-image: url(drone.png);
    background-size: cover;
    background-color: transparent;
    border: transparent;
    cursor: pointer;
}
nav {
    display: table-cell;
    vertical-align: middle;
    padding-right: 8%;
}
/* CSS continues... */

It's important to note that the lorem ipsum text lines are being skipped due to the header covering them up.

Answer №1

To achieve the desired white space between the fixed header and content, adjust the styling of the .nav element by setting the background color to white and adding some padding-bottom. Additionally, apply a yellow background color to the nav element with the following CSS changes:

.nav {
  /* other rules... */
  background-color: #FFF;
  padding-bottom: 25px;
}

nav {
  /* other rules... */
  background-color: #fff672;
}

Here is a complete example including the necessary HTML and JavaScript:

var stickyOffset = $('.nav').offset().top;

$(window).scroll(function() {
  var sticky = $('.nav'),
    scroll = $(window).scrollTop();

  if (scroll >= stickyOffset) sticky.addClass('nav-fixed');
  else sticky.removeClass('nav-fixed');
});
.top p,
.bottom p {
  font-size: 18px;
}

.nav {
  box-shadow: 0 4px 8px 0 rgba(121, 121, 121, 0.1);
  z-index: 99;
  display: table;
  height: 100px;
  width: 100%;
  background-color: #FFF;
  padding-bottom: 25px;
}

#home_btn_header {
  margin: 0;
  width: 90px;
  height: 85px;
  background-image: url(drone.png);
  background-size: cover;
  background-color: transparent;
  border: transparent;
  cursor: pointer;
}

nav {
  display: table-cell;
  vertical-align: middle;
  padding-right: 8%;
  background-color: #fff672;
}

nav ul {
  float: right;
  list-style: none;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav ul::after {
  content: "";
  display: table;
  clear: both;
}

nav ul li {
  font-family: 'Sifonn Pro';
  background-color: transparent;
  border: transparent;
  cursor: pointer;
  text-align: center;
  display: inline;
}

.nav a {
  color: #061127;
  text-decoration: none;
  position: relative;
  font-size: 20px;
  margin: 0 6px;
  padding: 12px 12px;
  display: inline-block;
}

.nav-fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="top">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ...
</div>

It is not recommended to create this white gap as it may cause an awkward visual appearance when scrolling.

Answer №2

To ensure the navigation remains fixed, it is important to maintain the container within the content and only fix the nav position.

var stickyOffset = $('.nav').offset().top;

            $(window).scroll(function(){
            var sticky = $('.nav'),
                scroll = $(window).scrollTop();

            if (scroll >= stickyOffset) sticky.addClass('nav-fixed');
            else sticky.removeClass('nav-fixed');
            });
.top p, .bottom p {
font-size: 18px;
}


.nav {
                box-shadow: 0 4px 8px 0 rgba(121, 121, 121, 0.1);
                z-index: 99;
                height: 100px;
                width: 100%;
                background-color: #fff672;
            }
            #home_btn_header {
                margin: 0;
                width: 90px; height: 85px;
                background-image: url(drone.png);
                background-size: cover;
                background-color: transparent;
                border: transparent;
                cursor: pointer;
            }
            nav {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                padding-right: 8%;
            }
            nav ul {
                float: right;
                list-style: none;
                list-style-type: none;
                margin: 0;
                padding: 0;
                overflow: hidden;
            }
            nav ul::after {
                content: "";
                display: table;
                clear: both;
            }
            nav ul li {
                font-family: 'Sifonn Pro';
                background-color: transparent;
                border: transparent;
                cursor: pointer;
                text-align: center;
                display: inline;
            }
            .nav a {
                color: #061127;
                text-decoration: none;
                position: relative;
                font-size: 20px;
                margin: 0 6px;
                padding: 12px 12px;
                display: inline-block;
            }
            .nav-fixed {
                position: fixed;
                top: 0; left: 0;
                width: 100%;
            }
            .nav-container {
              height: 100px
            }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="top">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Lorem ipsum dolor...

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

Adjust the transparency level of the image's mapped area

I need help with changing the opacity of a specific area on an image map when clicked. The image has three areas, but I only want to target and change the opacity of the test2 area regardless of which area is clicked. Since my knowledge of jQuery syntax is ...

Resolve the issue with automatically generating SCSS type definitions (style.d.ts) for Preact within a TypeScript webpack setup

Utilizing webpack with Preact 10.x (nearly identical to React) and TypeScript in the VSCode environment. Following an update from Node version 12 to version 14, there seems to be a problem where *.scss files no longer automatically generate their correspo ...

Next.js Firebase Analytics Error: Undefined Window Object

I am currently working on implementing and exporting the Firebase analytics module in Next.js using Firebase v9. When attempting to run the code snippet below, I encountered an error message stating "ReferenceError: window is not defined". Howev ...

Ext.ux.TDGi.iconMgr is a cutting-edge plugin designed specifically for the latest version of

Has anyone successfully migrated the Ext.ux.TDGi.iconMgr plugin to ExtJS 4? (http://tdg-i.com/44/extuxtdgiiconmgr...-icons-and-css) ...

Guide on enabling automatic rotation using Javascript

Recently, I came across a slider online that unfortunately did not have an autorotate feature. Despite my attempts to add an autorotate function using setTimeout and click functions, I was unsuccessful. This particular slider contains 4 buttons for each s ...

What is the best way to access and utilize the data stored in my database using mySQL, PHP, and JavaScript?

My Objective : When a user selects a location from the dropdown list, I want the function "newMark()" to place a marker on Google Maps; Current Achievements : The map is displayed along with the select field containing all locations saved in my database. ...

Choose from a range of knockout fetching options for the select feature and choose one

Upon loading my page, there is a <select> element positioned. I am utilizing knockout data-bind to retrieve the values like this: <select data-bind="attr: { id: 'input-' + id(), required: !allowBlank }, option ...

What is the best method to extract jQuery JSON request attributes in a servlet?

I am currently working with the following JavaScript code: var data = {message:"Hi"} var sendJson = function (){ alert(data); $.ajax({ url:"./jsonTest", data: data, contentType:"application/json", type:"post", ...

The creation of the Angular project was unsuccessful due to the outdated circular-json dependency

After attempting to create a new Angular project with the command: ng new hello-world I encountered an error message: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d6dcc7d6c0d9d4c798dfc6dadbf5859b809b8 ...

Renaming errors within a project with a complex nested structure using npm

I am encountering an issue in my NodeJS project which consists of nested subprojects with their own package.json files. Whenever I make changes to dependencies in the subprojects, I encounter errors similar to the one below: npm ERR! code ENOENT npm ERR! ...

Element with a fixed position within an iScroll container

Has anyone encountered an issue with fixing an element with position: fixed inside a big Iscroll page? It seems like the element scrolls along with the content due to the transition imposed by Iscroll. I'm trying to fix the "FIXTHIS" element, but it ...

How to Use Jquery to Position an Image in the Center of a Slider

I am trying to center the image within a div. Here is the code I have: <html> <head> <title>test</title> <style type="text/css"> .container { background:#EFEFEF; margin: auto; ...

What is the best approach in JavaScript (jQuery) to loop through every 'a' element on a webpage, sequentially applying a unique style to each one?

Imagine this scenario: On a webpage, there are multiple 'a' tags with different alt tag values: <a href="img1.jpg" class="myClass" alt="0,0,600,200"></a> <a href="img2.jpg" class="myClass" alt="200,0,600,75"></a> <a hr ...

Tips for adjusting the size of a dropdown arrow icon

Is there a way to adjust the size of the drop-down icon using CSS? If not, I am looking to replace it with an image button. Thank you! <select> <option value="age">18</option> <option value="age"& ...

Articles related to Express.js - encountering issues when attempting to read properties from a null object

I'm trying to display related articles based on categories using Mongoose for querying data from MongoDB. However, when I attempt the code below, I encounter an error message stating "TypeError: Cannot read properties of null (reading 'category& ...

Navigate one level up or down from the current tag that contains a specified value by utilizing Scrapy

To extract the price text from within the custom-control / label / font style, I must use the data-number attribute data-number="025.00286R". This unique identifier differentiates between control section divs based on the letter at the end. <d ...

Having trouble with utilizing pako.js in your JavaScript code? The error "Pako is not defined

I'm relatively new to the world of JavaScript. Currently, I'm tackling an algorithm that involves deflating in Java and inflating in JavaScript. While researching solutions, I came across pako.js as a recommended tool for decompression. However, ...

Attempting to retrieve JSON data using jQuery

Currently, I have a PHP file that outputs JSON data like the following: {"news":[{"title":"title news example1","content":"blabla bla 1","img":"url"}, {"title":"title news example2","content":"blabla bla 2","img":"url2"}, {"title":"title news example3","c ...

File or directory does not exist: ENOENT error occurred while trying to scan 'pages'

Having trouble adding a sitemap to my nextjs app - when I go to http://localhost:3000/sitemap.xml, I get this error: Error: ENOENT: no such file or directory, scandir 'pages' https://i.sstatic.net/cxuvB.png The code in pages/sitemap.xml.js is a ...

Ways to transfer large amounts of data to Amazon Web Services

After reading the node documentation on sqs.sendMessage, I noticed that it mentions the ability to send messages up to 256KB in size. However, my messages tend to exceed this limit. What is the recommended approach for handling large payloads in this scena ...