Author avatar

Gaurav Singhal

What to Do if Your React and Bootstrap Columns Aren't Working Together

Gaurav Singhal

  • Nov 10, 2020
  • 3 Min read
  • 97 Views
  • Nov 10, 2020
  • 3 Min read
  • 97 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Using native Bootstrap in a React project can often cause errors due to an incorrect initial setup. In order to make Bootstrap columns work together, you need to make sure that you have correctly imported and required the CSS file containing styles for row and col classes. Otherwise, your Bootstrap rows and columns will act like regular <div> without the required styles to support their intended structure. In this guide, you'll learn how to correctly set up React with Bootstrap so that Bootstrap columns work correctly together.

Implementation

Create a new React project using the following command:

1
npx create-react-app react-bootstrap-columns
shell

Inside the root folder, install Bootstrap via npm using the following command:

1
npm i bootstrap
shell

Next, modify the App.js as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import './App.css';

function App() {
  return (
    <div className="App">
      <div className="row">
        <div className="col">
          <div className="box">Col 1/3</div>
        </div>
        <div className="col">
          <div className="box">Col 2/3</div>
        </div>
        <div className="col">
          <div className="box">Col 3/3</div>
        </div>
      </div>
    </div>
  );
}

export default App;
jsx

The App component simply outputs three nested <div> containing display text inside a parent <div>. The parent <div> is a Bootstrap row, aligning all child elements in a row. Since the three children <div> have the col class and are direct children of the row, each is expected to occupy a third of the width of the entire row next to each other as equally spaced columns. Add some simple styles inside App.css as shown below.

1
2
3
4
5
6
7
8
9
10
11
.App{
  max-width: 100vw;
  overflow-x: hidden;
}
.box{
  margin: 10px;
  padding: 20px;
  border: 2px solid salmon;
  text-align: center;
  color: salmon;
}
css

In contrary to the expected outcome, the three bordered boxes appear as regular <div> spanning over an entire row stacked on top of each other instead of being aligned as columns. This is because the default Bootstrap styles for row and col classes are not applied even though Bootstrap is installed in your project. The App component simply loads the JSX without any declared styles for the classes used. These styles are present inside a CSS file inside the node_modules folder where the entire Bootstrap library is installed. Import this file on the top of your App component as shown below.

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

Now the entire styles come back, with the three columns stacked next to each other in a single row. The font-family of the text inside the box also changes. This is because it inherited some styles from Bootstrap's CSS file.

Conclusion

It's important to understand how different UI libraries let you import the required stylesheet for their components. Working with Bootstrap by either downloading the files or using CDNs may not produce the error because the styles are shipped to your components via the <script> or <link> tags. But when using it via npm, you need to import the stylesheet as a module inside every component where you intend to use its native classes. The same pattern can be extended to libraries such as React Bootstrap, Reactstrap, etc.

0