82

I want to align my button to the right of the parent.

I was wondering if there is a proper way to do it in Material UI. I could use:

<Grid container justify="flex-end">

But then I would have to use another <Grid item />. Seems like too much work.

Or maybe I am just better off using plain old CSS, messing around with float: right, and dealing with the apparent zero height of the element.

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
Vedant Agarwala
  • 18,146
  • 4
  • 66
  • 89

9 Answers9

126

BEFORE Material UI 5:

Try this

<Grid container justify="flex-end">
  <Button>Example</Button>
</Grid>

UPDATE Material UI 5: (Thanks to Dipak)

The prop justify of ForwardRef(Grid) is deprecated. Use justifyContent instead, the prop was renamed.

<Grid container justifyContent="flex-end">
  <Button>Example</Button>
</Grid>

Update: The best solutions are the answers of NearHuscarl or Eduardo Oviedo Blanco, you'd rather use Stack or Box than Grid.

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
ThomasP1988
  • 4,597
  • 3
  • 30
  • 36
43

If you would like to align items within the <Grid item> you can use a wrapping Box component as follows:

<Grid container>
  <Grid item sm={6}>
    <Box display="flex" justifyContent="flex-end">
      <Button>Button Aligned To The Right</Button>
    </Box>
  </Grid>
</Grid>

See the docs for more positioning options.

andromeda
  • 4,433
  • 5
  • 32
  • 42
13

In Material UI v5, you can use Stack to display a set of components on a row or a column. Stack is a flexbox so you only need to set the justifyContent prop to align the item inside:

<Stack direction="row" justifyContent="end">
  <Button variant="contained">Item 1</Button>
</Stack>

Codesandbox Demo

Olivier Tassinari
  • 8,238
  • 4
  • 23
  • 23
NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
3

If you do not want to use the Grid component, you have to rely on CSS.

But if you use Material-UI you should use JSS (CSS-IN-JS) and not plain CSS.

In CSS you may use any of these solutions for example. "text-align" being the simplest one.

  • text-align: right
  • float: right
  • flexbox
    • parent with : display: flex
    • child with : align-content: flex-end
Ricovitch
  • 2,278
  • 1
  • 21
  • 28
2

Use the Box component with flex.

<Box display="flex" flexDirection="column" >
  <... other stuff/>
</Box>

You can learn more here

2
            <Toolbar sx={{ display: "flex", justifyContent: "space-between" }}>
                <Typography sx={{ fontWeight: 600 }}>
                    Recent Repositories
                </Typography>
                <Box>
                    {/* <Typography></Typography> */}
                    <Button error>+ New</Button>
                </Box>
            </Toolbar>
Sailist
  • 134
  • 8
1

For Latest material-ui version 5 use justifyContent="flex-end" or alignContent which is equivalent to css text-align and flex-end=right

    <Grid container alignContent="flex-end">
         <Button>Example</Button>
    </Grid>

or
    <Grid item justifyContent="flex-end">
         <Button>Example</Button>
    </Grid>

For old material-ui version 4

    <Grid item justify="flex-end">
         <Button>Example</Button>
    </Grid>
Hassan Saeed
  • 6,326
  • 1
  • 39
  • 37
0

As this question is not specified towards Grid, here a more general answer:

there are other components for which the parameters need to be provided in an sx.

<Toolbar sx={{ justifyContent: 'flex-end' }}>
til
  • 832
  • 11
  • 27
-3

Material UI's Grid Component has helper props to achieve this.

<Grid align-items-xs-center justify-xs-flex-end>
 <Button>Click me</Button>
</Grid>

You can find the docs here.

eskawl
  • 587
  • 1
  • 4
  • 17