Excessive margin is preventing the navigation from being properly displayed

I am experiencing an issue with my sticky navigation bar that is enclosed in a div with 40px of margin on each side. The fixed navbar has a width of 100%, and while it correctly considers the parent's margin on the left side, it overflows on the right side. I have attempted to use overflow hidden on the parent element, but it does not seem to solve the problem. There must be a simple solution that I am missing, but I can't seem to identify it.

$(window).scroll(function() {
  if ($(window).width() > 768) {
    if ($(document).scrollTop() > 100) {
      $('header').addClass('nav-border');
    } else {
      $('header').removeClass('nav-border');
    }
  }
});

$('.burger-icon').click(function() {
  $(this).toggleClass('burger-active');
  $('.main-nav').fadeToggle();
  $('body').toggleClass('nav-scroll-lock');
});
.test-div {
  height: 1800px;
  width: 100%;
  background: black;
}

.main-wrapper {
  margin: 0 40px;
}

.container {
  max-width: 800px;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto
}

.row {
  margin-left: -15px;
  margin-right: -15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.col {
  -webkit-box-flex: 1;
  -ms-flex: 1 1 0px;
  flex: 1 1 0;
}

header {
  background-color: #fff;
  position: fixed;
  width: 100%;
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;
}

.navbar {
  height: 60px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.logo {
  position: relative;
  z-index: 1000;
  background: #151515;
  padding: 8px;
}

.logo a {
  font-weight: 700;
  color: #fff;
  font-size: 18px;
  letter-spacing: 1px;
}

.burger-icon {
  position: relative;
  z-index: 1000 !important;
  -ms-flex-item-align: end;
}

.burger-icon .burger-line {
  width: 40px;
  height: 3px;
  background-color: #282828;
  display: block;
  margin: 5px auto;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.burger-icon.burger-active .burger-line:nth-child(2) {
  opacity: 0;
}

.burger-icon.burger-active .burger-line:nth-child(1) {
  -webkit-transform: translateY(8px) rotate(45deg);
  transform: translateY(8px) rotate(45deg);
}

.burger-icon.burger-active .burger-line:nth-child(3) {
  -webkit-transform: translateY(-8px) rotate(-45deg);
  transform: translateY(-8px) rotate(-45deg);
}

.main-nav {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.92);
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  display: none;
}

.nav-items {
  display: block;
  position: relative;
  text-align: center;
  width: 100%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.nav-items li {
  padding: 25px 0;
}

.nav-items li a {
  font-size: 35px;
  font-weight: 700;
  color: #282828;
  text-transform: uppercase;
  -webkit-transition: all .3s;
  transition: all .3s;
}

.nav-border {
  .border-bottom: 1px solid rgba(75, 78, 83, 0.2);
  -webkit-box-shadow: 0 1px 10px #999;
  box-shadow: 0 1px 10px #999;
}

.show-nav {
  -webkit-transition: all .2s ease-in;
  transition: all .2s ease-in;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-wrapper">
  <header>
    <div class="container">
      <div class="row">
        <div class="col navbar">
          <div class="logo">
            <a href="index.php">Logo</a>
          </div>
          <div class="burger-icon">
            <span class="burger-line"></span>
            <span class="burger-line"></span>
            <span class="burger-line"></span>
          </div>
        </div>
      </div>
      <nav class="main-nav">
        <ul class="nav-items">
          <li class="wow fadeIn" data-wow-delay=".2s"><a href="index.php" class="active">One</a></li>
          <li class="wow fadeIn" data-wow-delay=".4s">
            <a href="about.php">Two</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".6s">
            <a href="projects.php">Three</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".8s">
            <a href="contact.php">Four</a>
          </li>
        </ul>
      </nav>
    </div>
  </header>
  <div class="container">
    <div class="row">
      <div class="test-div">

      </div>
    </div>
  </div>
</div>

Answer ā„–1

Adjust the header width to width: calc(100% - 80px);.

The 80px represents the margin on both sides.

$(window).scroll(function() {
  if ($(window).width() > 768) {
    if ($(document).scrollTop() > 100) {
      $('header').addClass('nav-border');
    } else {
      $('header').removeClass('nav-border');
    }
  }
});

$('.burger-icon').click(function() {
  $(this).toggleClass('burger-active');
  $('.main-nav').fadeToggle();
  $('body').toggleClass('nav-scroll-lock');
});
body{
  margin:0
}
.test-div {
  height: 1800px;
  width: 100%;
  background: black;
}

.main-wrapper {
  margin: 0 40px;
}

.container {
  max-width: 800px;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto
}

.row {
  margin-left: -15px;
  margin-right: -15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.col {
  -webkit-box-flex: 1;
  -ms-flex: 1 1 0px;
  flex: 1 1 0;
}

header {
  background-color: #fff;
  position: fixed;
  width: calc(100% - 80px);
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;

}

.navbar {
  height: 60px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.logo {
  position: relative;
  z-index: 1000;
  background: #151515;
  padding: 8px;
}

.logo a {
  font-weight: 700;
  color: #fff;
  font-size: 18px;
  letter-spacing: 1px;
}

.burger-icon {
  position: relative;
  z-index: 1000 !important;
  -ms-flex-item-align: end;
}

.burger-icon .burger-line {
  width: 40px;
  height: 3px;
  background-color: #282828;
  display: block;
  margin: 5px auto;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.burger-icon.burger-active .burger-line:nth-child(2) {
  opacity: 0;
}

.burger-icon.burger-active .burger-line:nth-child(1) {
  -webkit-transform: translateY(8px) rotate(45deg);
  transform: translateY(8px) rotate(45deg);
}

.burger-icon.burger-active .burger-line:nth-child(3) {
  -webkit-transform: translateY(-8px) rotate(-45deg);
  transform: translateY(-8px) rotate(-45deg);
}

.main-nav {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.92);
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  display: none;
}

.nav-items {
  display: block;
  position: relative;
  text-align: center;
  width: 100%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.nav-items li {
  padding: 25px 0;
}

.nav-items li a {
  font-size: 35px;
  font-weight: 700;
  color: #282828;
  text-transform: uppercase;
  -webkit-transition: all .3s;
  transition: all .3s;
}

.nav-border {
  .border-bottom: 1px solid rgba(75, 78, 83, 0.2);
  -webkit-box-shadow: 0 1px 10px #999;
  box-shadow: 0 1px 10px #999;
}

.show-nav {
  -webkit-transition: all .2s ease-in;
  transition: all .2s ease-in;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-wrapper">
  <header>
    <div class="container">
      <div class="row">
        <div class="col navbar">
          <div class="logo">
            <a href="index.php">Logo</a>
          </div>
          <div class="burger-icon">
            <span class="burger-line"></span>
            <span class="burger-line"></span>
            <span class="burger-line"></span>
          </div>
        </div>
      </div>
      <nav class="main-nav">
        <ul class="nav-items">
          <li class="wow fadeIn" data-wow-delay=".2s"><a href="index.php" class="active">One</a></li>
          <li class="wow fadeIn" data-wow-delay=".4s">
            <a href="about.php">Two</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".6s">
            <a href="projects.php">Three</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".8s">
            <a href="contact.php">Four</a>
          </li>
        </ul>
      </nav>
    </div>
  </header>
  <div class="container">
    <div class="row">
      <div class="test-div">

      </div>
    </div>
  </div>
</div>

Answer ā„–2

To maintain a distance of 40px from the edges on both sides and eliminate the width, one option is to define left and right values for the element.

header {
  background-color: #fff;
  position: fixed;
  left:40px;
  right:40px;
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;
}

Answer ā„–3

The reason for the header overlapping is due to the fact that when using a position fixed, it is always relative to the document. This means that by default, it will take the width of the entire document... To resolve this issue, I recommend checking out this example: Fixed position but relative to container

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

Assistance needed in creating a MVC3 and Razor 2-level horizontal navigation menu

I am attempting to create a 2-level horizontal menu, similar to the one featured on the following site: TV.Com Despite searching on google, I am struggling to put it all together. Below is the sample code I am working with: <div id="divNav"> <u ...

What is the most effective method for data binding using [innerHTML] in Angular 4?

One issue I've encountered is that in Angular 4, there are limited ways to perform data binding between HTML and TypeScript, such as {{myText}}, [], (), and [innerHTML]="myText". Which method is the most effective for binding a simple variable to HTM ...

Exploring Grails Assets, Redirections, Secure Sockets Layer, and Chrome

Using Grails 2.1.1 with the resources plugin, I have encountered an issue when incorporating the jstree library which comes with themes configuration: "themes":{ "theme":"default", "dots":false, "icons":true } The JavaScript in the library locat ...

Tips for obtaining user input to place markers on a video timeline with JavaScript

I am new to Java script and I am trying to create a video player where users can click on the progress bar to place markers. Can you please review my code below and let me know if there are any errors? index.html <!doctype html> <html> <he ...

Excessive gap located above the section element

On the web page I'm practicing with, I've noticed an unwanted space at the top of the "Favorite Food" section. To center my unordered list, I made the "section" elements inline-block, but this created the issue. How can I remove that space at the ...

Update the innerHTML content dynamically every 5 seconds with Vue.js

I'm working on a new website and I'd like to spice things up by changing the header text with a random word every 5 seconds. Here's the starting point: const header = document.querySelector('.header') const needs = ['jacket& ...

Utilize AJAX or preloading to enable dynamic updates for items within a select element

Forgive the length of this question, but I want to provide all relevant information. Within our extensive web application, we have a generic code for inputting addresses. These addresses can vary - business address, user address, online shop delivery addr ...

Struggling with responsive design and the challenges of absolute positioning

Currently in the process of creating a page for case studies, where a main banner image is followed by 3 rows of 3 sub-images. Each image is added through the background-image property to maintain consistent div heights. The divs have a hover container on ...

"Enhance User Experience with Multilevel Dropdowns in Bootstrap 4 - Submenus Aligned to the Top

I recently embarked on a project that involved using Bootstrap 4.4, The project is an eCommerce grocery store with departments comprising categories and subcategories. The main menu became very lengthy when using the default code, so I encountered some al ...

The issue with React select right-to-left (RTL) functionality is that it

When using react select, I include isRtl as a prop like so: <Select onChange={handleChange} isRtl isMulti options={colourOptions} /> However, only the input receives the rtl direction style and not the options. How can I ensure that both the input a ...

unable to access various attributes in an angular directive

I have a particular HTML setup that effectively outlines each attribute's value through a distinct "attachment" directive. These values are located in the attrs list of said directive, which is within a modal file upload form. <p>For Testing Pu ...

Errors in AMP Validator

I have recently started working with next/amp and I am encountering some issues with the amp validator in dev mode. The errors that I am struggling to understand are as follows: An error related to CSS syntax in the 'style amp-custom' tag - show ...

Obtain the selected option from a user list

In my user table, each user is listed with a name, surname, right, and a "commit changes" button. The purpose of this setup is to assign rights to different individuals. Each column contains the userID, and the entire structure is generated from a database ...

Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone kn ...

Steps for triggering an event based on a user-provided time input

I'm developing an AC controlling application that allows users to specify a time for all building AC units to be automatically turned off using HTML, jQuery, and CSS. I need to implement a feature where the system compares the user-entered time with t ...

Align select and number inputs vertically in Firefox

How can I achieve vertical alignment for these inputs on Firefox? The alignment works perfectly in Chrome, Safari, and Opera, but not in Firefox (Version 57 tested on macOS, Windows, and Linux Mint). While removing type="number" from the input resolves th ...

Customizing the initial page layout in Elm

I am new to Elm and I need help with a particular issue. Can someone provide guidance or direct me to a useful resource for solving this problem? The challenge Iā€™m facing involves editing the start page of a website by removing specific elements, as list ...

The font size on mobile webkit suddenly grows larger without warning

Currently in the process of enhancing a website for mobile devices, and I'm almost finished (yay!) but there's one specific issue that is really challenging: The Method I decided to use CSS to transform a tab bar (which is essentially a list) i ...

Using background-image in Internet Explorer has always been a challenge

HTML <div id="banner" style="display: block; background-image: url('images/swirl_home.gif'); background-repeat: no-repeat; background-color: #e7e7e7;"> <div id="Hi"> some text here... </div> & ...

How to delete URL parameters in JavaScript and HTML5

I have a URL that looks like this: /users/?i=0&p=90 How can I remove the part from ? to 90 Can anyone provide me with some JavaScript code to achieve this? EDIT I mean doing this with window.location.href directly in the browser URL bar. I trie ...