🤩 If you like this product, please support us by starring our GitHub repository! GitHub stars

Share

How to use Bootstrap 5 in React.js

Using Bootstrap 5 in React.js involves a few steps. Bootstrap 5 is a popular CSS framework that makes it easier to create responsive and visually appealing web applications. Here's a simple guide on how to integrate Bootstrap 5 into a React.js project:

Method 1: Using CDN

 

1 - Create a new React App:

npx create-react-app my-react-bootstrap-app
cd my-react-bootstrap-app

 

2 - Install Bootstrap via CDN:

Open the public/index.html file and include the Bootstrap CSS and JS files in the <head> section:

<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Other meta tags and links -->
  <link
    href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
    rel="stylesheet"
  />
</head>
<body>
  <div id="root"></div>
  <!-- Other scripts -->
</body>
</html>

 

3 - Use Bootstrap Components in React Components:

Open src/App.js and start using Bootstrap components:

import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';

function App() {
  return (
    <div className="container mt-5">
      <h1>Hello, Bootstrap in React!</h1>
      <button className="btn btn-primary">Bootstrap Button</button>
    </div>
  );
}

export default App;

Method 2: Using npm

 

1 - Create a new React App:

npx create-react-app my-react-bootstrap-app
cd my-react-bootstrap-app

 

2 - Install Bootstrap using npm:

npm install bootstrap@5

 

3 - Import Bootstrap in your React Components:

Open src/index.js or src/App.js and import the Bootstrap CSS file:

import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

 

4 - Use Bootstrap Components:

Now you can use Bootstrap components in your React components as shown in the previous example.

Remember that these methods provide a basic setup. You may need to adjust the integration based on your project structure and requirements. Bootstrap documentation (https://getbootstrap.com) is a valuable resource for understanding and using Bootstrap components and utilities.