簡體   English   中英

在材質 UI 中,如何將 Button 置於 Grid 項目的中間?

[英]In material UI, how do I center a Button in the middle of my Grid item?

我在我的 React 16.13.0 應用程序中使用 Material UI 的 Grid。 我想將按鈕的內容居中在我的行中間。 所以我想出了這個

center: { alignItems: "center", width: "100%", }, halfRow: { width: "50%", }, ...

      <Grid item className={classes.halfRow}>
        <Button size="medium" onClick={startMission} className={classes.center}>
          <Grid container direction="row" spacing={1} style={{ width: "100%", alignItems: "center" }}>
            <Grid item>
              <PlayCircleFilledIcon />
            </Grid>
            <Grid item>START</Grid>
          </Grid>
        </Button>
      </Grid>

但是,這並沒有起到作用。 按鈕似乎向左對齊...

在此處輸入圖像描述

我應該應用什么正確的樣式以使所有內容都集中在單元格的中間?

Grid 接受justifyalignItems作為道具,您可以將其傳遞給您的內容居中

 <Grid item className={classes.halfRow}>
    <Button size="medium" onClick={startMission} className={classes.center}>
      <Grid container direction="row" spacing={1} alignItems={'center'} justify={'center' }style={{ width: "100%" }}>
        <Grid item>
          <PlayCircleFilledIcon />
        </Grid>
        <Grid item>START</Grid>
      </Grid>
    </Button>
  </Grid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM