React Material UI – 如何将内容居中于页面中央

huangapple go评论66阅读模式
英文:

React Material UI - How to centre contents to the middle of the page

问题

目前,按钮位于左上角。如何将按钮移到页面中间?

import { Button } from '@mui/material';

function App() {
  return (
    <Button variant="contained">Hello World</Button>
  );
}

export default App;

尝试了上面的代码,但显然需要进行更多的更改以居中它。

英文:

At the moment, the button is outputting on the top left corner. How to bring the button to the middle of the page?

import {Button} from &#39;@mui/material&#39;;

function App() {
  return (
      &lt;Button variant=&quot;contained&quot;&gt;Hello World&lt;/Button&gt;
  );
}

export default App;

Tried the above code but obviously requires more changes to centre it

答案1

得分: 1

你必须使用 Grid如果你不想添加自定义样式

import { Button, Grid } from "@mui/material";

function App() {
  return (
    <Grid container justifyContent="center" alignItems="center">
      <Button variant="contained">Hello World</Button>
    </Grid>
  );
}

export default App;
英文:

You have to use Grid, if you don't want to add custom styles

import { Button, Grid } from &quot;@mui/material&quot;;

function App() {
  return (
    &lt;Grid container justifyContent=&quot;center&quot; alignItems=&quot;center&quot;&gt;
      &lt;Button variant=&quot;contained&quot;&gt;Hello World&lt;/Button&gt;
    &lt;/Grid&gt;
  );
}

export default App;

答案2

得分: 0

你可以使用Box标签:

&lt;Box textAlign=&#39;center&#39;&gt;
  &lt;Button variant=&#39;contained&#39;&gt;
     你好,世界
  &lt;/Button&gt;
&lt;/Box&gt;
英文:

You can use the Box tag:

&lt;Box textAlign=&#39;center&#39;&gt;
  &lt;Button variant=&#39;contained&#39;&gt;
     Hello World
  &lt;/Button&gt;
&lt;/Box&gt;

huangapple
  • 本文由 发表于 2023年3月7日 15:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定