What is the best way to align a dropdown menu in Bootstrap 5?

When working with Bootstrap, I discovered two classes, 'dropdown-menu-end' and 'dropdown-menu-start', that almost perfectly suit my needs for a dropdown menu. However, what I truly desire is to have the dropdown menu centered on the webpage.

To learn more: https://getbootstrap.com/docs/5.0/components/dropdowns/#menu-alignment

To center the drop-down menu button on the page, I utilized the following CSS:

.dropdown .btn.dropdown-toggle {
    border-radius: 50px;
    position: fixed;
    z-index: 99;
    bottom: 45px;
    right: 50%;
    transform: translate(50%); 
}

The issue arises from the fact that the menu opens upwards automatically due to lack of space below. Although I tried applying custom CSS to reposition the dropdown menu, Bootstrap's inline styles take precedence at runtime, preventing any overriding.

I have created an example demonstrating the problem on jsfiddle: https://jsfiddle.net/meeky333/ve98ydkj/5/

If anyone has insights on how to tackle this challenge, please share!

P.S. Here is an illustration of the desired outcome: https://i.sstatic.net/mcaUX.png

Answer №1

To disable static positioning, use the

data-bs-display="static"
attribute on the button and then make inline adjustments to the desired attributes. This approach seems to achieve the same outcome as the previous solution, but without the need for external CSS with the !important flag

.buttonContainer {
  display: flex;
  position: absolute;
  top: 50%;
  left: 50%;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096b66667d7a7d7b6879493c273b2739246b6c7d6838">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="31535e5e45424543504171041f031f011c5354455000">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>
<div class="buttonContainer">
  <div class="dropup">
    <button class="btn btn-primary dropdown-toggle" data-bs-display="static" style="width: 6rem; height: 6rem; border-radius: 50%;" type="button" id="ddl-quick-links" data-bs-toggle="dropdown" aria-expanded="false">
           <i class="fa fa-chevron-up fa-2x"></i>
         </button>
    <ul class="dropdown-menu" style="width: 320px; height: max-content; transform: translate(-105px, -5px);" aria-labelledby="quick-links">
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add Item</a></li>
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add Person</a></li>
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add User</a></li>
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add Member</a></li>
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add Product</a></li>
      <li><a href="#" class="dropdown-item" style="padding: 10px; cursor: pointer;">Add CSS</a></li>
    </ul>
  </div>
</div>

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

Issue with fadeout() on absolutely positioned element loaded via AJAX in jQuery

Currently, I am utilizing AJAXify on a website project to implement page transitions and encountering some abnormal behavior with jQuery. Below is my code: HTML (I am transitioning through backgrounds using jQuery) <div id="backgrounds"> <img s ...

Change the blue line to a crisp, clean white

Is it possible to customize the color of the blue line that appears when clicked? class Greetings extends React.Component { render() { return <div>Greetings {this.props.name}</div>; } } ReactDOM.render( <div> <p tabInde ...

Is there a way to prevent hover effects on the outer div when hovering over the inner div?

I am facing an issue with disabling hover effects on an outer div when hovering over an inner button. I have tried various methods but haven't been successful in achieving the desired outcome. .container { background-color: #e6e6e6; width: 400p ...

Trouble with using .className for form validation

The .className is not applying the formValidation class on getWeight.length < 1 and getHeight.length < 1. I am stuck trying to figure out why this is happening after reviewing the code extensively. Any thoughts on what could be causing this issue? Y ...

What could be causing my logo to disappear from the Bootstrap navbar while incorporating Ruby on Rails?

Currently working with Ruby on Rails, I'm in the process of trying to incorporate a logo.png image into a bootstrap navbar. The following code snippet was used in app/views/home/_header.html.erb <nav class="navbar navbar-expand-lg bg-primary & ...

Bootstrap Inconsistency: Unnecessary spacing on the right side of the page

The navigation bar is overflowing the viewport on smaller devices, as observed on a personal device and in Chrome developer tools. From 575px onwards, there is increasing whitespace on the side of the screen, with content occupying only 25% at its smalles ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

Learn how to center content and stretch a column using Vuetify frameworks

Looking for a layout with 2 columns of equal height and centered content vertically? Here's how to achieve it using Vuetify! Check out the code snippet below: <div id="app"> <v-app> <v-row align="center"> ...

Resizing the background while scrolling on a mobile web browser

My website background looks great on desktop, but on mobile (using Chrome) the background starts to resize when I scroll, mainly due to the browser search bar hiding and reappearing upon scroll. Is there a way to prevent my background from resizing? Check ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

Display One Div at a Time in Sequence on Page Load Using jQuery

I have come across this question multiple times: How to Fade In images on page load using JavaScript one after the other? Fade in divs sequentially Using jQuery .fadeIn() on page load? Despite trying various recommended methods, I'm still unable t ...

Ways to make a background stretch infinitely in the x-axis

Currently, my header has a grey background with a width of 100% and a maximum width of 1000px. To extend the grey background beyond the 1000px limit, I have a separate div with an absolute position behind the header div. The issue arises when the user scro ...

How can I vertically center a back button in jQuery Mobile?

Recently, I've been incorporating the back button into my jQuery Mobile page. One thing I'm trying to achieve is vertically aligning the back button in the header. Appreciate any help in advance! ...

Custom Bootstrap 3 Panel Organization

I'm intrigued by the layout shown in the image attached. My attempts to recreate it using a combination of columns and rows have been unsuccessful. Could it be that I'm going about this the wrong way? ...

What is the best way to align content evenly between the logo and navigation items in the Bootstrap navbar?

Welcome to my HTML code: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="container-fluid"> <img class="logo" src="/images/logo.png" alt=""> < ...

CSS is functioning properly on a local level, but fails to take effect once the "npm run build" command is executed in the React application's build

I am attempting to override the CSS of Muidrawer-paper. It works locally after running "npm start", but it does not take effect when building the package. .css-12i7wg6-MuiPaper-root-MuiDrawer-paper { position: absolute !important; top: 50px !import ...

Unable to change background image to an external image link

<div class="flipbook-viewport"> <div class="container"> <div class="flipbook"> <div style="background-image:url("https://example.com/images/lareg/1.JPG")">< ...

Could CSS transitions stack on top of each other?

My current approach involves using CSS to create sliding menus. With JavaScript, I simply apply a class to trigger the CSS animations. The menu consists of 4 columns with list items in desktop mode, but collapses into only headings in mobile mode. These he ...

A step-by-step guide on closing a Bootstrap 5 Modal using JavaScript

Objective: I am trying to close a bootstrap 5 modal using JavaScript code after showing an alert message. Challenge: I am facing difficulty getting the function myFunction to work and subsequently closing the modal once the alert message is displayed ...

Designing a website's layout with HTML and CSS

I am having trouble developing a concept depicted in the image below. Here is what I have accomplished so far: https://jsfiddle.net/o0zmtr3q/2/. I am struggling to make each box unique, especially with the layout where the text is positioned differently in ...