How to add Bootstrap in Next.js

bootstrapnextjs

To use Bootstrap in a Next.js app, you will need to install the Bootstrap library and include its styles in your app. Here is an example of how you can add Bootstrap to a Next.js app:

First, install Bootstrap by running the following command in your terminal:

npm install bootstrap

Next, create a file called _app.js in the pages directory of your Next.js app. This file allows you to customize the app component provided by Next.js. In the _app.js file, import the Bootstrap styles by adding the following line at the top of the file:

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

Then, you can use the Bootstrap classes in your component templates to apply Bootstrap styles to your app.

Here is an example of how you can use Bootstrap to create a navbar in a Next.js app:

import Head from 'next/head';

function MyApp({Component, pageProps}) {
    return (
        <>
            <Head>
                <title>My App</title>
            </Head>
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <a class="navbar-brand" href="/">My App</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
                        aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav">
                        <li class="nav-item active">
                            <a class="nav-link" href="/">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/about">About</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="/contact">Contact</a>
                        </li>
                    </ul>
                </div>
            </nav>
            <Component {...pageProps} />
        </>
    );
}

export default MyApp;

This will create a navbar with a brand name and a toggle button for responsive layouts. The navbar also has links for the “Home”, “About”, and “Contact” pages.

For more information on how to use Bootstrap with Next.js, you can refer to the Bootstrap documentation: https://getbootstrap.com/docs/4.5/getting-started/introduction/.