ReactFix
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • React
  • React Native
  • Programming
  • Object Oriented Programming

Friday, November 4, 2022

[FIXED] How to align a Button to the far right in MUI AppBar?

 November 04, 2022     css, material-ui, reactjs   

Issue

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 https://mui.com/system/properties, I might be missing an important CSS concept here. Could anyone enlighten me?

enter image description here

Thank you.


Solution

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



Answered By - NearHuscarl
Answer Checked By - - Clifford M. (ReactFix Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post Home

Featured Post

Is Learning React Difficult?

React is difficult to learn, no ifs and buts. React isn't inherently difficult to learn. It's a framework that's different from ...

Total Pageviews

Copyright © ReactFix