Show submenu in Bootstrap 4 dropdown on the right-hand side

I am working with a Bootstrap 4 multilevel dropdown menu and I am trying to figure out how to display the submenu on the right side of the parent dropdown, rather than just aligning it to the right. Any suggestions on how to achieve this?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />


<nav class="navbar navbar-custom navbar-expand-md bg-dark navbar-dark">

  <button class="navbar-toggler" data-toggle="collapse" data-target="#collapse_target">
              <span class="navbar-toggler-icon"></span>                  
            </button>

  <div class="collapse navbar-collapse" id="collapse_target">

    <ul class="navbar-nav">
      <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" data-target="#dropdown_target" href="#">
                    DEPARTMENTS
                  </a>
        <ul class="dropdown-menu" aria-labelledby="dropdown_target">

          <li class="dropdown-item dropdown">
            <a data-toggle="dropdown" data-target="#auto_en" href="#">Automobile Engineering</a>
            <ul class="dropdown-menu dropdown-menu-right" aria-labelledby="auto_en">
              <li class="dropdown-item"><a href="#">HOD's Desk</a>
              </li>
              <li class="dropdown-item"><a href="#">About Us</a></li>
              <li class="dropdown-item"><a href="#">Faculty</a></li>
              <li class="dropdown-item"><a href="#">Events</a></li>
              <li class="dropdown-item"><a href="#">Class 
        Timetable</a></li>
              <li class="dropdown-item"><a href="#">Results</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

In the specific case of the Automobile Engineering submenu: -HOD's Desk -About Us -Faculty.... I would like these items to appear on the right side of the parent menu instead of inside it.

I have tried using the "dropdown-menu-right" Bootstrap class, but this only aligns the menu to the right without moving it outside the parent menu.

Any help is appreciated!

Answer №1

To adjust the position of a dropdown menu, add the following code specifying the left position:

.dropdown-menu.show.abc {
  left: 95%;
}

.dropdown-menu.show.abc {
  left: 95%;
  top: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />


<nav class="navbar navbar-custom navbar-expand-md bg-dark navbar-dark">

  <button class="navbar-toggler" data-toggle="collapse" data-target="#collapse_target">
              <span class="navbar-toggler-icon"></span>                  
            </button>

  <div class="collapse navbar-collapse" id="collapse_target">

    <ul class="navbar-nav">
      <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" data-target="#dropdown_target" href="#">
                    DEPARTMENTS
                  </a>
        <ul class="dropdown-menu abc" aria-labelledby="dropdown_target">

          <li class="dropdown-item dropdown">
            <a data-toggle="dropdown" data-target="#auto_en" href="#">Automobile Engineering</a>
            <ul class="dropdown-menu dropdown-menu-right" aria-labelledby="auto_en">
              <li class="dropdown-item"><a href="#">HOD's Desk</a>
              </li>
              <li class="dropdown-item"><a href="#">About Us</a></li>
              <li class="dropdown-item"><a href="#">Faculty</a></li>
              <li class="dropdown-item"><a href="#">Events</a></li>
              <li class="dropdown-item"><a href="#">Class 
        Timetable</a></li>
              <li class="dropdown-item"><a href="#">Results</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</nav>

Answer №2

  .responsive-navbar .menu-items .dropdown-list {
        placement: fixed;
        top-position: -7px;
        right-margin: auto;
        left-offset: 100%;
    }

Answer №3

This might be the solution you are seeking: https://codepen.io/svnt/pen/beEgre

HTML:

<div class="container">
    <div class="row">
        <h2>Dropdown Menu in Bootstrap</h2>
        <hr>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Dropdown
            </button>
            <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                <li class="dropdown-item"><a href="#">Some action</a></li>
                <li class="dropdown-item"><a href="#">Some other action</a></li>
                <li class="dropdown-divider"></li>
                <li class="dropdown-submenu">
                  <a  class="dropdown-item" tabindex="-1" href="#">Hover me for more options</a>
                  <ul class="dropdown-menu">
                    <li class="dropdown-item"><a tabindex="-1" href="#">Second level</a></li>
                    <li class="dropdown-submenu">
                      <a class="dropdown-item" href="#">Even More..</a>
                      <ul class="dropdown-menu">
                          <li class="dropdown-item"><a href="#">3rd level</a></li>
                            <li class="dropdown-submenu"><a class="dropdown-item" href="#">another level</a>
                            <ul class="dropdown-menu">
                                <li class="dropdown-item"><a href="#">4th level</a></li>
                                <li class="dropdown-item"><a href="#">4th level</a></li>
                                <li class="dropdown-item"><a href="#">4th level</a></li>
                            </ul>
                          </li>
                            <li class="dropdown-item"><a href="#">3rd level</a></li>
                      </ul>
                    </li>
                    <li class="dropdown-item"><a href="#">Second level</a></li>
                    <li class="dropdown-item"><a href="#">Second level</a></li>
                  </ul>
                </li>
              </ul>
        </div>
    </div>
</div>

CSS:

.dropdown-submenu {
    position: relative;
}

.dropdown-submenu>.dropdown-menu {
    top: 0;
    left: 100%;
    margin-top: -6px;
    margin-left: -1px;
    -webkit-border-radius: 0 6px 6px 6px;
    -moz-border-radius: 0 6px 6px;
    border-radius: 0 6px 6px 6px;
}

.dropdown-submenu:hover>.dropdown-menu {
    display: block;
}

.dropdown-submenu>a:after {
    display: block;
    content: " ";
    float: right;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
    border-width: 5px 0 5px 5px;
    border-left-color: #ccc;
    margin-top: 5px;
    margin-right: -10px;
}

.dropdown-submenu:hover>a:after {
    border-left-color: #fff;
}

.dropdown-submenu.pull-left {
    float: none;
}

.dropdown-submenu.pull-left>.dropdown-menu {
    left: -100%;
    margin-left: 10px;
    -webkit-border-radius: 6px 0 6px 6px;
    -moz-border-radius: 6px 0 6px 6px;
    border-radius: 6px 0 6px 6px;
}

Please note that these codes do not belong to me.

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

Using jQuery to eliminate any symbols from a text field

I am working on a text symbol removal function. However, I want to make sure that commas and periods ( . , ) are retained in the text field. Currently, the function is removing everything except digits. How can I modify it to keep the commas and dots int ...

When using CasperJS to capture multiple screenshots, the most recent screenshot will replace all previous ones

Exploring CasperJS has been a great experience for me. Despite my enjoyment, I've encountered an issue with casper.capture() that has me stumped. I've set it up to capture screenshots whenever a test fails and placed it in a separate setup module ...

What is the best approach to ensure that a loop is completed within an async function before moving on to execute code in another async function?

I am facing a scenario where I have an asynchronous function that iterates through an array of files for uploading. I also have another function responsible for submitting the final form, but it needs to ensure that all uploads are complete before proceedi ...

Deactivating a button if the input fields are blank using ReactJS

Hi there, I'm new to reactJS and recently encountered an issue with my code. Everything seems to be working fine except for the NEXT button not being disabled when text fields are empty. My expectation is that the NEXT button should only be enabled af ...

The function attached to the `click` event of `#cart-continue` is not invoking

I'm currently working on implementing a navigation to a specific anchor tag when the user clicks the continue button on the cart page. This project involves a mobile application built with Cordova, and the function call is done using jQuery. Here is ...

Customizing the "added to cart" message in Woocommerce and setting up a notification system for customers

My main focus is on customizing the Woocommerce notation system for a personalized shopping cart experience. After some research, I stumbled upon a very helpful user response which suggested modifying the message by adding the following code snippet to th ...

Is there a way to incorporate a computed value into a table prop while working with Element UI?

In my frontend development, I am utilizing vuejs along with element ui. I am faced with the task of rendering a table that includes dates in unix format. To make these dates more user-friendly, I have incorporated moment.js to convert them into a readable ...

Executing a function within the constructor in Angular 2/IONIC 2

I am new to Angular 2 and I have a question regarding invoking a child method from the current constructor. Is it possible to call the getPosition method from the constructor? I attempted to do so, but encountered an exception stating "getPosition is not ...

Tips for extracting only a portion of the JavaScript timestamp

I have a JavaScript timestamp that reads Tue Sep 30 2014 12:02:50 GMT-0400 (EDT). When I use the .getTime() method, I get 1412092970.768 Typically, this timestamp represents a specific time of today. My question is whether it's possible to extract o ...

Is it possible to implement a rolling time picker using jQuery UI Datepicker?

Currently, I am working on a project that heavily utilizes jQuery's UI Datepicker. However, there is one field that also requires the selection of a time, and the client has requested an iPhone-like timepicker in addition to the original date picker. ...

How can you display an alert message when new data is successfully added to a database using ajax?

In my web application, I have implemented a functionality to display an alert message when new data is successfully inserted into the database. The Ajax code below is responsible for sending a request to the EditDeleteLecture.php file. However, a challenge ...

Encountering a blank response when using $http.get() in AngularJS version 1.5.0

Within my AngularJS application, I have established a factory that is responsible for fetching updated data from the server following the deletion of one or more items from a list. var myApp = angular.module('app'); myApp.factory('mvListRef ...

Adjust the default margins and padding of email header images specifically for Outlook emails

I am currently working on coding an email, and I have encountered an issue with the alignment of the hero image, specifically in Outlook. https://i.sstatic.net/ap7SG.png Despite trying various solutions from previous answers to this problem (such as sett ...

Incorporate new markers into Google maps without the need to constantly initialize the map

My current goal is to have the user input a latitude and longitude into a text box, and then display a marker on the map for that location without needing to reinitialize the map each time. To start, I have set up my map like this: <script type="text/ ...

What is the reason for my algorithm's inability to work with this specific number?

I'm currently working on creating an algorithm to compute the sum of prime numbers that are less than or equal to a specified number. Below is my attempt: function calculatePrimeSum(num) { // initialize an array with numbers up to the given num let ...

Safari not updating Angular ng-style properly

I created a simple carousel using Angular and CSS animations which works well in Chrome. However, I recently tested it in Safari and noticed that the click and drag functionality does not work. I've been trying to fix this issue for days and could use ...

In the event of API data failure, take action

I've integrated a geo lookup API known as wipmania into my website to determine the country of origin for each user and perform various actions accordingly. jQuery.getJSON('https://api.wipmania.com/jsonp?callback=?', function (data) { // ...

Unexpected token encountered when exporting an Arrow function in an Express class

A situation has come up where I have a file containing my class which was exported and used in express and Node. I am interested in implementing arrow functions, so here is an example: class MyClass { myFunc(arg) { console.log(arg); } ...

Arranging a list using Flexbox with equal spacing between elements in each row containing three items

I'm struggling with aligning a flexbox list. I have a row of 3 elements, but when there are only 2 elements on the row, there's a gap in the middle. My goal is to have a row with 2 elements look like x x o. However, my current code displays the ...

Searching for values that do not begin with a specific set of characters using regular expressions

How can I create a regular expression for excluding words with a specific starting pattern? Consider the following list: January February June July October I need a regular expression that will match all words except "June" and "July" because they star ...