I’m using React-Bootstrap in my React app. It is causing margin on the left and right side. I’m using the following code:
import React, { Component } from "react";
import "react-bootstrap/dist/react-bootstrap.min.js";
import "bootstrap/dist/css/bootstrap.min.css";
import { Grid, Row, Col } from "react-bootstrap";
import MuiThemeProvider from "material-ui/styles/MuiThemeProvider";
import AppBar from "material-ui/AppBar";
<Grid fluid>
<Row>
<Col xs={12} md={12}>
<AppBar title="Title"
iconClassNameRight="muidocs-icon-navigation-expand-more"/>
</Col>
</Row>
<Row>
<Col xs={4} md={4}>
<h1>Hello</h1>
</Col>
<Col xs={8} md={8} >
<h1>Hello World!</h1>
</Col>
</Row>
</Grid>
I’m getting the following output:
If I remove xs and md
from <Col>
then the issue gets fixed.
Importing twitter-bootstrap is causing this issue. If I remove the twitter-bootstrap import then the bootstrap styling doesn’t work.
This issue is same as Twitter-Bootstrap’s issue, but I’m not able to fix it in React-Bootstrap.
7
Answers
EDIT: I changed the row to grid. if that fails, then you need to post more code. is there any other div containers the navbar is inside?
there is a few ways to do this:
another way is:
I tested your code with a clean react app. The previous suggestions were wrong. You need to set
Grid
componentspadding-left
andpadding-right
to 0.UPDATE: Just setting
Grid
is not enough. Also need to set margins to 0 ofRow
and paddings to 0 ofCol
.You can achieve this by 3 ways.
1. Way: Add inline style for
Grid
,Row
andCol
OR
2. WAY: Add a custom class name
3. WAY You can globally change
Grid
,Row
andCol
components behaviour by overriding components classNameThe answer above is overkill. This really isn’t that complicated. According to the official React Bootstrap documentation on Grid the fluid property can be applied. Here’s the description for fluid:
Turn any fixed-width grid layout into a full-width layout by this property.
Adds container-fluid class.
Here’s what I did and it works perfectly:
Really late to the party here; I just wanted to add that in Bootstrap 4, I was able to remove the column margins by adding
fluid="true"
to theContainer
andnoGutters
to theRow
, e.g.Adding
fluid
only does not seem to be enough. Sample code is untested.The most correct answer that worked for me is
I used react-bootstrap with next.js. I came up with short solution:
Comment. bootstrap’s css was imported in my _App.js
2021
I faced the same issue with react bootstrap the way I solved it is by nullifying gutters. Bootstrap 5 classes work perfectly in such cases.
Note that you also need to add gx-0 not only to the parent Container, but also to the child Row, otherwise it won’t work.