Ravgeet's Blog

Ravgeet's Blog

Setup and Customize Bootstrap in Next.js

Setup and Customize Bootstrap in Next.js

Learn how to improve the look and feel of the Next project by configuring the default Bootstrap behaviour.

Subscribe to my newsletter and never miss my upcoming articles

This blog was originally published on RavSam blog.

A few months back, we wrote a blog on how to add and customize Bootstrap in Nuxt.js. Today, we will learn how to set up Bootstrap in a Next.js project. We will also install react-bootstrap to use Bootstrap based React components.

Installing Bootstrap

Let us get started by installing the required NPM packages. We will install bootstrap and optionally react-bootstrap.

Since we are going to create custom SCSS files, we also need to install node-sass.

npm install --save bootstrap react-bootstrap node-sass

Creating a Custom SCSS

Let us now create a custom scss file in the styles/scss directory, and name it global.scss. In this file, we need to import Bootstrap’s bootstrap.scss. For the sake of simplicity, let us override the default colour system provided by Bootstrap.

$theme-colors: (
  'primary': #145bea,
  'secondary': #833bec,
  'success': #1ce1ac,
  'info': #ff7d50,
  'warning': #ffbe0b,
  'danger': #ff007f,
  'light': #c0ccda,
  'dark': #001738,
);

@import '/node_modules/bootstrap/scss/bootstrap.scss';

Configuring Next Config

The best part about the newer versions of Next is that they provide built-in SASS/SCSS support. All we need to tell Next is where our styles are stored by configuring the next.config.js and adding the following piece of code:

const path = require('path')

module.exports = {

  ...

  sassOptions: {
    includePaths: [path.join(__dirname, 'styles')],
  },
}

Importing Bootstrap

The final step is to import our custom Bootstrap into our project. Based on where we need to use the custom styles, we can import our global.scss. In this example, let us configure it to be used by the entire project.

In pages/_app.js file, we need to add the following code:

import 'styles/scss/global.scss' // added

function MyApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

export default MyApp

We have done it. We have set up Bootstrap in our Next project.

Thanks for reading 💜


If you enjoyed my blog, follow me for more informative content like this.

I publish a monthly newsletter in which I share personal stories, things that I am working on, what is happening in the world of tech, and some interesting dev related posts which I across while surfing on the web.

Connect with me through TwitterLinkedInGithub or send me an Email.

Ravgeet, Full Stack Developer and Technical Content Writer

 
Share this