How can I position a Bootstrap dropdown textbox or menu directly beneath its button?

Currently, I am working on implementing a simple dropdown textbox for a button within a navbar. However, I am encountering an issue where the text does not drop below the button as expected. Initially, it was displaying to the left, obstructing the brand logo. After setting the dropdown-menu position to relative, it now appears on the right side, causing the navbar to expand downward. I have attempted adjusting the margins with no success.

I want the dropdown text to appear directly below the button and navbar. I am unsure of why this setup is not functioning correctly?

HTML

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">news</a>
    </div>

    <div class="dropdown show">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        How It Works
      </button>
      <div class="dropdown-menu" data-toggle="dropdown" aria-labelledby="dropdownMenuButton">
        <div class="dropdown-item"> Click </div>
      </div>
    </div>

  </div>
</nav>

CSS

.navbar-header .navbar-brand{
  color: rgb(97, 100, 206);
  font-size: 2.0em;
}

.navbar {
  background-color: rgb(71, 71, 88);
  background-color: black;
  height: 100%;
  font-family: 'Alegreya Sans', sans-serif;
  margin: 0;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  height: 120px;
  width: 150px;
  position: relative;
}

.dropdown button {
  background-color: black;
  color: rgb(163, 163, 179);
  border: .5px solid black;
  float: left;
  margin-top: 9px;
}

Scripts

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>AetherNews</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link href="https://fonts.googleapis.com/css?family=Alegreya+Sans:900" rel="stylesheet">

</head>

Answer №1

To update your HTML code, simply remove the show class from the

<div class="dropdown show">
. Additionally, in your CSS file, delete the line position: relative that applies to the .dropdown-menu class:

/*NAV*/

.navbar-header .navbar-brand {
  color: rgb(97, 100, 206);
  font-size: 2.0em;
}

.navbar {
  background-color: rgb(71, 71, 88);
  background-color: black;
  height: 100%;
  font-family: 'Alegreya Sans', sans-serif;
  margin: 0;
  /*justify-content: space-between;*/
  /*align-self: center;*/
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-menu {
  height: 120px;
  width: 150px;
}

.dropdown button {
  /*background-color: #4446ce;*/
  background-color: black;
  color: rgb(163, 163, 179);
  border: .5px solid black;
  float: left;
  margin-top: 9px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">news</a>
    </div>

    <div class="dropdown">
      <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        How It Works
      </button>
      <div class="dropdown-menu" data-toggle="dropdown" aria-labelledby="dropdownMenuButton">
        <div class="dropdown-item"> Click </div>
      </div>
    </div>

  </div>
</nav>

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 steps should I take to ensure my bootstrap form is fully responsive?

I'm struggling to make my form responsive. It looks fine on desktop, but not on mobile. As a beginner, I feel lost... Any help with this would be greatly appreciated. Here's the code snippet: <div class="container"> <div class="row ...

Tips for preventing bootstrap from resizing the top card image too large while maintaining consistency across various image files

Hello, I'm facing an issue with bootstrap image sizing. When I insert images with smaller dimensions into a card, they are being blown up to fit the card which results in them becoming blurry. Here is the HTML code that's causing the problem: { ...

Automatically sync textbox width with gridview dimensions

My goal is to dynamically resize a number of textboxes so that they match the width of my gridview's table headers. The gridview will always have the same number of columns, but their widths may vary. However, as shown in the image below, the width va ...

Issue with menu display on Internet Explorer

Having some trouble with IE. My menu displays fine in Firefox and Chrome, but IE is causing issues... It seems that IE isn't rendering the <li> tag properly, so my dropdown menu isn't functioning in IE. Can anyone assist me? Here's t ...

Disregard the CSS styling within the modal window

Currently, I am working with ReactJS and ANTD. My modal has been adjusted with paddings to center-align the text, but now I want to enhance the design. Here is the desired look: https://i.stack.imgur.com/qef7n.png This is how it appears at the moment: htt ...

Developing a jsp page with interconnected drop down menus

I am looking to dynamically populate the options in a second drop-down based on the selection made in the first drop-down. For instance, if I have a first drop-down with options {India, South Africa, USA}, and I choose India, then the second drop-down shou ...

Data is transmitted from the server side to standard HTML

Is there a way to transfer data from the server-side to the client-side without relying on view engines like Pug or EJS? Currently, I am using XHTTP requests to retrieve data, but it would be more convenient to minimize that dependency. function getAllBe ...

Defaulting summernote to display in italics

Looking for a way to italicize a series of summernote inputs by default? I have removed all toolbar options except the italics button. Here's the HTML generating these inputs: <div style="width:250px;"> < ...

What could be causing my button to be elongated in a vertical direction when a logo image is present

I am currently utilizing tailwindcss for my project. Within a flexbox container, I have placed a login button which appears to be stretched out. <nav font-am class="m-6 text-xl"> <div class="flex flex-row justify-between conta ...

Conceal the bullet point in an unorganized list

I am currently attempting to customize the appearance of an unordered list that is being used for the navigation menu on a specific website. The website in question is: On this site, you will notice that there is a dot or disc between each menu item, disp ...

A layout featuring nested buttons and links within a card element, utilizing the power of Link in NextJs

After extensive searching on S.O., I have been unable to find a solution that works flawlessly. The issue at hand involves a card component in a NextJs application that is encompassed within a <Link> tag. Additionally, there is another <Link> t ...

What is the best way to create a JavaScript function that can be used for multiple expandable cards in HTML and CSS?

I'm dealing with a situation where I have a list of cards, and I want to be able to expand the content individually when clicking on "more info". Can someone offer advice on how to achieve this using Javascript? Check out this CodePen for reference: ...

Add a button that allows users to input their information into an array, and then display the data from the array in a <div>

I'm new to the coding world, so please bear with me if I miss any unspoken rules. I welcome any feedback. Essentially, I have three text input fields: Name:, Age:, and Password:. If any of these fields are left empty, an error message will display. H ...

Adjusting badge width in Bootstrap v5 for fixed positioning

Is it possible to have a button with a notification badge that maintains a consistent size regardless of the number displayed? <button class="btn btn-orange w-75 fs-4 mb-3 position-relative"> Next Round <span class=" ...

Button click not triggering Ajax functionality

I've been working on implementing a simple Ajax function in a JSP using JQueryUI. The goal is to pass two text fields from a form and use them to populate two separate divs. However, when I click the button, nothing seems to be happening. I even tried ...

Building a Dynamic Checkbox Validation Feature in Angular Using Data retrieved from an API

Currently, I have a function that retrieves and displays a list obtained from an API: displayEventTicketDetails() { this.Service .getEventTicketDetails().subscribe((data: any) => { this.eventTicketDetails = data.map(ticket => ticket. ...

How can I extract the HTML value of a JSON array element using jQuery?

My jQuery function takes PHP JSON encoded data as an argument. function html_modify(html) { var str = JSON.stringify(html); var arr = $.parseJSON(str); var tot = ''; $.each(arr, function(i, val){ if(i == 'options') { ...

Error Encountered in HTML Form

I've created a form, but when I hit the submit button, an error pops up, and I can't figure out what's causing it. <div class="status alert alert-success" style="display: none"></div> <form name="contactform" method="post" ...

Rearranging HTML elements using jQuery

I have a collection of 5 HTML elements <a href="#"><img src="image1.jpg" /></a> <a href="#"><img src="image2.jpg" /></a> <a href="#"><img src="image3.jpg" /></a> <a href="#"><img src="image4.j ...

AngularJS: Utilize the ngStyle directive to add styling to a specific child

I'm trying to style an embedded svg file within a span using ng-style based on its index. Can anyone help me figure out how to do this? <li ng-repeat="menuItem in menuItems" class="menuitem" ng-class="{'smenuitem': ($index === menuselect ...