由css网格提供动力的“复杂”矩阵样式网格是否可能?

问题描述 投票:1回答:1

我目前正在尝试使用CSS网格在Material-UI中实现以下网格结构:

Desired complex layout

我已经使用Material-UI创建了如下所示的CSS网格:

Current layout

这是它的代码:

import React from "react";
import clsx from "clsx";
import { makeStyles } from "@material-ui/core/styles";
import { Container } from "@material-ui/core";

const useStyles = makeStyles(theme => ({
    container: {
        marginTop: theme.spacing(8),
        display: "grid",
        gridTemplateColumns: "repeat(6, 56px)",
        gridAutoRows: "56px",
        gridGap: theme.spacing(0.5),
    },
    purple: {
        background: "#e502f9",
        color: "#ffffff",
        height: "100%",
    },
    orange: {
        background: "#f98202",
        color: "#ffffff",
        height: "100%",
        gridColumnStart: "2",
    },
    lightGrey: {
        background: "#d8d8d8",
        height: "100%",
    },
    topLabel: {
        gridColumnStart: "3",
        gridColumnEnd: "span 4",
    },
    sideLabel: {
        gridRowStart: "3",
        gridRowEnd: "span 4",
        writingMode: "vertical-rl",
        transform: "rotate(180deg)",
    },
    align: {
        alignItems: "center",
        display: "flex",
        justifyContent: "center",
        textAlign: "center",
    },
}));

function ExamplePage() {
    const classes = useStyles();

    return (
        <Container component="main" maxWidth="xs">
            <div className={classes.container}>
                <div />
                <div className={clsx(classes.topLabel, classes.align)}>
                    Text x-axis
                </div>
                <div className={clsx(classes.sideLabel, classes.align)}>
                    Text y-axis
                </div>

                <div />
                <div />
                <div className={clsx(classes.purple, classes.align)}>1</div>
                <div className={clsx(classes.purple, classes.align)}>2</div>
                <div className={clsx(classes.purple, classes.align)}>3</div>
                <div className={clsx(classes.purple, classes.align)}>4</div>

                <div className={clsx(classes.orange, classes.align)}>A</div>
                <div className={clsx(classes.lightGrey, classes.align)}>4</div>
                <div className={clsx(classes.lightGrey, classes.align)}>4</div>
                <div className={clsx(classes.lightGrey, classes.align)}>4</div>
                <div className={clsx(classes.lightGrey, classes.align)}>4</div>

                <div className={clsx(classes.orange, classes.align)}>B</div>
                <div className={clsx(classes.lightGrey, classes.align)}>3</div>
                <div className={clsx(classes.lightGrey, classes.align)}>3</div>
                <div className={clsx(classes.lightGrey, classes.align)}>3</div>
                <div className={clsx(classes.lightGrey, classes.align)}>3</div>

                <div className={clsx(classes.orange, classes.align)}>C</div>
                <div className={clsx(classes.lightGrey, classes.align)}>2</div>
                <div className={clsx(classes.lightGrey, classes.align)}>2</div>
                <div className={clsx(classes.lightGrey, classes.align)}>2</div>
                <div className={clsx(classes.lightGrey, classes.align)}>2</div>

                <div className={clsx(classes.orange, classes.align)}>D</div>
                <div className={clsx(classes.lightGrey, classes.align)}>1</div>
                <div className={clsx(classes.lightGrey, classes.align)}>1</div>
                <div className={clsx(classes.lightGrey, classes.align)}>1</div>
                <div className={clsx(classes.lightGrey, classes.align)}>1</div>
            </div>
        </Container>
    );
}

export default ExamplePage;

有人对如何以编程方式将蓝点设置在网格中的某个地方有想法吗?我不确定在CSS网格中是否完全可行?

html css material-ui css-grid
1个回答
0
投票
如@Michael_B所述,网格区域必须为矩形。因此,不可能创建更复杂的布局。
© www.soinside.com 2019 - 2024. All rights reserved.