【问题标题】:How do I apply gutters but not margins in Material UI grid?如何在 Material UI 网格中应用装订线而不是边距?
【发布时间】:2020-11-25 10:54:50
【问题描述】:

多年来,我一直在努力让我的响应式布局发挥作用!但是当我使用 class=spotlight 对 Grid 容器应用间距时,事情就会变得不协调。如何将间距应用到排水沟(即聚光灯容器网格内的网格项目之间)而不是边距(即网格外)。这是我的代码:

import React from "react";
import { Grid, makeStyles, Typography } from "@material-ui/core";
import MasterLayout from "../components/MasterLayout";
import ContentThumbnail from "../components/ContentThumbnail";
import ContentList from "../components/ContentList";

const useStyles = makeStyles((theme) => ({
  masterLayout: {
    padding: theme.spacing(3),
  },
  spotlight: {},
}));

const DefaultDashboard = ({ padding }) => {
  const classes = useStyles();
  return (
    <MasterLayout>
      {/*Top row with right bar */}
      <Grid
        container
        className={classes.masterLayout}
        spacing={3}
        alignItems="flex-start"
      >
        {/* Spotlight section */}
        <Grid container xl={8} lg={12} item className={classes.spotlight}>
          <Grid item xs={12}>
            <Typography variant="h3">Spotlight</Typography>
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
          <Grid item xl={3} lg={3} md={3} xs={6}>
            <ContentThumbnail />
          </Grid>
        </Grid>
        {/* List section 1 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 2</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 2 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 2</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 3 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 3</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
        {/* List section 4 */}
        <Grid container item xl={4} lg={3} md={6} xs={12}>
          <Grid item xs={12}>
            <Typography variant="h3">Title 4</Typography>
          </Grid>
          <Grid item xs={12}>
            <ContentList />
          </Grid>
        </Grid>
      </Grid>
    </MasterLayout>
  );
};

export default DefaultDashboard;

非常感谢,

凯蒂

【问题讨论】:

    标签: css reactjs material-ui


    【解决方案1】:

    TL;DR: 覆盖您的 Grid 项目的样式:

    const useStyles = makeStyles((theme) => ({
    //...
      gutter:{
        marginLeft: 80, // or something
        '&:first-child':{
          marginLeft: 0,
        }
      }
    }));
    //...
    <Grid ... item classes={{item:classes.gutter}}>
    

    以下网格项目有装订线(蓝色轮廓之间的装订线宽度为 80 像素,但第一个):

    NL;PR:看看这个pastebin。我查看了 Grid API,classes 对象允许您自定义组件的内部样式。我添加了一个伪类来为除第一个之外的所有孩子提供一些间距,从而添加内部排水沟。

    【讨论】:

    • 嗨,大卫 - 非常感谢您的帮助。由于某种原因,我正在努力实施它?我已经从网格中删除了所有间距,以便它仅由排水沟玻璃应用。但是,当我应用它时,它会将排水沟应用于所有孩子并且不会离开第一个。我和你的风格完全一样,我已经尝试将 classes={{item:classes.gutter}} 应用于类聚光灯内的每个网格项目(标题除外)。你能想到我哪里可能出错了吗?再次感谢
    • 你好——我意识到因为我有一个标题,我需要将代码修改为 &:nth-child(2)。但是现在可以了,非常感谢!我还需要进行修改以考虑在移动设备上会发生什么(例如,如果网格滑到下面,那么你想将排水沟应用到另一个孩子。我有 8 个组件,有时它会显示 4 个行,有时是 2。再次感谢!
    • 不客气,凯蒂。如果可能,请在 pastebin(如代码和框)中分享您的最终版本,也许您的更改可能会帮助其他开发人员,=]。
    • 遗憾的是,我的公司屏蔽了 pastebin 和类似网站。为了看到您的答案,我不得不继续查看个人平板电脑,这并不容易!我认为唯一的区别是使用第 n 个孩子而不是第一个孩子,因为我的 中的第一个孩子不是我想从中删除填充的东西。我还添加了 &:nth-child(5) 来满足第二行的 4 个项目。但是,我需要做一些额外的逻辑来适应屏幕较小的情况。我认为下面的链接是答案,但还没有弄清楚! material-ui.com/customization/breakpoints/#css-media-queries
    【解决方案2】:

    也许我没有正确理解你,但你可以尝试使用 'alignItems' 道具(更改为拉伸)做一些事情,或者尝试使用 'alignContent' 来代替。

    link 可能会有所帮助。

    抱歉,如果不是关于您需要的内容,可能会更容易帮助一些沙盒演示。

    【讨论】:

    • 非常感谢尤金的帮助。我想这样做并尝试过,但我就是无法让它发挥作用。这可能更多地与我的技能(或缺乏!)有关,而不是解决方案。再次感谢:)
    猜你喜欢
    • 2019-11-23
    • 2019-01-31
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2019-01-31
    • 2019-04-07
    相关资源
    最近更新 更多