18
votes

I couldn't figure out how to center buttons in material ui. This is the code I have:

function BigCard(props) {
    const { classes } = props;
    return (
    <div>
        <Card className={classes.card}>
        <CardContent>
            <Typography variant="display1" className={classes.title}>
            Start Funding!
            </Typography>
        </CardContent>
        <CardActions >
            <Button size="small" color="primary" className={classes.actions}>
            Share
            </Button>
            <Button size="small" color="primary">
            Learn More
            </Button>
        </CardActions>
      </Card>
    </div>
  );

How can I center my button?

8
Add code as text instead of images. Also, try to add a minimal verifiable example: stackoverflow.com/help/mcve - MUlferts

8 Answers

26
votes

you could use the override with classes in the material ui doc,

FIRST WAY

You can do something like :

//Add your justify css in your styles const
const styles = {
    ...
    root: {
        justifyContent: 'center'
    }
};

And use the classes props to add this to your CardActions component :

 <CardActions classes={{root: classes.root}}>

SECOND WAY (easier)

OR You can use the style directly on your component, but i advise you to train how to use classes if you're working alot with material-ui

Just do something like :

<CardActions style={{justifyContent: 'center'}}>
16
votes

You can use Box element

<Box textAlign='center'>
  <Button variant='contained'>
     My button
  </Button>
</Box>
13
votes

For use cases that avoid defining css

<Grid container justify="center">
  {props.children}
</Grid>
4
votes

Quick and dirty:

<Button style={{margin: '0 auto', display: "flex"}}>
  NEXT
</Button>
3
votes

What I tried is below and is also shown in official videos of material ui on youtube.

<Grid item xs={12} sm={12} md={4} lg={4}
    style={{
        textAlign:'center' // this does the magic
    }}
>
    <Button>
         NEXT
    </Button>
</Grid>

Thanks and best wishes

3
votes

Here is how I did it with Material - UI

import {Typography, Button} from '@material-ui/core';

 <Typography align='center'>
    <Button
      color='primary'
      size='large'
      type='submit'
      variant='contained'
     >
      Sign Up
    </Button>
  </Typography>
0
votes

You have to use two properties: display and justify-content

<CardActions display='flex' justifyContent='center'>
0
votes

This will centre your button horizontally

 <Button
                size="medium"
                variant="contained"
                color="primary"
                style={{
                    display: "flex",
                    flexDirection: "row",
                    justifyContent:"center",
                    backgroundColor: purple[500],
                    '&:hover': {
                        backgroundColor: purple[700],
                     },
                }}
                
            >
            Get Hired
       </Button>