Material UI Grid List Rows with a big awkward gap

不问归期 提交于 2021-01-28 05:49:15

问题


I am using Material UI with Reactjs. I have issues with the Grid List Component. I am trying to have a grid - 1000x1000px so I specified the height and width in the custom gridList style as 1000 and 1000 respectively as in the documentation. There should be 10 columns and each cell should have a height of 100px.

Problem comes when I have more than 1 row in the grid list. There is too big a gap between the row elements. I tried to override the CSS styles but none of them work nicely. I would expect the rows of grid cell to stack right below each other instead of having such a big gap in between.

Click here to see the awkward cell row gap

Here is my code,

import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';
import {GridList, GridTile} from 'material-ui/GridList';
import { Container } from 'semantic-ui-react';

const styles = {
  root: {
    "display": 'flex',
    "flexWrap": 'wrap',
    "justifyContent": 'space-around',
  },
  gridList: {
    "width": 1000,
    "height": 1000,
    "overflowY": 'auto',
  },
  indvCell: {
    "borderRadius": 25,
  }
};

const tilesData = [
  {
    img: '/img/sample/alex-wong-17993.jpg',
    title: 'Breakfast',
    author: 'jill111',
  },
  {
    img: '/img/sample/casey-horner-339165.jpg',
    title: 'Tasty burger',
    author: 'pashminu',
  },
  {
    img: '/img/sample/danny-postma-302063.jpg',
    title: 'Camera',
    author: 'Danson67',
  },
  {
    img: '/img/sample/derek-thomson-330312.jpg',
    title: 'Morning',
    author: 'fancycrave1',
  },
  {
    img: '/img/sample/hermansyah-352364.jpg',
    title: 'Hats',
    author: 'Hans',
  },
  {
    img: '/img/sample/kalen-emsley-99660.jpg',
    title: 'Honey',
    author: 'fancycravel',
  },
  {
    img: '/img/sample/lachlan-dempsey-397956.jpg',
    title: 'Vegetables',
    author: 'jill111',
  },
  {
    img: '/img/sample/linas-bam-223729.jpg',
    title: 'Water plant',
    author: 'BkrmadtyaKarki',
  },
  {
    img: '/img/sample/michal-kmet-257136.jpg',
    title: 'Water plant',
    author: 'BkrmadtyaKarki',
  },
  {
    img: '/img/sample/mohdammed-ali-340700.jpg',
    title: 'Water plant',
    author: 'BkrmadtyaKarki',
  },
  {
    img: '/img/sample/ng-55633.jpg',
    title: 'Water plant',
    author: 'BkrmadtyaKarki',
  },
  {
    img: '/img/sample/xan-griffin-419096.jpg',
    title: 'Water plant',
    author: 'BkrmadtyaKarki',
  },
];

class Blocks extends Component {

  render() {
    return (
      <Container>
        <div style={styles.root}>
          <GridList
            cellHeight={100}
            style={styles.gridList}
            cols={10}
          >
            {tilesData.map((tile) => (
              <GridTile
                key={tile.img}
                style={styles.indvCell}
              >
                <img src={tile.img} />
              </GridTile>
            ))}
          </GridList>
          </div>
      </Container>
    );
  }
}

My Material UI version is "material-ui": "^0.20.0"


回答1:


The issue in this case is the height defined in the gridList styles, it's forcing the container to stretch the cell containers out. Removing that or setting it to auto fixes the spacing:

const styles = {
  root: {
    "display": 'flex',
    "flexWrap": 'wrap',
    "justifyContent": 'space-around',
  },
  gridList: {
    "width": 1000,
    "height": 'auto',
    "overflowY": 'auto',
  },
  indvCell: {
    "borderRadius": 25,
  }
};



来源:https://stackoverflow.com/questions/48036086/material-ui-grid-list-rows-with-a-big-awkward-gap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!