Issue arises with the Dropmenu due to the property Display: block causing an undesirable margin

I'm currently utilizing Bootstrap 4 and have applied a CSS style to the drop-down menu:

.dropdown .dropdown-menu {
  -webkit-transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  -moz-transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  -ms-transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  -o-transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  max-height: 0;
  display: block; /* this property */
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
}

.dropdown.show .dropdown-menu {
  -webkit-transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  -moz-transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  -ms-transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  -o-transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  max-height: 290px;
  opacity: 1;
  visibility: visible;
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="SupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

... (this part has been shortened for brevity) ...    

  </div>
</nav>

... (this part has been shortened for brevity) ... 
</html>

The issue I encountered is that the display: block; attribute resulted in an unwanted margin at the bottom on mobile devices:

https://i.sstatic.net/GNFuP.jpg

How can I resolve this problem?

Answer №1

Initially, the dropdown-menu is set to display:none with padding styles. When you change it to display:block, the padding starts having an effect even if the height remains at 0. To address this, add padding:0 and include it again when the dropdown is expanded:

.dropdown .dropdown-menu {
  transition: max-height 0.5s, opacity 0.2s 0.1s, visibility 0s 4s;
  max-height: 0;
  display: block; /**/
  padding: 0; /*Added this*/
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
}

.dropdown.show .dropdown-menu {
  transition: max-height 0.9s, opacity 0.2s, visibility 0s;
  max-height: 290px;
  opacity: 1;
  visibility: visible;
  padding: .5rem 0; /*Added this*/
}
<!doctype html>
<html lang="en">
<!- Head and body content goes here ->

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

Running a JQuery function within an Angular expression

Can Angular expressions execute JQuery code? Here's an example: <span>{{$("[for='loanType']").text()}}</span> I attempted it, but haven't been successful so far. Is there a way to make it work without creating a key/value ...

The overwriting of SCSS transition properties

I have a handy @mixin that I use to easily add different transition properties to elements. Here's an example of how I use it in my .scss file: @mixin transition($prop, $sec){ -webkit-transition: $prop $sec; -moz-transition: $prop $sec; -ms-tra ...

The CSS selector within the website's URL

When a URL like www.example.com/#signup is entered, the browser focuses its view on the HTML element with the ID signup. Is this correct? Can the CSS style of the element be changed if it is focused in this way? For example, if there is a div element wit ...

Display the chosen date from the datepicker in the input field

Utilizing a JQuery DatePicker to be constantly displayed, I have assigned it to a div. Despite wanting it to display the selected date in the input box like in this example, it just isn't functioning as desired for me. Is there a way to have the selec ...

Toggle JavaScript Query Display

Welcome to my HTML test website where I am testing show/hide JavaScript functionality. Upon loading the page, you may notice that everything is hidden until a button is clicked. <html> <head><title>Test</title> <scr ...

The mysterious case of disappearing Bootstrap 4 modals with data-target

I am retrieving a Json object from a local API, and everything is functioning properly. I use this object to populate a table with rows and columns, and each column contains a search button. When this button is clicked, it should open a modal displaying mo ...

In the Swiper function of JavaScript within a Django template, the occurrence of duplicate elements (products) is being generated

Experimenting with displaying products in a Django template, I decided to utilize the Swiper js class. This allowed me to showcase the products within a div, complete with navigation buttons for scrolling horizontally. However, when testing this setup wit ...

When the window size is reduced, the image continues into the next div

I need to create a page that displays text alongside an image. However, when the window size is minimized, the image overlaps with the text. Here is how it looks: https://i.sstatic.net/CBMh8.png To fix this issue, I am using the following HTML code: ...

Determine the position within the DOM of a webpage and dynamically update the navigation menu classes to reflect the changes

Help! I am trying to create a navigation menu with links that direct users to specific parts of my page. I want to use JavaScript to add a class to each navigation menu item when the scroll bar reaches the corresponding section in the HTML. I think I know ...

The Bootstrap Grid Leaves Blank Spaces Unfilled

View the screenshot here Take a look at the image above. I'm struggling to fill the empty space with div elements. I've experimented with display:inline-block and other display properties, but they haven't yielded the desired results. < ...

The URL is unresponsive following the deployment of the production build in tailwind

The image URL in my project is functioning correctly in the VITE server environment before the production build. However, after the production build, it fails to display the correct path of the image in the HTML. All other images are displaying properly ex ...

Tips for displaying the content of a personalized navigation tab using jQuery

I successfully created custom tabs using JQuery. Here is the code for my custom tabs: <div class="row"> <div class="col-4 master-advanced-left-tab master-advanced-left-tab-active"> <p>Item 1</p> </div> < ...

What is causing the z-index: -1 to not function properly in this particular case?

I'm struggling to get the <i> element to display below the anchor in the stacking order. Any suggestions on how to achieve this? a.button { background-color: gray; border-radius: 5px; color: rgb(247, 247, 247); display: inline-block; ...

"Enhance your code editing experience in Eclipse with HTML, CSS, and JavaScript syntax

Hi there! I'm looking for a way to enable syntax highlighting for HTML/CSS/JS in Eclipse. My main focus is on developing in Python using the PyDev package, but currently I am working on creating Cheetah templates which are difficult to read without pr ...

Tips for deleting the current row in an HTML table using jQuery and JSON?

Looking to implement code for batch deletion using jQuery and JSON. Here is the jQuery code snippet: function DeleteSelected() { var categories = new Array(); debugger; $("input[type='checkbox']").each(function () { ...

Is there a way to enable multiple selections in a particular section?

Currently, I am in the process of working on my step by step order and I want to express my gratitude for all the assistance I have received so far. Despite the help, I still have some unanswered questions! The steps in my project are categorized into dif ...

What are some methods for creating a tooltip or a similar quick information pop-up that appears instantly when hovered over?

I'm familiar with creating a basic tooltip by utilizing title="here's a tooltip", but I'm interested in having it appear instantly upon hovering instead of the usual delay. Is it feasible to achieve this using different tools such ...

Can you explain the contrast between open and closed shadow DOM encapsulation modes?

My goal is to create a shadow DOM for an element in order to display elements for a Chrome extension without being affected by the page's styles. After discovering that Element.createShadowRoot was deprecated, I turned to Element.attachShadow. Howeve ...

Ways to invoke jQuery(this) within an anchor tag's onclick event handler

My task involves working with HTML code and I need to apply a class name to the parent li tag using jQuery. Here is the HTML code snippet: <ul> <li><a onclick="setSelectedTestPlan();" href="javascript:void(0);">New Test</ ...

When I try to call jQuery's getScript function, the callback does not execute as expected within

When I invoke the script in a function at runtime function CheckOutMyFace() { $.getScript("/scripts/jtimers.js", function (data) { }); }; I notice in Firebug that the script is being called every time the function is invoked, but for some reason the ...