Best Way to Add Bootstrap 4 to Project
Last Updated :
17 May, 2023
New developer generally face problems while implementing or adding bootstrap to their projects. The problem mainly occurs to implement the bootstrap all over the project (that is on all the required files) without adding the stylesheet and script on every page of the project.
In this article, we have provided a simplified way to the same.
Example without using Bootstrap:
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.green {
color: green;
}
.yellow {
color: yellow;
}
.red {
color: red;
}
.blue {
color: blue;
}
.grey {
color: grey;
}
</style>
</head>
<body>
<h1>Geeksforgeeks</h1>
<p class="green">This text is in green color</p>
<p class="yellow">This text is in yellow color</p>
<p class="red">This text is in red color</p>
<p class="blue">This text is in blue color</p>
<p class="grey">This tag is in grey color</p>
</body>
</html>
Output:
Without using Bootstrap
Adding Bootstrap to our project: We can change the font color and styles using Bootstrap. The general way to do this is to add stylesheets and script in our head tab of the code as shown.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">
</script>
<script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">
</script>
</head>
<body>
<h1>GeeksforGeeks</h1>
<p class="text-primary">This text is in blue color.</p>
<p class="text-success">This text is in green color.</p>
<p class="text-warning"> This text is in yellow color.</p>
<p class="text-danger">This text is in red color.</p>
<p class="text-secondary">This text is in grey color.</p>
</body>
</html>
Output:
Bootstrap applied
You can note that the font of the same program is changed, not only font the spacing, placement size is also changed that means the Bootstrap 4 CDN has been applied.
Another way of adding Bootstrap to our project: In the above example, we saw that we need to add so many additional lines of code on every file of our project to apply bootstrap on that specific file. But this becomes exhausting when we have too many files to manage in our project.
Problem: If our project contains 30 files out of which we have to apply bootstrap on 18 files, then the above mentioned traditional method becomes too much load for the developer.
Solution: The same thing said above can be achieved just by writing one line of code in our file and setting the stylesheets and the scripts into some other file.
Explanation:
Step 1: Create a folder such as “components” in your ‘Web Pages’ folder of your project and also create a JSP file of your desired name such as “css-js.jsp” in components folder and clear all the prewritten code in the css-js.jsp file as shown.
All the prewritten code is removed
Step 2:
<!-- For CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" crossorigin="anonymous">
<!-- For JavaScript -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" crossorigin="anonymous"><>/script>
Copy the above quoted code into the “css-js.jsp” file. (The code provided are the stylesheets for the application of CSS and <script> tag for JavaScript )
Save the file
Stylesheets and script is copied to css-js file
Step 3: To apply the bootstrap on any page of your project you just need to include the “css-js.jsp” file in your head tag.
For example: <@include file=” components/css-js.jsp ”/>
HTML
<!DOCTYPE html>
<html>
<head>
<!-- This line will copy the entire code placed
in the mentioned file(css-js.jsp) to here
(file in which bootstrap has to be applied) -->
<@include file="components/css-js.jsp" />
</head>
<body>
<h1>GeeksforGeeks</h1>
<p class="text-primary">This text is in blue color.</p>
<p class="text-success">This text is in green color.</p>
<p class="text-warning"> This text is in yellow color.</p>
<p class="text-danger">This text is in red color.</p>
<p class="text-secondary">This text is in grey color.</p>
</body>
</html>
The "include" line will add the code written in the css-js.jsp file to the file in which it is written (You can understand this as - the whole code is given a name, and now we are using that name everywhere we require instead of putting the whole code.)
That is instead of writing the whole quoted code (stylesheets and scripts) in every file you need to just write one line of code( that is include the file where the stylesheet and script is kept "components/css-js.jsp" in this example).
Output:
Bootstrap applied
You can apply bootstrap on any file just by including the css-js.jsp file than to write the whole stylesheet on every page/file.
Additional Step 4: You need to follow this step only if you want to make your custom styles and write JavaScript code.
Step 4.1 Create two files with the Cascading Style Sheet (.css) and JavaScript File (.js) extension.
Step 4.2 Add these files in the "css-js.jsp" file using the below quoted text. This is also shown in the image below.
// Under CSS Section
<link rel="stylesheet" href ="style.css"/>
// Under JavaScript section
<script src="script.js"></script>
style.css and script.js is included in the css-js.jsp file.
Now, you can add your code in these files and have a play with them. To get the stylesheets and script to add in your file you can visit -
Bootstrap Documentation
Similar Reads
How to Add Bootstrap in a Project?
Bootstrap is a popular open-source framework for building responsive and mobile-first websites using HTML, CSS, and JavaScript. It provides pre-designed components and templates that simplify web development. Why Use Bootstrap?Bootstrap offers several advantages for web development:Faster and Easier
3 min read
How to add icons in project using Bootstrap ?
Bootstrap Icons is an open-source icon library specifically designed for use with Bootstrap's framework. Adding icons using Bootstrap means integrating the Bootstrap Icons library into your project and using its icon classes in HTML. This allows easy inclusion of scalable, customizable icons directl
2 min read
React Bootstrap Tutorial
React-Bootstrap is a popular front-end framework that combines the power of React with the simplicity of Bootstrap. It provides a modern way to build responsive and mobile-first web applications with prebuilt React components styled using Bootstrap.Easy to use React components for Bootstrap-based de
4 min read
Bootstrap 4 | Progress Bars
A progress bar is used to display the progress of a process on a computer. A progress bar displays how much of the process is completed and how much is left. You can add a progress bar on a web page using predefined bootstrap classes. Bootstrap provides many types of progress bars.Syntax:Â Â <div c
4 min read
What is the difference between Bootstrap v3 and v4 ?
In this article, we will see the differences in Bootstrap v3 & v4 and their implementation. Bootstrap is a free open-source front-end framework for creating responsive websites and web applications. It is the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-firs
5 min read
How to Add Bootstrap Form Component Styling ?
Forms play a crucial role in web development, allowing users to interact with websites. By incorporating Bootstrap form component styling, which includes predefined classes like "form-group" for organizing form elements, "form-control" for styling inputs, and "btn" for buttons, we enhance both the f
2 min read
Bootstrap 5 Input Group Button Addons
Bootstrap 5 Input Groups are used to extend form controls by adding the buttons, text, or button groups before or after the text inputs, custom selects, or file inputs. Input Group Button Addons are used to extend form controls by adding buttons or button groups.Bootstrap 5 Input group Button Addons
2 min read
How Many Colors available in Bootstrap 4 ?
Bootstrap 4 supports 10 different color utility classes that can be used for text and background colors. Each of these colors has a name that can be used to describe the use of colors on a page. Color Utility Text Classes: These classes are used for changing the color of the text. text-primary: It g
3 min read
Difference between HTML and Bootstrap
HTML stands for HyperText Markup Language. It is a standard markup language for creating webpages. It describes the structure of webpages. HTML is a basic building block of a website. It has different attributes and elements with different properties. HTML consists of a series of elements, which you
4 min read
How to Use Bootstrap with React?
Bootstrap is one of the most popular front-end frameworks, widely used to create visually appealing, responsive, and mobile-first websites quickly. It provides pre-designed UI components, grid systems, and various CSS classes that help you build mobile-first, responsive web applications quickly and
9 min read