Published on October 10, 2023
▶️ Watch This Guide as a Video
▶️ If you're project is using the pages router, refer to this video.
🔗 See the complete code for this example on GitHub.
npx create-next-app@latest nextjs-bootstrap-example --use-npm
cd nextjs-bootstrap-example code .
npm run dev
export default function Home() { return ( <main> <h1 className="text-danger">Hello Bootstrap</h1> </main> ) }
npm install bootstrap
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap.css'; import './globals.css' import { Inter } from 'next/font/google' const inter = Inter({ subsets: ['latin'] }) export const metadata = { title: 'Create Next App', description: 'Generated by create next app', } export default function RootLayout({ children }) { return ( <html lang="en"> <body className={inter.className}>{children}</body> </html> ) }
export default function Home() { return ( <main> <div className="text-center mt-4 col-md-6 mx-auto"> <h1 className="text-danger">Hello Bootstrap</h1> <div className="accordion" id="accordionExample"> <div className="accordion-item"> <h2 className="accordion-header" id="headingOne"> <button className="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Accordion Item #1 </button> </h2> <div id="collapseOne" className="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample"> <div className="accordion-body"> <strong>This is the first accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. Also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow. </div> </div> </div> <div className="accordion-item"> <h2 className="accordion-header" id="headingTwo"> <button className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Accordion Item #2 </button> </h2> <div id="collapseTwo" className="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample"> <div className="accordion-body"> <strong>This is the second accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. Also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow. </div> </div> </div> <div className="accordion-item"> <h2 className="accordion-header" id="headingThree"> <button className="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> Accordion Item #3 </button> </h2> <div id="collapseThree" className="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample"> <div className="accordion-body"> <strong>This is the third accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. Also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow. </div> </div> </div> </div> </div> </main> ) }
"use client" import { useEffect } from 'react'; function BootstrapClient() { useEffect(() => { require('bootstrap/dist/js/bootstrap.bundle.min.js'); }, []); return null; } export default BootstrapClient;
import BootstrapClient from '@/components/BootstrapClient.js';
import 'bootstrap/dist/css/bootstrap.css'; import './globals.css' import { Inter } from 'next/font/google' import BootstrapClient from '@/components/BootstrapClient.js'; const inter = Inter({ subsets: ['latin'] }) export const metadata = { title: 'Create Next App', description: 'Generated by create next app', } export default function RootLayout({ children }) { return ( <html lang="en"> <body className={inter.className}> {children} <BootstrapClient /> </body> </html> ) }
I'm a software developer interested in building world changing applications. My goal is to document and share all the knowledge I learn so others can quickly start building something awesome.