problem with CSS negative margin

When trying to position the navbar inside a div with the class main using negative margins, it causes issues on small screens when clicking on the toggler icon. How can I address this problem without relying on negative margins?

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
<div class="top"></div>
<div class="wrapper">
    <nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
        <button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navDrop">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navDrop">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a href="#" class="nav-link">HOME</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">ABOUT</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">SKILL</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">PORTFOLIO</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">TEAM</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">BLOG</a>
                </li>
                <li class="nav-item">
                    <a href="#" class="nav-link">CONTACT</a>
                </li>
            </ul>
        </div>
    </nav>
    <div class="main"></div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-    bootstrap/4.1.1/js/bootstrap.min.js"></script>

View the output of my code here: https://codepen.io/Sakhawat5/pen/NzwQJz

Answer №1

--New Update--

It seems like the question is about adding the navbar class to the main div once the user has scrolled more than 50px. Here's a solution that might help:

$(document).scroll(function() {
  if ($(document).scrollTop() >= 50) {
    // User has scrolled 50 pixels or more;
    // Do something like:
     $('.navbar').appendTo('.main');
    // This will append the navbar to the main div. If you want additional styling for the navbar, you can do:
     $('.navbar').addClass('.UserHasScrolled')  
    // Now, in your CSS, style the .UserHasScrolled class accordingly.
  } else {
    // Perform actions when the user has NOT scrolled more than 50px
  }
});

Don't forget to include jQuery by adding this script below all other scripts:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

To style the .UserHasScrolled class, you can use properties like:

.UserHasScrolled {
     position: absolute;
     width: 100%;
     top: 0;
}

--Initial Response--

If you're looking to target screen sizes less than 767px and apply specific styles, you can use media queries like this:

@media screen and (min-width 767px) {
   .navbar {
        margin-bottom: -336px;
   }
}

However, this may lead to an issue where the main class retains a margin of -336px when the navbar is closed.

To ensure the navbar stays on top, set its position to fixed:

.navbar {
     position: fixed;
     width: 90%;
     margin-left: 5%;
     margin-right: 5%;
}

Answer №2

Here is the solution you've been looking for:

.top{
  height:50px;
  background:red;
}
.navbar{
  margin-left:20px;
  margin-right:20px;
}
.main{
  height:1500px;
  background:orange;
}
<div class="top"></div>
<div class="wrapper">
  <div class="main">
    <nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
    <button class="navbar-toggler ml-auto" data-toggle="collapse" data-target="#navDrop">
    <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navDrop">
    <ul class="navbar-nav">
    <li class="nav-item">
      <a href="#" class="nav-link">HOME</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">ABOUT</a>
    </li>
    <li class="nav-item">
      <a href="#" class="nav-link">SKILL</a>
   </li>
   <li class="nav-item">
     <a href="#" class="nav-link">PORTFOLIO</a>
   </li>
   <li class="nav-item">
     <a href="#" class="nav-link">TEAM</a>
   </li>
   <li class="nav-item">
     <a href="#" class="nav-link">BLOG</a>
   </li>
   <li class="nav-item">
     <a href="#" class="nav-link">CONTACT</a>
   </li>
 </ul>
</div>
  </nav>
  </div>
</div>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-    bootstrap/4.1.1/js/bootstrap.min.js"></script>

Answer №3

To resolve the issue, make the following adjustments to your CSS:

.navbar{
    margin-left: 5%;
    margin-right: 5%;
    width: 90%;
    position: fixed;
}

If you prefer not to have a fixed navigation bar:

.navbar{
    margin-left: 5%;
    margin-right: 5%;
    width: 90%;
    position: absolute;
}

Feel free to reach out if you need further clarification.

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 replicate a div's background color using an image within the body element?

I am looking to achieve a layout similar to the one below: https://i.sstatic.net/1cOYw.png The dark grey color represents the sidebar, but I want to use this color as a repeating vertical image in the body element without covering the footer (light gray) ...

Adjust the color of the text in the Stepper component using React Material UI

Is there a way to modify the text color within the stepper? Specifically, I aim to have the number 3 displayed in black instead of white: Image Link ...

AngularJS - Show Banner after two rows, then show two more rows

I'm attempting to showcase a Banner <div> after 2 rows of 4 x "col-md-3", succeeded by another 2 Rows - thus, resulting in the following markup: <div class="col-md-3">1</div> <div class="col-md-3">2</div> <div clas ...

Tips for refreshing CSS following an ajax request

Currently, I am facing an issue with the CSS formatting on my page. I am using Django endless pagination to load content on page scroll. However, when new content is loaded, the CSS applied to the page does not work properly and needs to be refreshed. Can ...

Is there a way to utilize the reset() function within an input form?

Is there a way to automatically reset the textbox when submitting a value? Below is my code: <!DOCTYPE html> <html> <body> <ul id="myList"> </ul> <input type="text" id="myText" value="&q ...

I am dynamically generating table rows and populating them with data. However, I encountered an issue with retrieving the data by their respective IDs

Creating a table row dynamically by populating it with data. However, encountering an issue with retrieving the data by their respective id. if(xmlhttp.status == 200) { var adminList = xmlhttp.responseJ ...

How can you retrieve input values from a form using a button click but without requiring a

I am facing an issue with the delete button on my form. The form has three buttons - one to edit the form on another page, one to add a value on the existing page, and one to delete a value. While the submit and edit buttons work fine, I am struggling wi ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

Appear and disappear gradually when hovering

When hovering over #one, the class .hidden is applied to #first. I have added a script to make .hidden fade out using transition: ease-in 0.3s;, but I am having trouble getting the fade in effect to work. I attempted adding transition: ease-in 0.3s; to #fi ...

Struggling to make addClass and removeClass function correctly for the development of the unique "15 puzzle" game

I am currently in the process of creating a version of "the 15 game" using jQuery in HTML. You can find more information about the game on this page. The objective of the game is to rearrange a table of "boxes" in ascending order from 1 to 15. Below is th ...

Infinite layers of options in the dropdown navigation bar

I am facing an issue with a dynamically generated unordered list that I want to turn into a dropdown menu. The challenge is that I do not know how many levels there will be, and I only want to display one level at a time. Here is what I have tried so far ...

Utilize an icon within an input box using content and the :before pseudo-element instead of relying on

My problem is that I have a great font set up and I use it to add custom icons next to my buttons. (for example: check here) Now, I want to create an input box and place an icon before it like in this guide HERE Instead of using a background image, I wou ...

Tap on the image placeholder to replace it with your own image

Just starting out, I have the following HTML code within a <form>: <div class="image-upload"> <img id="send_photo_img1" src="assets/images/index2.png"/> <input id="send_photo_input1" type="file"/> <img id="send_photo_img2" sr ...

What is the best way to send multiple values from the view to a method without using two-way binding?

When I change the dropdown value for the route type in my code, I need to pass both the gender value and the route type ID to my data retrieval method. Currently in my HTML file, I have only written a change event. I attempted two-way binding but encounte ...

Adjust the size of the Div and its content to fit the dimensions of

Currently, I have a div containing elements that are aligned perfectly. However, I need to scale this div to fit the viewport size. Using CSS scale is not an option as it does not support pixel values. https://i.stack.imgur.com/uThqx.png I am looking to ...

Searching dynamically using class names with JQuery

I am seeking to create a dynamic search input based on the class names within the span tags. However, I am struggling with displaying the class name that I have identified. My goal is to show the class names that match the entered value in the input on the ...

Is there a glitch in MacOS Firefox's background-size:cover feature? Any tips on how to work around it?

Here is an example link to see the issue: I've noticed there is a strange empty 1px line at the bottom of the box. It seems to occur when Firefox tries to resize an image with an odd number of lines. Has anyone else encountered this problem? If so, h ...

Errors in Animation in Bootstrap Carousel Implementation - slides fail to smoothly transition on and off the screen

It appears that when the slideshow transitions to the next slide or image, it loads partway across the screen instead of smoothly displacing the previous slide. I have attempted the following troubleshooting steps: Eliminating any special classes applied ...

Enhance the v-autocomplete dropdown with a personalized touch by adding a custom

Currently utilizing the v-autocomplete component from Vuetify, and I am interested in incorporating a custom element into its dropdown menu. You can see the specific part I want to add highlighted with a red arrow in this screenshot: This is the current s ...

A dynamic homepage that transforms upon login, created without the use of any content management systems

I am embarking on creating a website with registration and login features without relying on any CMS, using only pure HTML/CSS and other necessary tools. My main confusion lies in how I can develop a homepage that can cater to any visitor by displaying rel ...