Start an app with Angular 12 and Bootstrap 4

Before you start, there's also a guide for bootstrap 5 here.

Angular and Bootstrap are a great combination for building good-looking, interactive web-apps. This guide describes how I go about starting a new project:

1. Create an angular project

This step is simple, just run:

ng new ng12b4-example

Replace ng12b4-example with the name of your project.

The Angular CLI will ask which stylesheet format you would like to use - select SASS, that way we can overwrite bootstrap variables (see step 4). You could also use SCSS if you're into that.

Finally,

cd ng12b4-example

2. Install bootstrap

For bootstrap version 4, run

npm install bootstrap@4.6.0

3. Include bootstrap styles

Add the following to src/styles.sass:

@import '~bootstrap/scss/bootstrap'

This will add the bootstrap styles to our app.

4. (Optional) customize bootstrap variables

We can override bootstrap variables to make our app more unique.

For example, default bootstrap uses rounded corners. If you're more into sharp edges, simply set the corresponding variable in styles.sass:

// Variable overrides must come before the import
$border-radius: 0
@import '~bootstrap/scss/bootstrap'

5. (Optional) install a library for interactive elements

If you only need the bootstrap styles, you can stop reading here. But if you want interactive elements (modals, alerts, etc.), you'll need one of two libraries: ngx-bootstrap by Valor software, or the community project ng-bootstrap. Both are released under the MIT License and, in my experience, both work well and have good documentation. I'll be using ng-bootstrap here:

ng add @ng-bootstrap/ng-bootstrap

Note that this command might change your styles.sass by importing bootstrap again, which is well-meaning but redundant in this case. If that happens, just remove the second import.

You'll also notice that app.module.ts has been changed and is now importing the NgbModule. Great!

Consult the ng-bootstrap docs to find out how to use your new library.


That's it! You can find the example code on my github.

Remember that there's a separate guide for bootstrap 5 here.

You might also be interested in how to add a custom theme switcher to your app.