header that sticks with irregular motion

Currently in the process of designing a website, I've run into an issue where whenever I scroll with my sticky header, the page automatically jumps to the bottom of the next element. Does anyone have any insights on what might be causing this odd behavior?

I've attempted tweaking the padding settings for various elements on the page such as the banner, heading, text content, and even anchor elements, but unfortunately nothing has resolved the problem.

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

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
  if (window.pageYOffset > sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
@import url(https://fonts.googleapis.com/css?family=Roboto);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Condensed);
body {
  margin: 0px;
  font-family: 'Roboto';
  background-color: white;
}

.header {
  padding: 10px 16px;
  background: white;
  color: black;
  width: 100%;
  border-bottom: 1px solid grey;
}

.content {
  padding-top: : 16px;
  padding-bottom: 16px;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 102px;
}

nav>div>a {
  padding: 6px 20px;
}

h3:hover {
  color: rgb(128, 128, 128)
}

h3 {
  display: inline-block;
}

.banner {
  width: 100%;
  padding-top: 0px;
  padding-bottom: 10px;
}

.image-cropper {
  width: 100px;
  height: 100px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}

.header-text {
  width: 100%;
  text-align: right;
  margin-right: 25px;
}

.logo {
  margin-bottom: -80px;
}
<!DOCTYPE html>
<html>

<head>
  <title> Nick Pemberton - About </title>
  <link rel="preload" href="../js/java.js" as="script">
  <link rel="preload" href="../css/main.css" as="style">
  <link rel="prerender" href="Gallery.html">
  <link rel="prerender" href="about.html">
  <link rel="prerender" href="HomePage.html">
  <link rel="preload" href="../photos/kilc/banner.jpg" as="image">
  <link rel="icon" href="favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="author" content="Nick Pemberton">
  <link rel="stylesheet" href="../css/main.css">
  <!-- Link to main.css -->
</head>
<!-- /head -->

<body>
  <nav class="header" id="myHeader"><img src="../photos/test.png" class="logo">
    <div class="header-text">
      <a href="HomePage.html">
        <h3>Home
          <!-- Home -->
        </h3>
      </a>
      <a href="Gallery.html">
        <h3>Gallery
          <!-- Gallery -->
        </h3>
      </a>
      <a href="about.html">
        <h3>About</h3>
      </a>
    </div>
  </nav>
  <!-- Nav Bar -->
  <content class="content">
    <img class="banner" src="../photos/kilc/banner.jpg"> A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is
    not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually
    here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant.
    A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed
    it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch of text is usually here, removed it since it is not relevant. A bunch
    of text is usually here, removed it since it is not relevant.
  </content>
  <!-- Content -->

  <script src="../js/java.js">
  </script>

</body>
<!-- body -->

</html>
<!-- html -->

Answer №1

It appears that the custom tag content is causing an issue as it defaults to being an inline element, resulting in padding-top not functioning as expected. To resolve this, consider using a simple div or adding display:block to the tag:

var header = document.getElementById("myHeader");
var sticky = header.offsetTop;

function myFunction() {
  if (window.pageYOffset > sticky) {
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}
window.onscroll=myFunction;
@import url(https://fonts.googleapis.com/css?family=Roboto);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Condensed);
body {
  margin: 0px;
  font-family: 'Roboto';
  background-color: white;
}

.header {
  padding: 10px 16px;
  background: white;
  color: black;
  border-bottom: 1px solid grey;
}

.content {
  padding-top: 16px;
  padding-bottom: 16px;
  font-size:32px;
}

.sticky {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

.sticky+.content {
  padding-top: 102px;
}

nav>div>a {
  padding: 6px 20px;
}

h3:hover {
  color: rgb(128, 128, 128)
}

h3 {
  display: inline-block;
}

.banner {
  width: 100%;
  padding-top: 0px;
  padding-bottom: 10px;
}

.image-cropper {
  width: 100px;
  height: 100px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}

.header-text {
  width: 100%;
  text-align: right;
  margin-right: 25px;
}

.logo {
  margin-bottom: -80px;
}
<nav class="header" id="myHeader">
  <div class="header-text">
    <a href="HomePage.html">
      <h3>Home
        <!-- Home -->
      </h3>
    </a>
    <a href="Gallery.html">
      <h3>Gallery
        <!-- Gallery -->
      </h3>
    </a>
    <a href="about.html">
      <h3>About</h3>
    </a>
  </div>
</nav>
<!-- Nav Bar -->
<div class="content">
   Content could be inserted here but has been removed for brevity.

</div>
<!-- Content -->

You may also simplify your code by considering utilizing the sticky position instead of fixed positioning:

@import url(https://fonts.googleapis.com/css?family=Roboto);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Condensed);
body {
  margin: 0px;
  font-family: 'Roboto';
  background-color: white;
}

.header {
  padding: 10px 16px;
  background: white;
  color: black;
  border-bottom: 1px solid grey;
  position: sticky;
  top: 0;
}

.content {
  padding-top: 16px;
  padding-bottom: 16px;
  font-size: 32px;
}


.sticky+.content {
  padding-top: 102px;
}

nav>div>a {
  padding: 6px 20px;
}

h3:hover {
  color: rgb(128, 128, 128);
}

h3 {
  display: inline-block;
}

.banner {
  width: 100%;
  padding-top: 0px;
  padding-bottom: 10px;
}

.image-cropper {
  width: 100px;
  height: 100px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}

.header-text {
  width: 100%;
  text-align: right;
  margin-right: 25px;
}

.logo {
  margin-bottom: -80px;
}
<nav class="header" id="myHeader">
  <div class="header-text">
    <a href="HomePage.html">
      <h3>Home
        <!-- Home -->
      </h3>
    </a>
    <a href="Gallery.html">
      <h3>Gallery
        <!-- Gallery -->
      </h3>
    </a>
    <a href="about.html">
      <h3>About</h3>
    </a>
  </div>
</nav>
<!-- Nav Bar -->
<div class="content">
   Content could be inserted here but has been removed for brevity.

</div>
<!-- Content -->

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

Using nodeMCU along with ajax means that instead of refreshing just a single element, the entire web page

I'm in need of assistance with reloading two elements on my webpage. I am working with small electronics and the NodeMCU as its brain. I require two outputs (connected to relays) and two inputs. For the inputs, I would like them to update periodicall ...

What are the best practices for updating models using Bookshelf.js?

I'm struggling to make sense of the Bookshelf API, particularly when it comes to performing upsert operations. Let me outline my specific scenario: My model is named Radio, with a custom primary key called serial. For this example, let's assume ...

What causes the return of undefined when accessing an item from an object?

In order to extract the item "Errors" from the data object provided below: {Id: 15, Date: "22-02-2019", Time: "22:45", Sport: "Football", Country: "United Kingdom", …} Bet: "Win" Bookie: "Bet365" Competition: "Premier League" Country: "U ...

Adjusting Renderer Size in ThreeJS for Fullscreen Display

After creating a ThreeJS application designed for a 4:3 layout with multiple buttons and features, I decided to enable Fullscreen mode using THREEx.Fullscreen.js. Although the Fullscreen mode is functioning properly, a new issue arose - the renderer size(x ...

Filtering data in an antd table by searching

Just starting out with React hooks, specifically using TypeScript, and I'm struggling to implement a search filter with two parameters. Currently, the search filter is only working with one parameter which is 'receiver?.name?'. However, I wo ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Struggling to implement JQuery code in a Next.js application

I'm having trouble getting my tracking code to work in Next.js. <Script> window.dataLayer = window.dataLayer || []; function gtag(){ dataLayer.push(arguments) } gtag('js', new Date()) ...

Is there a way to modify the standard width of semantic-ui sidebars?

I'm trying to adjust the width of semantic-ui sidebars, which are originally 275px wide. Where should I include the css/js code to make them wider or narrower? Here is their default code: .ui.sidebar { width: 275px!important; margin-left: -275 ...

Display/Conceal WP Submenu

Struggling to switch the behavior of my Wordpress menu. Want it to display when clicked, not when hovered: <nav> <ul> <li> <?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ...

What is the best way to fill a sub-document following a $geoNear query?

I'm currently working with NodeJs and Mongoose to create a feature that lists nearby deals. Deal.db.db.command({ "geoNear": Deal.collection.name, "near": [23,67], "spherical": true, "distanceField": "dis" }, function (err, docum ...

Creating Low-Poly Terrain with a Touch of Serendipity

Looking to create terrain in a low-poly style inspired by the landscapes in Cube Slam and this video. I experimented with the webgl_geometry_terrain.html example, but it's still generating smooth terrain instead of the flat polygons I'm aiming fo ...

MongooseServerSelectionError: Connection timed out

Recently, I encountered an issue while attempting to run my Node.js application. An error message popped up stating MongooseServerSelectionError: connect ETIMEDOUT, along with type: 'ReplicaSetNoPrimary'. Surprisingly, everything had been functio ...

"Utilizing CSS to ensure a div adheres to a specific class

I'm trying to change the floating position of a div by using a CSS class, instead of its predefined properties. Currently, the div is styled as follows: #div { height: 30px; width: 30px; float:left;} To make it float to the right, I created a class ...

Discover Vuejs: Display Less, Reveal More

In order to achieve a specific task, I need to display 12 items in 4 columns with 4 items each. Initially, only the first four items are visible and the rest are hidden until the user clicks the "Show More" button. Upon clicking the button, the next row ...

Perform a function dynamically once a specific textfield is filled and continuously while filling another textfield

Imagine we have two text fields - one for a first name and one for a surname. As I type in the surname field after already filling out the first name, a function called sug() should provide suggestions or perform some action each time I add another letter ...

External JavaScript functions remain hidden from the HTML page

Having issues with JavaScript functions. I created functions in a separate file named index.js, but when I use them in index.html like "onclick = function()", the file doesn't recognize the function. <!doctype html> <html lang="{{ app()-> ...

Iterate through the form fields and save the information into an object

I am attempting to create a JavaScript object by looping through form inputs. const data = {}; // loop through each input found in form $('#form_name').filter(':input').each(function() { const $id = $(this).attr('id'); c ...

Unable to generate a menu with bootstrap

Hey there, I've been experimenting with creating a responsive menu using Bootstrap, but I'm struggling with the implementation. My vision is to have a logo on the left side and a menu on the right side. Below is the concept for my menu design. ...

What is the best way to convert Decimal numbers (including decimal points) into Hexadecimal Strings using javascript?

I'm attempting to change a decimal value with a decimal point into a hexadecimal string: Decimal: 0.01 Hexadecimal: 3C23D70A I am struggling to convert 0.01 to 3C23D70A in JavaScript, as using .toString(16) only gives me back 0. Does anyone have a s ...

Is separating Jssor Slider CSS effective?

Having a bit of trouble with the Jssor slider here. I'm trying to separate the slider styles into an external CSS document and running into issues. For instance: <div id="slider1_container" style="position: relative; top: 0px; left: 0px; width: 11 ...