Using Bootstrap In Angular Application
There is no doubt that using a design library making the web page design easier for most of the web developers especially those does not want to busy themselves with the style sheet codes and the alignment … etc.
One of the libraries that is very popular and easy to use is the bootstrap. Using bootstrap is very easy to use and implement as it has a ready-made CSS classes for almost every usage in the web page.
I this article we will go through a step-by-step tutorial on how to implement and use bootstrap library with Angular 2+. The time this article was written, the latest version of Angular was Angular 14.
In order to complete this tutorial you need to have the following installed.
1. Nodejs
2. TypeScript
3. Angular CLI
And a running Angular Application.
In case you are not familiar with any of the previous three; please check this post. If not continue with the following
Step1: Install Bootstrap Package.
Just like the way we installed angular CLI we can install Bootstrap. Run the following command in a terminal window:
npm i boostrap
(npm) => stands for node package manager(i) => stands for install (it is possible to write the complete word “install”).
(bootstrap) => the name of the package to be installed.
After the installation is done we now need to use the bootstrap library in our application to do that we need to do some easy operations:
1- Open the angular.json file located in the root of the application folder (learnangularbootstrap) in our case. This is a JSON file. Locate the “styles” array and paste the following path in the array:
"./node_modules/bootstrap/dist/css/bootstrap.min.css"
2- Open the style.css file located in this path (/learnangularbootstrap/src). This is a CSS file. All the classes in this file are available across the application files. Add the following line to the file
@import "~bootstrap/dist/css/bootstrap.min.css";
That is it, now we can use the library with all of its classes in the HTML files.
Step2: Using Bootstrap library in HTML Files.
Here I will make a small sample using some Bootstrap CSS classes. Let us take for example this sample HTML:
|
<div> <!-- Container --> <div> This is the header </div> <div> <div> <div> <input type="text" name="input1" value="" /> </div> <div> <input type="text" name="input2" value="" /> </div> <div> <input type="text" name="input3" value="" /> </div> </div> </div> </div> |
When displayed in the web page it displays like this
Now, let us use the bootstrap classes to the DIVS and the input controls
|
<div class="card"> <!-- Conainer --> <div class="card-header"> This is the header </div> <div class="card-body"> <div class="form-group row col-md-12"> <div class="col-md-4"> <input class="form-control" type="text" name="input1" value="" /> </div> <div class="col-md-4"> <input class="form-control" type="text" name="input2" value="" /> </div> <div class="col-md-4"> <input class="form-control" type="text" name="input3" value="" /> </div> </div> </div> </div> |
Now, it looks like this in the browser:
Cool, right!

Comments
Post a Comment