Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUI - How to align a component to the center/right?

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 MUI. I could use:

<Grid container justify="flex-end"> 

But then I would have to use another <Grid item />. Seems too much of 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.

like image 818
Vedant Agarwala Avatar asked Jul 18 '17 06:07

Vedant Agarwala


People also ask

How do you align centers in MUI?

Since direction=”row”, we have to manually set the center alignment both vertically and horizontally. On the “Bottom Center” item, alignItems=“flex-end” adds the vertical bottom alignment and justify=“center” adds the horizontal centering.

How do you align elements in react?

To center a component horizontally and vertically in React. js, set its display property to flex and its alignItems and justifyContent properties to center . The components's content will be horizontally and vertically centered. Copied!


1 Answers

BEFORE MUI 5:

Try this

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

UPDATE MUI 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 solution is the answer of NearHuscarl, you'd better use Stack than Grid.

like image 124
ThomasP1988 Avatar answered Sep 25 '22 13:09

ThomasP1988