How to Install Bootstrap in React
By Codes With Pankaj
Bootstrap is a popular CSS framework that helps style your React application quickly and easily.
Step-by-Step Guide
Step 1: Create a React App (if not already created)
If you don’t already have a React app, create one by running:
Step 2: Install Bootstrap
Option 1: Install via NPM
Open the terminal in your project directory.
Run the following command to install Bootstrap:
Once installed, you’ll see
bootstrap
listed in yourpackage.json
file under dependencies.
Option 2: Use a CDN (Quick and Easy)
You can add Bootstrap via a CDN link if you don’t want to install it.
Open the
public/index.html
file in your React app.Add the following
<link>
tag inside the<head>
section:
Note: Using the CDN method does not require installing Bootstrap, but it’s less flexible for custom builds.
Step 3: Import Bootstrap in Your React App
If you used NPM to install Bootstrap, you need to import it into your project.
Open the
src/index.js
file.Add the following import statement at the top:
This makes Bootstrap’s styles available globally in your app.
Step 4: Use Bootstrap Classes in Your Components
Now, you can use Bootstrap’s CSS classes in your React components to style your elements.
Open
src/App.js
.Replace its content with the following code to create a simple Bootstrap-styled component:
Explanation:
container
: Centers and limits the width of the content.text-center
: Centers the text horizontally.text-primary
: Applies the Bootstrap primary color (blue) to the text.btn btn-success
: Styles the button with a green background.
Step 5: Run the Project
Start the development server:
Open your browser at
http://localhost:3000
.
You should see a styled page with a centered heading and a green button.
Optional: Add JavaScript Components (e.g., Modals, Dropdowns)
If you need Bootstrap components that require JavaScript (like modals or dropdowns), you’ll also need Bootstrap’s JavaScript library and Popper.js.
Install the required libraries:
Import Bootstrap’s JavaScript in
src/index.js
:Now, you can use advanced Bootstrap components like modals, carousels, and dropdowns.
Congratulations! 🎉
You’ve successfully installed and started using Bootstrap in your React project.
Next Steps
Experiment with Bootstrap components like grids, cards, and navbars.
Stay tuned for more React tutorials from Codes With Pankaj! 😊
Last updated