How can I eliminate the space between the navbar and toggle menu item in Bootstrap 4?

Upon experimenting with Bootstrap 4, I came across the default navbar template. While using it for learning purposes, I encountered difficulties when trying to modify or style the CSS. I'm having trouble understanding why there is a gap between the navbar and the toggle menu item.

I'm attempting to eliminate this gap, but finding it challenging to achieve the desired result. Here is an image showing the issue: Gap-img

<html>

<head> </head>

<body>
  <nav class="navbar navbar-expand-md navbar-inverse navbar-fixed-top" role = "navigation">
    <div class="navbar-header">   
<a class="navbar-brand" href="#">
    <img src="https://upload.wikimedia.org/wikipedia/commons/e/ea/Boostrap_logo.svg" width="22" height="22" class="d-inline-block align-top" alt="">
    Bootstrap
  </a>
        <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<!--         <button class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse-x"> -->
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li class="nav-item"><a href="#">Home</a></li>
        <li class="nav-item"><a href="#">About</a></li>
        <li class="dropdown-divider"></li>
        <li class="nav-item"><a href="#">Contact</a></li>
      </ul>
    </div>
  </nav>

  <section>
    <div class="container">
      <div id="about">
        <h1>My title</h1>
        <p>Hello World. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Laudantium error tempore voluptas iusto. Deleniti impedit repellendus mollitia ad, repudiandae ab fugit ipsa in laboriosam culpa, aspernatur amet quo modi possimus.</p>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque ea assumenda, soluta natus culpa debitis perspiciatis repudiandae doloremque eum repellendus fugiat corrupti aspernatur accusamus neque laborum totam voluptates error possimus.</p>
      </div>
    </div>
  </section>

</body>

<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>

</html>`




.navbar-collapse {
  border: 0;
  -webkit-box-shadow: none;
}

.nav-item{
   height:49px;
      margin-top: 0px;
      margin-bottom: 0px;
        padding-top:0px;
        padding-bottom:0px;
}

/*  */
.navbar-default,
.navbar-inverse {
  /* SET color for top bar*/
  background-color: darkblue;
  margin-bottom:20px;
}

.navbar-toggle {
  border: 0px;
  border-radius: 0px;
  padding: 18px;
  margin: 0px;
  background-color: rgba(0, 0,200, 0.5);
  outline: none;
}

.navbar-toggle .icon-bar:nth-of-type(1) {
  height: 2px;
  top: 1px;
}
.navbar-toggle .icon-bar:nth-of-type(2) {
  height: 2px;
  top: 0.9px;
}
.navbar-toggle .icon-bar:nth-of-type(3) {
  height: 2px;
  top: 0.7px;
}

Any assistance on resolving this issue would be highly appreciated.

Answer №1

After analyzing your Codepen, I discovered that the ul element in your code has margins of 7.5px on both the top and bottom. To get rid of these margins, simply add the following rule to your code. By setting the second value (left and right margin) to a negative value, you will ensure that the size remains long enough for the background color to change when hovered over. If this doesn't make sense, try applying a 0 margin to all sides and observe the difference.

CSS

.navbar-nav {
  margin: 0px -15px;
}

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

Refreshing the page in Next.js causes issues with the CSS classNames

I am currently in the process of migrating a React SPA to Next.js, and I am relatively new to this framework. The issue I am encountering is that when I initially load the Home page, everything appears as expected. However, if I refresh the page, incorrect ...

Progressive zoom effect applied to an image in a JavaScript slideshow

Can someone assist me in replicating the zooming effect demonstrated in this code snippet: `http://codepen.io/docode/pen/EjyVQY`. I am specifically focusing on the zooming feature of this slideshow. How can I replicate this zooming effect in JS rather t ...

Adaptable images - Adjusting image size for various screen dimensions

Currently, my website is built using the framework . I am looking for a solution to make images resize based on different screen sizes, such as iPhones. Can anyone suggest the simplest way to achieve this? I have done some research online but there are t ...

Challenges experienced during the process of uploading a website to the server

I seem to have encountered an issue where the Navigation background image is missing after uploading my website onto the server. Surprisingly, everything else seems to be working perfectly. What could possibly be the cause of this discrepancy? navbar-de ...

The stylesheet reference, <link rel="stylesheet" href="../css/style.css">, appears to be malfunctioning

My project structure includes css files in a folder named css and jsp files in a folder named jsp, both located inside the WEB-INF folder. I am wondering how to access the css file within a jsp file. <link rel="stylesheet" href="../css/style.css> T ...

Issues with displaying or hiding a DIV using javascript - popup box unresponsive

I am working with this ASPX code: <div id="IsAccountingOk" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a><br /> <asp:Label ID="lblIsAccountingOkHeader" runat="server" Text ...

exclude the last item in flexbox from wrapping using the nowrap property

I have implemented flexbox nowrap on my container and I am wondering if it is possible to use CSS, without JavaScript, to push the last item in the container onto a new line at a specific width. This should be done using media queries and I am open to us ...

Issue with Bootstrap5: images not properly fitting into the carousel content

Hey, I am currently working on implementing a Bootstrap carousel on my webpage. However, I am encountering an issue with the display of images uploaded by users. When a user uploads a large image, it ends up overflowing on my webpage. How can I ensure that ...

What is the best way to ensure a container adjusts perfectly to its contents without constraining the contents to their minimum width?

Imagine you have a div with a flexible width (like min-width 200px and max-width 1000px). Let's refer to this div as Div1. The goal is to enclose this div in a parent div that adjusts its size to fit Div1 without impacting Div1's width whatsoever ...

Button hyperlink

I'm working on a new 'tour' page that will showcase the dates for upcoming shows. I would like to include a button on the page that, when clicked, will display the number of available tickets and provide an option to purchase them. I am also ...

The conditional comment designed for browsers other than Internet Explorer may not work properly on outdated versions of

I find myself in a predicament where I must dynamically load one of two stylesheets depending on the browser accessing the page: If any browser other than IE, then load the "new" stylesheet If IE is version 9 or higher, then load the "new" stylesheet ...

Create dynamic CSS pseudo content for before/after in Angularjs automatically

I have a question regarding applying pseudo elements dynamically using Angular. I have a div element where I need to push data to the content attribute in CSS dynamically from my scope. How can I achieve this with Angular? Here is a sample CSS: .bar:befo ...

What is the best way to make a CSS change triggered by a onScroll() event stay in place permanently?

I need a div element to be displayed as a "scroll back to top" button on my webpage when the user has scrolled past a certain point. To achieve this, I am using the JavaScript code provided below: $(document).ready(function() { $(window).scroll(functio ...

Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...

Sliding through HTML content

Unfortunately, I lack expertise in advanced HTML and CSS. After some investigation, I attempted to create a "content slider" similar to the one on the Redlight Traffic website (an anti-human trafficking organization). However, my efforts have been unsucces ...

Seeking guidance on utilizing jQuery to trigger changes in CSS properties upon

Just starting out with jQuery and hoping to find an easy script (or maybe a more straightforward approach) that allows for changing the CSS property of one element when another element is hovered over. For instance, hovering over this image could cause th ...

Designing a dynamic button that expands on hover to catch the eye

My instructor has provided us with this code snippet: <a class="btn btn-tertiary" href="#"> <span class="circle"> <i class="arrow-right"> </i> </span>Create profile </a> .btn.bt ...

The issue of assigning Ajax data to jQuery inputs with identical classes is not being resolved correctly

I am currently developing an Invoicing System where the Rate(Amount) value changes automatically when the Product(Item) is changed. The issue I am encountering is that when I change the first Product, all the other Product Rates change to the Rate of the ...

Incorporating chart.js into a current Django page: a step-by-step guide

I am currently developing a website that includes a feature displaying total hours worked by an employee. I am looking to enhance this function by also showing the hours worked for each day in a month. I have successfully created a chart for this purpose, ...

Ways to decrease font size and insert a line break within a constrained input space?

I am facing an issue where the input box remains static with old numbers when it overflows, and newly typed numbers do not appear at all. <!DOCTYPE html> <html> <body> <input type="text" id="output-area" placehold ...