tutorial tutorials

How to use Bootstrap 5 in React js

When it comes to styling a website as quickly as possible, there is no good alternative to Bootstrap, Bootstrap gives us ready-made components to use inside our website and style it as quickly as possible.

While developing websites using REACT we have various ways to style our website and one of them is by using Bootstrap,

There are multiple ways of using Bootstrap in a REACT APP.

  1. Using Bootstrap from CDN
  2. Downloading compiled source files of Bootstrap.
  3. Using react-bootstrap package (specially created for react )

The prerequisite is to have a react app created, if not already created then use npx create-react-app my-app to create a new react app if you have already created then continue.

1. Using Bootstrap from CDN

Using Bootstrap from CDN is very simple you just have to include the CDN link of Bootstrap inside <head> </head> tag inside the index.html file which you can find inside the public folder of your project and below is the code which you need to use.

<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-0evHe/X+R7YkIZDRvuzKMRqM+OrBnVFBL6DOitfPri4tjfHxaWutUpFmBp4vmVor" crossorigin="anonymous">

<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-pprn3073KE6tl6bjs2QrFaJGz5/SUsLqktiwsUTF55Jfv3qYSDhgCecCxMW52nD2" crossorigin="anonymous"></script>

after including both the script tag given above you’ll be able to use Bootstrap in your project right away.

2. Downloading compiled Bootstrap source files.

Another method of using Bootstrap in React is also the same as using Bootstrap in any project that is the traditional way of downloading the source files of Bootstrap and linking them inside our HTML files and use as local CSS, JS files.

Follow this link to download Bootstrap 5.2 source files from the official website.

3. Using React Bootstrap package

React Bootstrap is the special version of Bootstrap created especially to be used with React and is currently aligned with the Bootstrap version 5.1 don’t be confused with its own version which is currently 2.4.0.

You can follow this official documentation for better reference.

To use react-bootstrap we have to first install this package using below command

npm install react-bootstrap bootstrap

After the package is successfully installed now we have to import Components that we want to use from React Bootstrap.

For example, if we want to use the Butthon component of Bootstrap inside our project we have to import them as below

import "./styles.css";

import Button from 'react-bootstrap/Button';
//or 
import { Button } from 'react-bootstrap';

export default function App() {
  return (
    <div className="App">
    <h1> Hello Coders!</h1>
    </div>
  );
}

Now we are ready to use Button component inside our app like

import "./styles.css";
import Button from 'react-bootstrap/Button';

export default function App() {
  return (
    <div className="App">
    <h1> Hello Coders!</h1>
    <Button >Click me </Button>
    </div>
  );
}

Now when you refresh your app it should look like this

But as you can see there is no styling in the Button component, this is because React Bootstrap does not offer CSS styling unless we import it explicitly with

import 'bootstrap/dist/css/bootstrap.min.css';

and now your code should work fine and you can use every Component in similar way

To learn about more react-bootstrap Components Click Here .

That’s it for this tutorials hope you will like other tutorials too.

Related Posts

How To Use react-icons in React Js or Next Js app

Using Icons on a website makes it look so beautiful and easy to navigate, and if we are building one with React Js or any React-based frameworks like…

How To Upgrade Existing Projects To Next Js 13

Upgrade your existing next js projects to next js 13, with simple steps recommended by Vercel like comdemod

responsive navbar using tailwind css

Create a Responsive Navbar using Tailwind CSS

In this tutorial, we will see how we can create a simple responsive Navbar or Header using only Tailwind CSS. For this tutorial, we are going to set…

particle js in react

How to use Particles Js in React with react-tsparticles

Particle.js is a great JavaScript library for creating 2d as well as 3d looking particles on your website. But using Particle.js is not an easy task so there…

tailwind sidebar preview

Tailwind Sidebar Navigation Menu

Source Code Tailwind CSS is a great UI framework for designing and building websites faster, we can quickly implement different components in a short time using Tailwind CSS….

animated hamburger preview

How to create an Animated Toggle Button with CSS

In this tutorial we are going to create an animated Hamburger Navigation Toggle Button with HTML, CSS and little bit of JS Source Code on GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *