How to install bootstrap in angular?

How to install bootstrap in angular?

In this article I will show you how to install bootstrap in Angular.

I also recorded a Video version of this Article. So you can watch my video from youtube to see more details:

I assume that you already have angular project installed.

In your project root directory run following command:

npm install bootstrap jquery --save
1npm install bootstrap jquery --save

Some of features of bootstrap depends on the jQuery.

image-10 (2).webp

With above command, it will install bootstrap and jquery in the node_modules folder

image-11 (1).webp

If you scroll down further, then you will also see jquery installed:

image-13 (1).webp

Also it will save the package name in the package.json file

image-12 (1).webp

After installing bootstrap we need to configure it to use in our angular project. For that open the angular.json file and add the path of bootstrap.min.css, jquery.min.js and bootstrap.min.js files as shown in screenshots below:

image-16.webp

This is how my entire angular.json file looks like now:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "bootstrapProject": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/bootstrapProject",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": [
              "src/styles.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "bootstrapProject:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "bootstrapProject:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "bootstrapProject:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": ["src/favicon.ico", "src/assets"],
            "styles": ["src/styles.scss"],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": ["**/node_modules/**"]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "bootstrapProject:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "bootstrapProject:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "bootstrapProject",
  "cli": {
    "analytics": "3c267790-6b84-4e7e-91a8-967937a6c248"
  }
}
1{ 2 "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 "version": 1, 4 "newProjectRoot": "projects", 5 "projects": { 6 "bootstrapProject": { 7 "projectType": "application", 8 "schematics": { 9 "@schematics/angular:component": { 10 "style": "scss" 11 } 12 }, 13 "root": "", 14 "sourceRoot": "src", 15 "prefix": "app", 16 "architect": { 17 "build": { 18 "builder": "@angular-devkit/build-angular:browser", 19 "options": { 20 "outputPath": "dist/bootstrapProject", 21 "index": "src/index.html", 22 "main": "src/main.ts", 23 "polyfills": "src/polyfills.ts", 24 "tsConfig": "tsconfig.app.json", 25 "aot": true, 26 "assets": ["src/favicon.ico", "src/assets"], 27 "styles": [ 28 "src/styles.scss", 29 "node_modules/bootstrap/dist/css/bootstrap.min.css" 30 ], 31 "scripts": [ 32 "node_modules/jquery/dist/jquery.min.js", 33 "node_modules/bootstrap/dist/js/bootstrap.min.js" 34 ] 35 }, 36 "configurations": { 37 "production": { 38 "fileReplacements": [ 39 { 40 "replace": "src/environments/environment.ts", 41 "with": "src/environments/environment.prod.ts" 42 } 43 ], 44 "optimization": true, 45 "outputHashing": "all", 46 "sourceMap": false, 47 "extractCss": true, 48 "namedChunks": false, 49 "extractLicenses": true, 50 "vendorChunk": false, 51 "buildOptimizer": true, 52 "budgets": [ 53 { 54 "type": "initial", 55 "maximumWarning": "2mb", 56 "maximumError": "5mb" 57 }, 58 { 59 "type": "anyComponentStyle", 60 "maximumWarning": "6kb", 61 "maximumError": "10kb" 62 } 63 ] 64 } 65 } 66 }, 67 "serve": { 68 "builder": "@angular-devkit/build-angular:dev-server", 69 "options": { 70 "browserTarget": "bootstrapProject:build" 71 }, 72 "configurations": { 73 "production": { 74 "browserTarget": "bootstrapProject:build:production" 75 } 76 } 77 }, 78 "extract-i18n": { 79 "builder": "@angular-devkit/build-angular:extract-i18n", 80 "options": { 81 "browserTarget": "bootstrapProject:build" 82 } 83 }, 84 "test": { 85 "builder": "@angular-devkit/build-angular:karma", 86 "options": { 87 "main": "src/test.ts", 88 "polyfills": "src/polyfills.ts", 89 "tsConfig": "tsconfig.spec.json", 90 "karmaConfig": "karma.conf.js", 91 "assets": ["src/favicon.ico", "src/assets"], 92 "styles": ["src/styles.scss"], 93 "scripts": [] 94 } 95 }, 96 "lint": { 97 "builder": "@angular-devkit/build-angular:tslint", 98 "options": { 99 "tsConfig": [ 100 "tsconfig.app.json", 101 "tsconfig.spec.json", 102 "e2e/tsconfig.json" 103 ], 104 "exclude": ["**/node_modules/**"] 105 } 106 }, 107 "e2e": { 108 "builder": "@angular-devkit/build-angular:protractor", 109 "options": { 110 "protractorConfig": "e2e/protractor.conf.js", 111 "devServerTarget": "bootstrapProject:serve" 112 }, 113 "configurations": { 114 "production": { 115 "devServerTarget": "bootstrapProject:serve:production" 116 } 117 } 118 } 119 } 120 } 121 }, 122 "defaultProject": "bootstrapProject", 123 "cli": { 124 "analytics": "3c267790-6b84-4e7e-91a8-967937a6c248" 125 } 126}

After that, if your server is already running then stop it. And run the serve command to start the server again with the serve command:

ng s -o
1ng s -o

Remove all code from the app.component.ts file and add the following bootstrap related code to see if bootstrap is working fine:

<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingOne">
      <button
        class="accordion-button"
        type="button"
        data-bs-toggle="collapse"
        data-bs-target="#collapseOne"
        aria-expanded="true"
        aria-controls="collapseOne"
      >
        Accordion Item #1
      </button>
    </h2>
    <div
      id="collapseOne"
      class="accordion-collapse collapse show"
      aria-labelledby="headingOne"
      data-bs-parent="#accordionExample"
    >
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by
        default, until the collapse plugin adds the appropriate classes that we
        use to style each element. These classes control the overall appearance,
        as well as the showing and hiding via CSS transitions. You can modify
        any of this with custom CSS or overriding our default variables. It's
        also worth noting that just about any HTML can go within the
        <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingTwo">
      <button
        class="accordion-button collapsed"
        type="button"
        data-bs-toggle="collapse"
        data-bs-target="#collapseTwo"
        aria-expanded="false"
        aria-controls="collapseTwo"
      >
        Accordion Item #2
      </button>
    </h2>
    <div
      id="collapseTwo"
      class="accordion-collapse collapse"
      aria-labelledby="headingTwo"
      data-bs-parent="#accordionExample"
    >
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden
        by default, until the collapse plugin adds the appropriate classes that
        we use to style each element. These classes control the overall
        appearance, as well as the showing and hiding via CSS transitions. You
        can modify any of this with custom CSS or overriding our default
        variables. It's also worth noting that just about any HTML can go within
        the <code>.accordion-body</code>, though the transition does limit
        overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header" id="headingThree">
      <button
        class="accordion-button collapsed"
        type="button"
        data-bs-toggle="collapse"
        data-bs-target="#collapseThree"
        aria-expanded="false"
        aria-controls="collapseThree"
      >
        Accordion Item #3
      </button>
    </h2>
    <div
      id="collapseThree"
      class="accordion-collapse collapse"
      aria-labelledby="headingThree"
      data-bs-parent="#accordionExample"
    >
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden
        by default, until the collapse plugin adds the appropriate classes that
        we use to style each element. These classes control the overall
        appearance, as well as the showing and hiding via CSS transitions. You
        can modify any of this with custom CSS or overriding our default
        variables. It's also worth noting that just about any HTML can go within
        the <code>.accordion-body</code>, though the transition does limit
        overflow.
      </div>
    </div>
  </div>
</div>
1<div class="accordion" id="accordionExample"> 2 <div class="accordion-item"> 3 <h2 class="accordion-header" id="headingOne"> 4 <button 5 class="accordion-button" 6 type="button" 7 data-bs-toggle="collapse" 8 data-bs-target="#collapseOne" 9 aria-expanded="true" 10 aria-controls="collapseOne" 11 > 12 Accordion Item #1 13 </button> 14 </h2> 15 <div 16 id="collapseOne" 17 class="accordion-collapse collapse show" 18 aria-labelledby="headingOne" 19 data-bs-parent="#accordionExample" 20 > 21 <div class="accordion-body"> 22 <strong>This is the first item's accordion body.</strong> It is shown by 23 default, until the collapse plugin adds the appropriate classes that we 24 use to style each element. These classes control the overall appearance, 25 as well as the showing and hiding via CSS transitions. You can modify 26 any of this with custom CSS or overriding our default variables. It's 27 also worth noting that just about any HTML can go within the 28 <code>.accordion-body</code>, though the transition does limit overflow. 29 </div> 30 </div> 31 </div> 32 <div class="accordion-item"> 33 <h2 class="accordion-header" id="headingTwo"> 34 <button 35 class="accordion-button collapsed" 36 type="button" 37 data-bs-toggle="collapse" 38 data-bs-target="#collapseTwo" 39 aria-expanded="false" 40 aria-controls="collapseTwo" 41 > 42 Accordion Item #2 43 </button> 44 </h2> 45 <div 46 id="collapseTwo" 47 class="accordion-collapse collapse" 48 aria-labelledby="headingTwo" 49 data-bs-parent="#accordionExample" 50 > 51 <div class="accordion-body"> 52 <strong>This is the second item's accordion body.</strong> It is hidden 53 by default, until the collapse plugin adds the appropriate classes that 54 we use to style each element. These classes control the overall 55 appearance, as well as the showing and hiding via CSS transitions. You 56 can modify any of this with custom CSS or overriding our default 57 variables. It's also worth noting that just about any HTML can go within 58 the <code>.accordion-body</code>, though the transition does limit 59 overflow. 60 </div> 61 </div> 62 </div> 63 <div class="accordion-item"> 64 <h2 class="accordion-header" id="headingThree"> 65 <button 66 class="accordion-button collapsed" 67 type="button" 68 data-bs-toggle="collapse" 69 data-bs-target="#collapseThree" 70 aria-expanded="false" 71 aria-controls="collapseThree" 72 > 73 Accordion Item #3 74 </button> 75 </h2> 76 <div 77 id="collapseThree" 78 class="accordion-collapse collapse" 79 aria-labelledby="headingThree" 80 data-bs-parent="#accordionExample" 81 > 82 <div class="accordion-body"> 83 <strong>This is the third item's accordion body.</strong> It is hidden 84 by default, until the collapse plugin adds the appropriate classes that 85 we use to style each element. These classes control the overall 86 appearance, as well as the showing and hiding via CSS transitions. You 87 can modify any of this with custom CSS or overriding our default 88 variables. It's also worth noting that just about any HTML can go within 89 the <code>.accordion-body</code>, though the transition does limit 90 overflow. 91 </div> 92 </div> 93 </div> 94</div>

Now check app in the browser. You should see something like this:

image-17.webp

Now you can see that bootstrap styles and animations are working perfectly fine.

If you have any questions or you also want to share any suggestion then please leave your comments below. Also, share this article to help others.

I also have a Youtube channel where I keep recording a lot of useful content for you. So don’t forget to Subscribe to my channel and click on the bell icon to see all notifications.

Subscribe to my Youtube Channel AyyazTech