【问题标题】:How to align content differently for xs and md in material ui grid如何在材质ui网格中以不同方式对齐xs和md的内容
【发布时间】:2019-09-03 00:27:07
【问题描述】:

我正在使用材质 ui Grid 组件来创建响应式网格布局。我想根据屏幕大小对齐网格内的内容 - 左对齐 xs 右对齐 md。我该怎么做?

我使用 xs 和 md 断点来根据屏幕大小划分网格。使用material-ui-grid doc。该文档还提到了一个名为 align-items-xs-flex-start 的属性。所以我给了 align-items-xs-flex-start 和 align-items-md-flex-end 作为 Grid 组件的道具,以查看内容是否在 xs 中与 md 中的左对齐和右对齐。但它没有用。

一些示例代码。

<Grid>
  <Grid xs={12} md={7}>
    Hi there!
  </Grid>
  <Grid xs={12} md={5}>
    John Doe!
  </Grid>
</Grid>

在中等大小的屏幕上,我希望 John Doe 正确对齐。在一个小屏幕上,我希望 John Doe 来到下一行并左对齐。当我尝试使用 align-items-xs-flex-start 和 align-items-md-flex-end 时,在这两种情况下它仍然左对齐。

【问题讨论】:

    标签: reactjs material-ui grid-layout flexboxgrid


    【解决方案1】:

    不确定你最终是否解决了这个问题?无论如何我都会回答,以防其他人有类似的问题:

    然后您可以使用 Material UI 断点为不同的尺寸设置不同的样式。例如,对于您的文本对齐示例,您需要这样的内容:

        [theme.breakpoints.down('sm')]: {
          textAlign: left,
        },
        [theme.breakpoints.up('md')]: {
          textAlign: center,
        }
    

    另外,如果有人对您的示例感到困惑,他们应该知道 Grid 元素应该具有 containeritem 属性才能正确显示:

    <Grid container>
      <Grid item className="grid-el" xs={12} md={7}>
        Hi there!
      </Grid>
      <Grid item className="grid-el" xs={12} md={5}>
        John Doe!
      </Grid>
    </Grid>
    

    更多关于断点的信息:https://material-ui.com/customization/breakpoints/

    【讨论】:

    • 有一个名为 justify={"flex-start"|"flex-end"|...etc...} 的属性,问题是关于您是否可以拥有justify 属性对 xs 和 md 的工作方式不同。
    • 谢谢,但他们没有提到 justify 属性...他们询问如何根据屏幕尺寸对齐网格内的内容。如果您有更好的方法,请使用示例代码留下答案!
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2019-10-03
    相关资源
    最近更新 更多