简体   繁体   English

对齐不同长度的材质 UI 网格项目

[英]Align Material UI Grid Items of different length

I currently have a material ui grid system that looks like the following:我目前有一个材质 ui 网格系统,如下所示:

<>
      <Typography gutterBottom variant='h6'>
        Add New User{' '}
      </Typography>
      <Grid container spacing={3} alignItems='center' justify='center'>
        <Grid item>
          <Typography variant='body2'>Full name</Typography>
        </Grid>
        <Grid item>
          <TextField
            style={{ width: 400 }}
            fullWidth
            margin='dense'
            id='outlined-basic'
            label='Full name'
            variant='outlined'
          />
        </Grid>
      </Grid>
      <Grid container spacing={3} alignItems='center' justify='center'>
        <Grid item>
          <Typography variant='body2'>Email</Typography>
        </Grid>
        <Grid item>
          <TextField
            style={{ width: 400 }}
            fullWidth
            margin='dense'
            id='outlined-basic'
            label='Email'
            variant='outlined'
          />
        </Grid>
      </Grid>
</>

I am noticing that because email and full name are different lengths, the grid items do not align in the way I would like them to.我注意到因为 email 和全名的长度不同,所以网格项没有按照我希望的方式对齐。 They currently look like the following:它们目前如下所示:

在此处输入图像描述

I would like the grid to look like the following:我希望网格如下所示:

在此处输入图像描述

Is there a way to align the grid items like the bottom picture without needing to add a fixed width?有没有办法像底部图片一样对齐网格项目而无需添加固定宽度? that seems to be the only solution I have found but I feel like I'm missing something regarding the grid system.这似乎是我找到的唯一解决方案,但我觉得我错过了有关网格系统的一些东西。

attached is a code sandbox https://codesandbox.io/s/affectionate-bhabha-ur9nm?file=/src/App.js for debugging附件是用于调试的代码沙箱https://codesandbox.io/s/affectionate-bhabha-ur9nm?file=/src/App.js

I've added xs={9} for text-field and xs={3} for the label with style={{ textAlign: 'right' }} (not familiar with material-ui so don't know if there's a better way to apply this).我已经为 text-field 添加了xs={9}并为 label 添加了xs={3}style={{ textAlign: 'right' }} (不熟悉 material-ui 所以不知道是否有更好的方法应用这个)。

This is the result: https://codesandbox.io/s/hungry-euclid-n7y8f结果如下: https://codesandbox.io/s/hungry-euclid-n7y8f

you just need to add xs sm lg to your Grid component to take advantage of Auto layout .您只需将xs sm lg添加到Grid组件即可利用Auto layout accoording to the doc:根据文档:

The Auto-layout makes the items equitably share the available space.自动布局使项目公平地共享可用空间。

after applying this change, your items will be aligned properly.应用此更改后,您的项目将正确对齐。 you can also check the corrected version here in the sandbox .您还可以在沙箱中检查更正的版本。

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

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