Achieve a main-menu container with a full width dropdown using Bootstrap 4

I want the dropdown to start from the initial position for all menus and maintain a width of 600px.

Even if I attempt to open the dropdown for "Main Menu 2" or "Main Menu 3" or "Main Menu 4", the dropdown should always start from the position of "Main Menu 1". Here is a screenshot for better understanding. https://i.sstatic.net/Wp8QH.png

PS: This is in ng-Bootstrap 4 and contains some Angular-related codes. The following is the generated HTML.

<ul class="nav justify-content-end">
  <li class="nav-item">
    <div class="d-inline-block dropdown" ngbdropdown="">
      <button aria-haspopup="true" class="nav-link btn btn-link text-uppercase dropdown-toggle" id="dropdownBasic0" ngbdropdowntoggle="" aria-expanded="false">Main Menu 1</button>
      <div aria-labelledby="dropdownBasic0" ngbdropdownmenu="" class="dropdown-menu" x-placement="bottom-left">
        <div class="" ngbdropdownitem="">
          <div class="row">
            <div class="col-lg-4">
              <h2 _ngcontent-c4="">Submenu 1</h2>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu A</a>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu B</a>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu C</a>
            </div>
            <div class="col-lg-4">
              <h2 _ngcontent-c4="">Submenu 2</h2>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu D</a>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu E</a>
            </div>
            <div class="col-lg-4">
              <h2 _ngcontent-c4="">Submenu 3</h2>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu F</a>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu G</a>
              <a routerlink="/" ng-reflect-router-link="/" href="/">Menu H</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </li>

  ... (Remaining HTML code not repeated for brevity)

Below is the Angular Code:

<ul class="nav justify-content-end">
  <li class="nav-item">
    <div ngbDropdown class="d-inline-block">
      <button class="nav-link" id="dropdownBasic0"
              ngbDropdownToggle>Main Menu 1</button>
      <div ngbDropdownMenu aria-labelledby="dropdownBasic0">
        <div class="" ngbDropdownItem>
          <div class="row">
            <div class="col-lg-4">
              <a routerLink="/">Sub Menu 1</a>
              <a routerLink="/">Sub Menu 2</a>
              <a routerLink="/">Sub Menu 3</a>
            </div>
            <div class="col-lg-4">
              <a routerLink="/">Sub Menu 1</a>
              <a routerLink="/">Sub Menu 2</a>
              <a routerLink="/">Sub Menu 3</a>
            </div>
            <div class="col-lg-4">
              <a routerLink="/">Sub Menu 1</a>
              <a routerLink="/">Sub Menu 2</a>
              <a routerLink="/">Sub Menu 3</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </li>

  ... (Remaining Angular code not repeated for brevity)

Answer №1

Considering the use of ngbDropdown, I decided to tackle the question in an Angular context;

  • Instead of assigning a fixed width of 600px, I utilized BS4 classes to create a responsive solution where 50% of the width is reserved for the logo, and the other 50% for navigation buttons.
  • Since we needed to reposition the dropdown menu away from its default location (now noted in SB) just below the toggle button, we handled the toggle effect ourselves;
  • We also applied some styling to achieve the desired effect.
  • You can view a functional demo here

Relevant HTML code:

<div class="row">
    <div class="col-6">
        LOGO AREA
    </div>

    <div class="col-3 ">
        <div ngbDropdown class="fullWidth">
            <button class="btn btn-outline-primary" id="dropdownBasic1" ngbDropdownToggle (click)="toggleShow1()">dropdown1</button>
    </div>
  </div>

  <div class="col-3 ">
    <div ngbDropdown  class="fullWidth">
      <button class="btn btn-outline-primary" id="dropdownBasic2" ngbDropdownToggle (click)="toggleShow2()">dropdown2</button>
    </div>
  </div>
  </div>
  <div class='row'>
    <div class='col-6'>
    </div>
    <div class='col-6'>
      <div class='dropdown-menu show' *ngIf="show1" >
        <button ngbDropdownItem>Action - 1</button>
        <button ngbDropdownItem>Another Action</button>
        <button ngbDropdownItem>Something else is here</button>
      </div>
      <div class='dropdown-menu show' *ngIf="show2">
        <button ngbDropdownItem>Action - 2</button>
        <button ngbDropdownItem>Another Action</button>
        <button ngbDropdownItem>Something else is here</button>
      </div>
    </div>
</div>

Relevant TS code:

import { Component } from '@angular/core';

@Component({
  selector: 'ngbd-dropdown-basic',
  templateUrl: './dropdown-basic.html',
  styles: [`
  .fullWidth .btn {width:100%;}
  .dropdown-menu.show{ position: initial; width: inherit;}
  `]
})
export class NgbdDropdownBasic {
  show1: boolean = false;
  show2: boolean = false;

  toggleShow1() {
    if (this.show1) { this.show1 = false; }
    else {
      this.show1 = true;
      (this.show2) ? this.show2 = false : '';
    }
  }

  toggleShow2() {
    if (this.show2) { this.show2 = false }
    else {
      this.show2 = true;
      (this.show1) ? this.show1 = false : '';
    }
  }

}

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 Wordpress? Customize your sidebar with three widgets and don't forget to add the "last" class to the third one for a polished look

Successfully set up a custom sidebar on my WordPress website and configured it to display three widgets. The CSS for the sidebar includes a specific styling for each widget, with an additional class to remove margin-right from the last (third) widget. reg ...

Troubleshooting Problem with Updating Bootstrap Datepicker in AngularJS

Having trouble updating the date field in the database from my Angular page. Unable to bind the modified date to the database, as the item.ReimbDate still holds the old value. Currently using a bootstrap datepicker for this process. <input type="text" ...

Interactive JavaScript Slider for Customizing Selection

Recently, I came across a range slider and I am trying to make it more dynamic. The current JavaScript code has hardcoded references to specific HTML elements, and I want to have multiple sliders on my HTML page, each functioning independently. The code sn ...

Display a div above the Kendo for Angular Dialog

Looking at this StackBlitz example, there is a Kendo for Angular Dialog that contains a div with the CSS property position:absolute. The goal is to display this div on top of the dialog itself, rather than inside it, while ensuring that the dialog does n ...

Is it possible to utilize Bootstrap Icons as bullet points on a website?

Is it possible to use Bootstrap icons for bullet points with CSS? I am aware that FontAwesome can be used for this purpose, but I would like to confirm if the same can be achieved using Bootstrap. Though there is a method using: <ul> <li>< ...

Interactive feature allowing all embedded YouTube videos on a webpage to play synchronously, with sound disabled, and on a continuous loop

I am in the process of developing a button that, when clicked by a user, will trigger all embedded YouTube videos on a specific webpage to start playing simultaneously, with sound muted, and set to loop. The target page for this button implementation can ...

Using JQUERY to create a smooth sliding transition as images change

Currently, I have set up three images (1.jpg, 2.jpg, 3.jpg) along with an image using the code: <img id="add" src="1.jpg"></img>. The width, height, and position of this additional image are adjusted as needed and are functioning correctly. Fur ...

Tips for switching out images depending on the time of day

Currently, I have a script that dynamically changes the background color of my webpage based on the time of day. However, I am facing issues trying to implement a similar functionality for replacing an image source. The current code is also time zone-based ...

Total aggregate for a variety of Sliders (Bootstrap 4 & jQuery)

I am currently working on a web page that involves implementing 3 sliders. The total of all three sliders should always be limited to 100%. This project utilizes the Bootstrap 4 framework and jQuery 3.6.2 Here are my current challenges: The combined valu ...

Can you tell me the standard font size in Internet Explorer 9?

After examining this particular website, I found the default styling used by IE 9. The body selector in particular appears as follows: body { display: block; margin: 8px; zoom: 1; } I would have expected a font-size declaration in this code, but su ...

Is there a hashing algorithm that produces identical results in both Dart and TypeScript?

I am looking to create a unique identifier for my chat application. (Chat between my Flutter app and Angular web) Below is the code snippet written in Dart... String peerId = widget.peerid; //string ID value String currentUserId = widget.currentId ...

Is there a way to make the parent elements invisible when there are no child elements present?

Take this scenario for instance: <ul *ngIf="hasAnyPermission(['A:READ', 'B:READ', ...])"> <li *ngIf="hayPermission('A:READ')"> a </li> <li *ngIf="hasPermission('B:RE ...

items within the grid container are positioned without being constrained to rows and columns

After using the Container element, I noticed that all my grid items are displaying in columns instead of rows. How can I correct this layout issue? https://i.stack.imgur.com/9BjOA.png Click here to access the project link. ...

How to make the Bootstrap mobile navigation menu taller?

I'm having trouble adjusting the height of the collapsed mobile drop-down navigation. Right now, it's stuck at around 340px and I need it to expand to accommodate all the menu items within the collapsed nav. Despite my efforts searching on Google ...

Creating an organized layout with multiple bootstrap toasts in a grid formation

Is there a way to create multiple Bootstrap Toast elements side-by-side and top-to-top? For more information, visit: https://getbootstrap.com/docs/4.2/components/toasts/ <div aria-live="polite" aria-atomic="true" style="position: relative; min-height: ...

Checking Email in Angular 2 for Accuracy

Wondering about creating a directive for email validation in Angular? import { Directive } from '@angular/core'; @Directive({ selector: '[appEmailValidator]' }) export class EmailValidatorDirective { constructor() {} } I have ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

Inherit margins from child elements with fixed positions

Below is a very basic code example to consider: <div id="parent"> <div id="child"> Test it </div> </div> This code snippet includes the following CSS styles: #parent{ position:relative; margin-left:200px; color:#FFF; } #chi ...

The appearance of random instances of the letter "L" within text on Internet Explorer 8

Help needed! I'm encountering an issue where mysterious "L" characters are appearing in IE 8. The problem is specifically occurring in the Healthcare Professionals section and the bottom two blocks of the page. Has anyone else experienced this or have ...

Enhancing Twilio LocalVideoTrack with custom start and stop event handling

Is it possible to customize the events triggered when a video starts or stops playing? I attempted the code below, but unfortunately, it did not yield any results: this.videoTrack = screenTrack as LocalVideoTrack; this.videoTrack.stopped = function (eve ...