The hamburger menu remains static and unresponsive on mobile screens, failing to expand as intended

I am currently facing an issue with my Bootstrap navbar in Google Chrome. When I reduce the size of the browser, the navbar menus collapse into a hamburger icon but clicking on it does not reveal the menus.

The problem seems to be occurring in my app.component.html file:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/popper.js/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
    <link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@2/css/bootstrap4-toggle.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@2/js/bootstrap4-toggle.min.js"></script>

  </head>
  <body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <div class="container">
        <a class="navbar-brand" href="#">eNno</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav ml-auto">
            <li class="nav-item active">
              <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Service</a>
            </li>

            <li class="nav-item">
              <a class="nav-link" href="#">Blog</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  </body>
</html>

Additionally, here are some relevant files from my project:

package.json:

{
  // package.json contents here...
}

tsconfig.json:

{
  // tsconfig.json contents here...
}

angular.json:

{
  // angular.json contents here...
}

index.html:

{
  // index.html contents here...
}

app.module.ts:

{
  // app.module.ts contents here...
}

Project Information - Angular CLI: 10.0.7, Node: 12.18.1, Angular: 10.0.11

An example screenshot of the issue can be found here.

If anyone has encountered this issue before or has suggestions on how to resolve it, please let me know. Thank you!

Answer №1

Upon reviewing your project, I have identified several issues that need to be addressed one by one.

  1. You included bootstrap in your package.json and installed it using either npm i or yarn. This resulted in its presence in the node_modules/bootstrap folder along with requirements like jquery and popper. While you correctly imported it in angular.json, there are two key problems:
  • (a) Placement should be in the "build" section instead of the current placement under "test"
  • (b) Several links are broken and inaccessible (refer to the node_modules/boostrap/... directory for clarification).

Below is the corrected version of your angular.json:

{
  // Angular JSON schema
  "version": 1,
  // Project files location
  "newProjectRoot": "projects",
  // Project configuration details
  "projects": {
    "UIAssignment": {
      // Application type
      "projectType": "application",
      // Architectural structure
      "architect": {
        // Build properties
        "build": {
          ...
          "styles": [
            "./node_modules/bootstrap/dist/css/bootstrap.css",
            "src/styles.css"
          ],
          "scripts": [
            "./node_modules/jquery/dist/jquery.js",
            "./node_modules/popper.js/dist/umd/popper.js",
            "./node_modules/bootstrap/dist/js/bootstrap.js"
          ]
          ...
        },
        ...
      }
    }
  },
  // Default project setting
  "defaultProject": "UIAssignment",
  // Analytics tracking ID
  "cli": {
    "analytics": "01c58ec0-920b-4102-a361-7d46f52578c7"
  }
}
  1. Incorrectly, you added a

    <!doctype html><html lang="en"> ...
    segment within app.component.html. Remember that this file is loaded into index.html through
    <app-root></app-root>
    . Therefore, refrain from injecting such elements here. Keep app.component.html pure with only content like <nav>...</nav>.

  2. As Bootstrap was properly defined in your package.json and integrated via angular.json, avoid redundant loading through <script> or <link>.

The fixed app.component.html now displays the correct navigation bar layout:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <div class="container">
    <a class="navbar-brand" href="#">eNno</a>
    ...
  </div>
</nav>

Additionally, the revised index.html structure ensures proper initialization:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>UIAssignment</title>
    ...
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>

Remember to restart the Angular CLI after modifying angular.json. Additionally, for proper integration of Bootstrap and jQuery in an Angular project, consider utilizing ngx-bootstrap as a guide.

If any issues persist, consider removing the extra styles and scripts entries from angular.json, and insert these lines within the <head> section of your index.html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
...

Best of luck with your project!

Answer №2

To enhance the performance of your app, utilize CDN links for Bootstrap and include the CSS links twice while using a different version of the Bootstrap JS link.

If you are utilizing the Bootstrap package,

Add the following code to the angular.json file:

 "styles": [
      "./node_modules/bootstrap/dist/css/bootstrap.min.css",
      "src/styles.css"
    ],
"scripts": [
      "./node_modules/jquery/dist/jquery.min.js",
      "./node_modules/popper.js/dist/popper.min.js",
      "./node_modules/bootstrap/dist/js/bootstrap.min.js"
    ]

Don't forget to restart ng serve if it's already running so that Bootstrap can be implemented in your application successfully.

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

Is it possible to exclusively focus on the specified data attributes?

Is there a method to specifically target the 'data-season' attribute with the value "winter"? This is my HTML: <li data-season="summer">summer <ul class="groups"> <li data-item="beach">beach</li> <li data-item ...

Innovative Functions of HTML5 LocalStorage for JavaScript and TypeScript Operations

Step-by-Step Guide: Determine if your browser supports the use of localStorage Check if localStorage has any stored items Find out how much space is available in your localStorage Get the maximum storage capacity of localStorage View the amount of space ...

What may be causing the MuiThemeProvider to override the style of a component?

Within my outer component, I am utilizing MuiThemeProvider: <MuiThemeProvider theme={full_theme_e}> <div> <AppBar /> <Filter /> </div> </MuiThemeProvider> Inside the Filter component, I have specified a ...

Is it possible to disable the save option on a PDF popup window?

When displaying a PDF using an embed tag, I have managed to disable print and content copying through document properties. However, I am struggling to find a solution to disable the save option that pops up. Is there a way to achieve this using JavaScrip ...

Is there a way to display a foundation.css drop-down menu using jQuery?

After attempting to create a navigation bar using foundation.css, I encountered an issue where the sub-menu does not appear when hovering over it with the mouse. The main question at hand is how to display the sub-menu of test on this specific webpage. D ...

Stuck on changing the background color of a DIV in WooCommerce product descriptions with CSS

I seem to be stuck on a simple fix and despite searching everywhere, I can't seem to make it work. The problem lies within WooCommerce on my WordPress site, where I suspect that my CSS is conflicting with WC's and causing some color problems. Yo ...

Retrieve information from a JSON document

My project involves a bookStore with a list of books that I am working on. I am trying to extract all the book data from a JSON file and display them in a card view on a webpage. Although my code is error-free and seems to be functioning properly, the da ...

Is it possible to meta-refresh a page for redirection?

When creating a webpage, I included a META tag like this: <META http-equiv="refresh" content="5;URL=http://www.google.com"> The issue is that mobile browsers do not support this meta tag. It redirects properly on web browsers, but not on mobile dev ...

Challenge encountered when trying to add overlay hover effect on image cards

Yesterday I faced a challenge with a tracking problem while trying to implement code on a w3schools webpage. The issue was with a hover effect triggering an overlay when the mouse was placed anywhere on the line, rather than just within the card area. I&ap ...

Manipulating the .innerHTML property allows for selectively replacing sections

In my JavaScript code, I am trying to display a video along with a countdown timer. Once the countdown finishes, it should switch the content of the div to show a question. Below is my current implementation: <script type="text/javascript"> ...

Ensure the configuration is stored in a safe location where files cannot be accessed through external links

Currently, I am attempting to utilize a configuration file for both a database and rating script. However, the dilemma lies in the fact that the config file is located in this directory: website.com/include/config.php also known as websitename/include/co ...

Troubleshooting Bootstrap 3.0: Issues with nav-tabs not toggling

I have set up my navigation tabs using Bootstrap 3 in the following way: <ul class="nav nav-tabs pull-right projects" role="tablist" style="margin-top:20px;"> <li class="active"><a role="tab" data-toggle="tab" href="#progress">In Pr ...

Rotating arrows enhance the functionality of the accordion menu

I have successfully implemented a basic accordion with rotating arrows on click. Everything is working smoothly except for one issue: When I collapse one item and then try to collapse another, the previous arrow does not return to its default state. Is ...

Issue with CSS transition not displaying ng-show elements properly

I'm currently working on incorporating a fade-in effect for text using the second solution from this source when using ng-show. Here is my HTML setup: <input ng-focus="hasFocus = true" ng-blur="hasFocus = false" type="text" placeholder="Cli ...

Incorporating personalized CSS into Drupal 7 for concealing the notice

Utilizing my custom block to showcase a flash game on the main page of my Drupal 7 setup, I encountered an irksome message: <div id="first-time"><p>No front page content has been created yet.</p> <div class="item-list"><ul>&l ...

Ensure that the remaining space on the page is filled by utilizing 2 divs

I am currently in the process of developing a website that needs to be responsive on all pages. The layout consists of 5 divs positioned horizontally. The three middle divs have fixed sizes - 200px, 400px, and 200px respectively. However, I am facing a cha ...

The variable $ has not been defined in Jquery framework

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="deployJava.js"></script> <script type="text/javascr ...

Original code

I have a database containing HTML code for a table. I would like to display this code in its original form within a textarea so that I can edit it. However, when I try to echo the code, it appears in compiled form as a table rather than the original HTML ...

Bug with the animation of height in Jquery

Unfortunately, I can't share my entire source code here because it's quite lengthy. However, maybe someone can provide some insight into a common issue that may be happening elsewhere. The problem I'm facing is with a DIV element that has r ...

Tips for seamlessly integrating a Shopify App into a personalized HTML Call to Action

I'm in the process of putting together a Shopify online store and I've been able to put together a customized popup using HTML, CSS, and JS. I've also incorporated an app in Shopify for email marketing purposes. However, I'm looking to ...