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

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

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

问题

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

  1. import { Button } from '@mui/material';
  2. function App() {
  3. return (
  4. <Button variant="contained">Hello World</Button>
  5. );
  6. }
  7. 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?

  1. import {Button} from &#39;@mui/material&#39;;
  2. function App() {
  3. return (
  4. &lt;Button variant=&quot;contained&quot;&gt;Hello World&lt;/Button&gt;
  5. );
  6. }
  7. export default App;

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

答案1

得分: 1

  1. 你必须使用 Grid如果你不想添加自定义样式
  2. import { Button, Grid } from "@mui/material";
  3. function App() {
  4. return (
  5. <Grid container justifyContent="center" alignItems="center">
  6. <Button variant="contained">Hello World</Button>
  7. </Grid>
  8. );
  9. }
  10. export default App;
英文:

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

  1. import { Button, Grid } from &quot;@mui/material&quot;;
  2. function App() {
  3. return (
  4. &lt;Grid container justifyContent=&quot;center&quot; alignItems=&quot;center&quot;&gt;
  5. &lt;Button variant=&quot;contained&quot;&gt;Hello World&lt;/Button&gt;
  6. &lt;/Grid&gt;
  7. );
  8. }
  9. export default App;

答案2

得分: 0

你可以使用Box标签:

  1. &lt;Box textAlign=&#39;center&#39;&gt;
  2. &lt;Button variant=&#39;contained&#39;&gt;
  3. 你好,世界
  4. &lt;/Button&gt;
  5. &lt;/Box&gt;
英文:

You can use the Box tag:

  1. &lt;Box textAlign=&#39;center&#39;&gt;
  2. &lt;Button variant=&#39;contained&#39;&gt;
  3. Hello World
  4. &lt;/Button&gt;
  5. &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:

确定