简体   繁体   English

在 React 中使用 MUI v5 将按钮对齐到页面底部

[英]Align button to the bottom of the page using MUI v5 in React

I'm trying to position the button exactly at the center bottom of the page irrespective of the content height in that page.我试图将按钮准确地定位在页面的中心底部,而不管该页面中的内容高度如何。

Excerpt from my code摘自我的代码

const useStyles = makeStyles({
  button: {
    bottom: 0,
    right: 0,
    position: "absolute"
  }
});

export default function Home() {
  const classes = useStyles();
  return (
    <div>
      <Box>
        <Box>
          <Paper elevation={2} sx={{ width: "100%", height: "50vh" }}>
            Some data
          </Paper>
        </Box>
        <Box textAlign="center">
          <Button
            className={classes.button}
            variant="contained"
            sx={{ width: "200px" }}
          >
            Submit
          </Button>
        </Box>
      </Box>
    </div>
  );
}

I created a working example using CodeSandbox .我使用CodeSandbox创建了一个工作示例。 Could anyone please help?有人可以帮忙吗?

This is more css related ( see other post ) but you as you manually set the width of the Button you can position it 50% from the left and then subtracting half of its width from its left margin:这与 css 相关(请参阅其他帖子),但是当您手动设置Button的宽度时,您可以将其从左侧定位 50%,然后从其左边距减去其宽度的一半:

bottom: 0,
left: "50%",
marginLeft: -100,
position: "absolute"

see exemple 见例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM