Reputation: 623
How do I center a div element in react with using an external css file. I tried using bootstrap classes and inline styles that other people posted but none of those worked. I was wondering how you guys would implement this without an external css file.
Upvotes: 45
Views: 136766
Reputation:
Regular classes in Bootstrap also works with react just needed the original import "./bootstrap/bootstrap.min.css";
file, I often use
<div className={"d-block m-auto"}> Content here </div>
<div className={"text-center"}>Hello</div>
<div className={"d-flex justify-content-center align-items-center"}>Content here</div>
Upvotes: 0
Reputation: 11
className={"d-flex justify-content-center"}
Put this wherever you want the inner elements to be centred.
Upvotes: 0
Reputation: 9344
For people using React Bootstrap, here I show an example of the classes text-align
and justify-content-center
. As the names describe, one is for centering text and the other for centering elements.
Upvotes: 0
Reputation: 22437
I just added:
<Grid container spacing={2} justify="center"></Grid>
Upvotes: 0
Reputation: 857
Offsets: The first uses Bootstrap's own offset classes so it requires no change in markup and no extra CSS. The key is to set an offset equal to half of the remaining size of the row. So for example, a column of size 6 would be centered by adding an offset of 3, that's (12-6)/2.
In markup this would look like:
<div class="row">
<div class="col-md-6 col-md-offset-3"></div>
</div>
margin-auto: You can center any column size by using the margin: 0 auto; technique, you just need to take care of the floating that is added by Bootstrap's grid system. I recommend defining a custom CSS class like the following:
.col-centered{
float: none;
margin: 0 auto;
}
Now you can add it to any column size at any screen size and it will work seamlessly with Bootstrap's responsive layout :
<div class="row">
<div class="col-lg-1 col-centered"></div>
</div>
Upvotes: 32
Reputation: 911
Have this between the imports and React Class
const styles = {
center: {
marginLeft: "auto",
marginRight: "auto"
}
}
Then to use
<div className={styles.center}>
hi
</div>
Upvotes: 8
Reputation: 686
An easy way to do this using react-bootrsrap is
<Grid>
<Row className="text-center"><h1>Our Products</h1></Row>
</Grid>
Use className (instead of class) to take advantage of Bootstrap's built in class of "text-center".
Upvotes: 15
Reputation: 21575
I am using react-bootstrap
for this:
export default class CenterView extends React.Component {
render() {
return (
<Grid>
<Row className="show-grid">
<Col xs={1} md={4}></Col>
<Col xs={4} md={4}>{this.props.children}</Col>
<Col xs={1} md={4}></Col>
</Row>
</Grid>
)
}
}
and then in code
<CenterView>
<LoginForm/>
</CenterView>
Upvotes: 14
Reputation: 8314
If you don't have to support old browsers, you may look into Flexbox.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Try something like this:
<div style={{display: 'flex', justifyContent: 'center'}}>
<div>centered content</div>
</div>
Upvotes: 51