How to move a React component using CSS ? No functions or classes are working

Viewed 84

I am trying to move a button in my appbar to the right. Here is what it looks like: enter image description here

I have tried 2 ways to fix this:

    return (
        <AppBar
            position="sticky">
            <Toolbar
                sx = {{background:'black', float:'right'}}
                variant="dense">
                    <CustomButton
                        text='Upload'
                        sx = {{float:'right'}}
                        endIcon={<UploadIcon/>}/>
            </Toolbar>
        </AppBar>
    )
}
export default Header;

I also tried to do the same thing with a useStyles hook:

import UploadIcon from "@mui/icons-material/Upload";
import CustomButton from "./CustomButton";
import AppBar from '@mui/material/AppBar';
import Toolbar from '@mui/material/Toolbar';
import { makeStyles } from '@material-ui/core/styles';
import {createMuiTheme} from '@material-ui/core/styles';
const useStyles  = makeStyles({
    root: {
        float:'right'
    }
});
const Header = () => {
    const classes = useStyles();
    return (
        <AppBar position="sticky">
            <Toolbar
                sx = {{background:'black'}}
                variant="dense">
                    <CustomButton text='Upload'
                                  className={classes.root}
                                  endIcon={<UploadIcon/>}/>
            </Toolbar>
        </AppBar>
    )
}
export default Header; 

But none of them have worked and the button won't move to the right.

The code of my button component is as follows:

import {Button} from '@material-ui/core';
import UploadIcon from '@mui/icons-material/Upload';

const CustomButton = ({float,text, endIcon}) => {

    return (
        <div className='CustomButton'>
            <Button style={{fontWeight: 'bold'}}
                    variant="contained"
                    size='small'
                    endIcon={endIcon}>
                {text}
            </Button>
        </div>
    )
}
export default CustomButton;
2 Answers

In mui, u will use flex alot.

return (
    <AppBar
        position="sticky">
        <Toolbar
            sx = {{background:'black', display: 'flex', justifyContent: 'flex-end'}}
            variant="dense">
                <CustomButton
                    text='Upload'
                    endIcon={<UploadIcon/>}/>
        </Toolbar>
    </AppBar>
)
}
export default Header;

Since mui v5, also nice to learn how to use Stack.

<Toolbar>
    <Stack direction="row" justifyContent="space-between">
         <MyLogo />   //extreme left
         <MyButton /> //extreme right
    </Stack>
</Toolbar>

Stack can also be nested

<Toolbar>
    <Stack direction="row" justifyContent="space-between">
         <Stack direction="row"> // content in extreme left
              <MyLogo />   
              <MyOtherLeftComponent />
         </Stack>
         
         <MyButton /> //extreme right
    </Stack>
</Toolbar>

On the toolbar use following style

display:flex;
justify-content:flex-end.
Related