简体   繁体   English

材质用户界面。 网格内的 2 个项目; 一个左对齐,一个右对齐。 在同一行

[英]Material UI. 2 items inside a grid; one left-aligned, one right-aligned. In the same line

I am having a hard time understanding how to properly use Material UI's flexbox integeration:我很难理解如何正确使用 Material UI 的 flexbox 整数:

This way I can align the items the way I want:这样我可以按照我想要的方式对齐项目:

export default function Home() {
    return (
        <Grid container justify={"space-between"}>
            <Grid item>
                <Typography>Left text</Typography>
            </Grid>
            <Grid item>
                <Typography>Right text</Typography>
            </Grid>
        </Grid>
    );
}

But I'd expect that this should work, too:但我希望这也应该起作用:

export default function Home() {
    return (
        <Grid container>
            <Grid item xs={6} justify={"flex-start"}>
                <Typography>Left text</Typography>
            </Grid>
            <Grid item xs={6} justify={"flex-end"}>
                <Typography>Right text</Typography>
            </Grid>
        </Grid>
    );
}

But the latter has not the desired outcome.但后者并没有达到预期的结果。 What am I missing here?我在这里想念什么?

Without defining Typography component type, it'll spawn <p> tag, which is of block type.如果不定义 Typography 组件类型,它将生成<p>标签,它是块类型的。 You can find it in the docs: https://material-ui.com/api/typography/ Therefore, you did align something right, but that "something" takes whole 6 columns.您可以在文档中找到它: https://material-ui.com/api/typography/因此,您确实对齐了正确的内容,但是“某事”需要整整 6 列。 Text is not aligned, its heading tag is.文本未对齐,其标题标签是。

You should try this on "Right text" Typography component:您应该在“正确的文本”排版组件上试试这个:

<Typography align="right">Righttext</Typography>

EDIT : You can also add container prop to Grid component.编辑:您还可以将container道具添加到 Grid 组件。 This way, justify property will be available to you:这样,您可以使用 justify 属性:

<Grid item container xs={6} justify={"flex-end"}>
    <Typography>Right text</Typography>
</Grid>

When you add container prop to Grid component, children of that component become flex elements.当您将container prop 添加到 Grid 组件时,该组件的子组件将成为 flex 元素。 Then you don't have to know which tags each Material component spawns when component is rendered.然后,您不必知道每个 Material 组件在渲染组件时会生成哪些标签。

You can check this behavior in this example of Material docs: https://material-ui.com/components/grid/#nested-grid您可以在此材料文档示例中检查此行为: https://material-ui.com/components/grid/#nested-grid

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

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