Home > front end >  How to align a Button to the far right in MUI AppBar?
How to align a Button to the far right in MUI AppBar?

Time:10-14

I'm having trouble understanding how to align items in MUI. I have the following code:

class SignUpForm extends React.Component {
    render() {
        return (
            <Button sx={{ justifyContent: "flex-end" }}
                color="inherit" }>Sign Up</Button>
        )
    }
}

which is composed by:

class Nav extends React.Component {
    render() {
        return (
            <Box sx={{ flexGrow: 1}}>
                <AppBar position="static">
                    <Toolbar>
                        <SignUpForm />
                    </Toolbar>
                </AppBar>
            </Box>
        )
    }
}

But unfortunately the content is still staying to the left. Using this resource enter image description here

Thank you.

CodePudding user response:

I'm pretty sure you just need to change sx={{ justifyContent: "flex-end" }} to sx={{ marginLeft: "auto" }} on the Button

CodePudding user response:

Toolbar is a flexbox, so you can add a div on the left side and set justify-content to space-between to push the Button to the right:

<Toolbar sx={{ justifyContent: "space-between" }}>
  <div />
  <SignUpForm />
</Toolbar>

Codesandbox Demo

  • Related