Build & style an Angular 15 example app with CSS Bootstrap 5

Build & style an Angular 15 example app with CSS Bootstrap 5

By building an example application step by step, we'll learn how to integrate and leverage bootstrap 5 with Angular 15.

We'll demonstrate how to setup and integrate an Angular 15 project with Bootstrap 5 CSS framework. Following that, we'll design a responsive layout with navbars, tables, forms, buttons, cards, and jumbotrons using the different Bootstrap 5 CSS utilities.

Bootstrap is a free and open-source CSS framework for developing flexible layouts; it is mobile-first and includes pre-built CSS utilities for typography, forms, buttons, and navigation, among other things.

Our angular tutorial is compatible with all current versions of Angular, up to and including Angular 15.

As seen in the last section, there are several approaches to integrate Angular with Bootstrap. Let's consider an example with a possible approache. This part will go as follows:

  • Step 1 — Installing Angular CLI 15
  • Step 2 — Initializing your Angular 15 Project
  • Step 3 — Installing Bootstrap 5
  • Step 5 — Creating Angular Components and Setting up Routing
  • Step 5 — Adding a Bootstrap 5 Jumbotron
  • Step 6 — Creating an Angular Bootstrap 5 Table
  • Step 7 — Adding a Bootstrap 5 Form Component

Step 1 — Installing Angular CLI 15

Let’s start by installing the latest version of Angular CLI. In your terminal, run the following command:

$ npm install -g @angular/cli

Step 2 — Initializing your Angular 15 Project

After installing Angular CLI, let’s initialize an Angular 15 project by running the following command:

$ ng new angular15bootstrapexample

The CLI will then ask you:

Would you like to add Angular routing?

Press Y.

Next, it will ask you:

Which stylesheet format would you like to use?

Choose “CSS”.

Next, we need to set up Angular 15 forms.

Go to the src/app/app.module.ts file, import FormsModule from @angular/forms, and include it in the imports array as follows:

import { BrowserModule } from '@angular/platform-browser';import { NgModule } from '@angular/core';import { AppRoutingModule } from './app-routing.module';import { FormsModule } from '@angular/forms';

@NgModule({  declarations: [  /* ... */  ],  imports: [    BrowserModule,    AppRoutingModule,    FormsModule  ],  providers: [],  bootstrap: [AppComponent]})export class AppModule { }

Step 3 — Installing Bootstrap 5

After initializing your Angular 15 project, let’s proceed to install Bootstrap 5 and integrate it with Angular.

Go to your project’s folder:

$ cd angular15bootstrapexample

Next, install Bootstrap 5 from npm using the following command:

$ npm install bootstrap

Next, go the angular.json file and add the paths of Bootstrap CSS and JavaScript files as well as jQuery to the styles and scripts arrays under the build target as follows:

"architect": {  "build": {    [...],     "styles": [      "src/styles.css",         "node_modules/bootstrap/dist/css/bootstrap.min.css"      ],      "scripts": [        ,       "node_modules/bootstrap/dist/js/bootstrap.min.js"      ]    },

Step 4 — Creating Angular Components and Setting up Routing

After installing and integrating Bootstrap 5 with your Angular 15 project, let’s create some components to test various Bootstrap styles.

Go to your command-line interface and run the following commands:

$ ng generate component jumbotron
$ ng generate component bootstrap-form
$ ng generate component bootstrap-table

Next, we need to include these components in the routing module to enable multiple views.

Go to the src/app/app-routing.module.ts file and update it as follows:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { BootstrapTableComponent } from './bootstrap-table/bootstrap-table.component';
import { BootstrapFormComponent } from './bootstrap-form/bootstrap-form.component';
import { JumbotronComponent } from './jumbotron/jumbotron.component';

const routes: Routes = [  
{path:  "", pathMatch:  "full",redirectTo:  "home"},  
{path: "jumbotron", component: JumbotronComponent},  
{path: "bootstrap-form", component: BootstrapFormComponent},  
{path: "bootstrap-table", component: BootstrapTableComponent}  ];

imports: [RouterModule.forRoot(routes)],  exports: [RouterModule]})
export class AppRoutingModule { }

Step 5 — Adding A Bootstrap 5 Jumbotron

A Bootstrap Jumbotron is a lightweight, flexible component that can optionally extend the entire viewport to showcase key marketing messages on your site

Let’s add a Bootstrap Jumbotron component to our jumbotron page.

Head to the src/app/jumbotron/jumbotron.component.html file and add the following HTML markup:

<div class="jumbotron" style="height: calc(95vh);">  
<h1>Angular 15 Bootstrap 5 Demo</h1>  
<p class="lead">    
This tutorial teaches you how to integrate Bootstrap 5 with Angular 15    

We use the built-in .jumbotron class to create a Bootstrap Jumbotron.

Step 6 — Creating an Angular 15 and Bootstrap 5 Table

Let’s now see how to use a Bootstrap 5 table to display tabular data.

Go the src/app/bootstrap-table/bootstrap-table.component.ts file and add some data that we can display:

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

@Component({  selector: 'app-bootstrap-table',  templateUrl: './bootstrap-table.component.html',  styleUrls: ['./bootstrap-table.component.css']})
export class BootstrapTableComponent implements OnInit {

  employees = [    {id: 1, name: "E 001", description: "E 001 des", email: "[email protected]"},    {id: 2, name: "E 002", description: "E 002 des", email: "[email protected]"},    {id: 3, name: "E 003", description: "E 003 des", email: "[email protected]"},    {id: 4, name: "E 004", description: "E 004 des", email: "[email protected]"}  ];  selectedEmployee;

  constructor() { }

  ngOnInit() {      }
  public createEmployee(e: {id, name, description, email}){    this.employees.push(e);  }
  public selectEmployee(e){    this.selectedEmployee = e;  }}

We simply defined two variables employees and selectedEmployee for holding the set of employees and the selected employee. And a selectEmployee() method which assigns the selected employee to the selectedEmployee variable.

Next, go to the src/app/bootstrap-table/bootstrap-table.component.html file and update it as follows:

<div class="container" style="margin-top: 70px;">  <table class="table table-hover">    <thead>      <tr>        <th>#</th>        <th>Name</th>        <th>Email</th>        <th>Actions</th>      </tr>    </thead>    <tbody>      <tr *ngFor="let employee of employees">

        <td></td>        <td> </td>        <td> </td>        <td>          <button class="btn btn-primary" (click)="selectEmployee(employee)"> Select</button>        </td>      </tr>    </tbody>  </table>  <div class="card text-center" *ngIf="selectedEmployee">      <div class="card-header">        #       </div>      <div class="card-block">        <h5 class="card-title"></h5>        <p class="card-text">
        </p>          </div>

A Bootstrap 5 Card is a flexible and extensible content container. It includes options for headers and footers, a wide variety of content, contextual background colors, and powerful display options. If you’re familiar with Bootstrap 3, cards replace our old panels, wells, and thumbnails. Similar functionality to those components is available as modifier classes for cards.

We use the built-in .table and .table-hover classes to create Bootstrap tables, the .card, .card-block, .card-title and .card-text classes to create cards.

Step 7 — Adding A Bootstrap 5 Form Component to your Angular 15 App

Let’s proceed by adding a Bootstrap-styled form to the bootstrap-form component.

Next, go to the src/app/bootstrap-form/bootstrap-form.component.ts file and update it as follows:

import { Component, OnInit } from '@angular/core';
@Component({  selector: 'bootstrap-form/-create',  templateUrl: './bootstrap-form/.component.html',  styleUrls: ['./bootstrap-form/.component.css']})export class BootstrapForm/Component implements OnInit {
  employee : {id, name, description, email} = {id: null, name: "", description: "", email: ""};

  constructor() { }
  ngOnInit() {  }

  createEmployee(){    console.log("Employee created: ", this.employee);    this.employee = {id: null, name: "", description: "", email: ""};

Next, go to the src/app/bootstrap-form/bootstrap-form.component.html file and update it as follows:

<div class="container" style="margin-top: 70px;">

  <div class="row">
    <div class="col-sm-8 offset-sm-2">

      <div>        <form>          <div class="form-group">            <label for="id">ID</label>            <input [(ngModel)]="" type="text" name="id" class="form-control" id="id" aria-describedby="idHelp" placeholder="Employee ID">            <small id="idHelp" class="form-text text-muted">Enter your employee’s ID</small>

            <label for="name">Employee Name</label>            <input [(ngModel)]="" type="text" name="name" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter your employee name">            <small id="nameHelp" class="form-text text-muted">Enter your employee’s name</small>

            <label for="email">Employee Email</label>            <input [(ngModel)]="" type="text" name="email" class="form-control" id="email" aria-describedby="emailHelp"              placeholder="Enter your employee email">            <small id="nameHelp" class="form-text text-muted">Enter your employee’s email</small>

            <label for="description">Employee Description</label>            <textarea [(ngModel)]="employee.description" name="description" class="form-control" id="description" aria-describedby="descHelp">                      </textarea>            <small id="descHelp" class="form-text text-muted">Enter your employee’s description</small>

          </div>        </form>        <button class="btn btn-primary" (click)="createEmployee()">Create employee</button>      </div>    </div>  </div></div>

We make use of the .form-group and .form-control classes to create a Bootstrap form.

Step 8 — Serving your Angular 15 Application

Head over to your command-line interface, and run the following command from the folder of your project:

$ ng serve

A development server will be started at the http://localhost:4200 address.


As a recap, we have seen how to initialize an Angular 15 project and integrate it with Bootstrap 5. Next, we used various Bootstrap CSS utilities to create a responsive layout with tables, forms, buttons, cards and jumbotrons.

  • Date: