How to use Bootstrap with NextJS?
Last Updated :
30 Jul, 2024
Next.js is an open-source web development framework based on React and has gained significant popularity due to its amazing features. It is developed by Vercel and Next.js stands out for its robust capabilities, including server-side rendering(SSR) and enhanced search engine optimization (SEO). Next.js provides built-in routing, making it simple to create dynamic routes and handle navigation within your application.
In this article, we will learn how to use Bootstrap 5 with Nextjs. Bootstrap 5 is a powerful free framework that helps developers like you build modern webpages faster and easier. It provides pre-built components and styles for things like buttons, navigation bars, and forms.
Steps to use Bootstrap 5 with Next JS
Step 1: Create a Next Js App
You can create a new Next application using the command below.
npx create-next-app app-name
or
yarn create next-app app-name
or
pnpm create next-app app-name
Step 2 : Install Bootstrap
Once your next project is created, open the project’s root directory and install the bootstrap dependencies with the following command.
npm install bootstrap
Step 3 : Import the Bootstrap module in the project
Add the below code to the layout.js to add the styling to project
import "bootstrap/dist/css/bootstrap.min.css"
Project Structure:
Project StructureUpdated Dependencies:
"dependencies": {
"bootstrap": "^5.3.3",
"next": "14.2.3",
"react": "^18",
"react-dom": "^18"
}
Example: The App contains the Navbar .heading along with some buttons styled using Bootstrap 5
JavaScript
//page.js
export default function Home() {
return (
<div>
{/* navbar */}
<nav className="navbar navbar-expand-lg bg-body-tertiary">
<div className="container-fluid">
<a className="navbar-brand" href="#">
Navbar
</a>
<button
className="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Home
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
Contact-us
</a>
</li>
<li className="nav-item">
<a className="nav-link active" aria-current="page" href="#">
About Us
</a>
</li>
<li className="nav-item">
<a className="nav-link" href="#">
Link
</a>
</li>
</ul>
<form className="d-flex" role="search">
<input
className="form-control me-2"
type="search"
placeholder="Search"
aria-label="Search"
/>
<button className="btn btn-outline-success" type="submit">
Search
</button>
</form>
</div>
</div>
</nav>
<h2 className="text-center text-bg-primary m-2 p-2">
Bootstrap 5 with Next.js
</h2>
<div className="container-fluid m-2 border border-success text-center">
<h4>Components</h4>{" "}
<div className="row m-2 ">
<div className="col-sm">First Component</div>
<div className="col-sm">Second Component</div>
<div className="col-sm">Third Component</div>
</div>
</div>
<div className="container-fluid m-2 border text-center">
<h4>Buttons</h4>
<div className="row m-2 justify-content-between ">
<div className="col-sm-auto">
<button type="button" className="btn btn-primary">
Primary
</button>
</div>
<div className="col-sm-auto">
<button type="button" className="btn btn-secondary">
Secondary
</button>
</div>
<div className="col-sm-auto">
<button type="button" className="btn btn-success">
Success
</button>
</div>
<div className="col-sm-auto">
<button type="button" className="btn btn-danger">
Danger
</button>
</div>
</div>
</div>
</div>
);
}
JavaScript
//layout.js
import "bootstrap/dist/css/bootstrap.min.css"
import AddBootstrap from "./AddBootstrap";
export const metadata = {
title: "NextJs and Bootstrap 5",
description: "Generated by create next app",
};
export default function RootLayout({ children }) {
return (
<html lang="en">
<body >
<AddBootstrap/>
{children}</body>
</html>
);
}
JavaScript
// AddBootstrap.js
"use client";
import { useEffect } from "react";
export default function AddBootstrap()
{
useEffect(()=>{
import( "bootstrap/dist/js/bootstrap.bundle.js")
},[])
return <></>
}
Explanantion
- Paje.js contains the navbar ,header , components and buttons which are styled using the bootstrap classes.
- In layout.js we have imported the " Bootstrap module" which is used to add style to the children elements ,which add styles to whole project.
- In AddBootstrap.js we have used useEffect hook to add the functionality to toggle the navbar using the " bootstrap/dist/js/bootstrap.bundle.js"
Output:
Bootstrap 5 with Next Js
Similar Reads
How To Setup Multiple Themes In NextJS With React-Bootstrap? Multiple themes mean providing users with different visual styles or color schemes for an application, enhancing user experience and personalization. In this article, we'll explore how to set up multiple themes in a Next.js application using React-Bootstrap. We'll explore through creating a theme co
3 min read
How to use bootstrap 4 in angular 2? Bootstrap is an open-source toolkit for developing with HTML, CSS, and JS. The Bootstrap framework can be used together with modern JavaScript web & mobile frameworks like Angular. Bootstrap 4 is the newest version of Bootstrap, which is the most popular HTML, CSS, and JavaScript framework. This
2 min read
How to install Bootstrap 5? Bootstrap 5, a popular front-end framework, empowers developers to create sleek and responsive websites with minimal effort. To install Bootstrap 5, you can choose from three methods including CDN links for quick integration, employ package managers like npm for seamless dependency management, or ma
5 min read
How to use Material-UI with Next.js ? Integrating Material-UI (now known as MUI) with Next.js allows you to build modern, responsive user interfaces with a comprehensive set of React components. In this article, we will learn some additional necessary steps to be performed to integrate Material-UI with the Next.js project.Prerequisites:
4 min read
How to create a web page using Bootstrap ? Bootstrap is an open-source CSS framework for creating a responsive and customizable frontend for websites and web applications. Using Bootstrap's grid system one can easily create a web page very fast. Any webpage these days needs to have a navbar for user navigation, some content & a form for
6 min read
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 use Next.js API Routes? Next.js API Routes are a feature of Next.js that allows you to create server-side logic and APIs within your Next.js application. These API routes are implemented using files in the `pages/api` directory of your Next.js project. When you deploy your Next.js application, these API routes are automati
8 min read
How to add Popup in NextJS ? In this article, we are going to learn how we can add Popup in NextJs. NextJS is a React-based framework. It has the power to Develop beautiful Web applications for different platforms like Windows, Linux, and mac. Approach: To add our Popup in NextJs we are going to use the reactjs-popup package. T
3 min read
Bootstrap Tutorial Bootstrap is a popular front-end framework for building responsive and mobile-first websites. It provides pre-designed CSS, JavaScript components, and utility classes to quickly create modern and consistent user interfaces.It Includes pre-built responsive grid systems for mobile-first design.Offers
4 min read
How to Use NextJS in Typescript? TypeScript enhances Next.js applications by adding static type checking and improves developer experience through type safety. Integrating TypeScript into your Next.js project helps catch errors early and improves code maintainability. It offers even greater productivity and robustness to your web d
5 min read