简体   繁体   English

Material-UI 砌体:移除右侧空间

[英]Material-UI Masonry: Remove space on right side

Using Material-UI, the width of the Masonry Component doesn't fill the width of the parent container.使用 Material-UI,砌体组件的宽度不会填满父容器的宽度。 The width of this missing space is exactly the width of the spacing, which makes sense if there's an element next to it.这个缺失空间的宽度正好是间距的宽度,如果它旁边有一个元素,这是有道理的。

I tried to calculate the width of the masonry to be the width of the Box element plus 8 * spacing, but this breaks as soon as there is a scrollbar involved.我试图将砌体的宽度计算为 Box 元素的宽度加上 8 * 间距,但是一旦涉及滚动条,就会中断。

How can I use the full width of the container for Masonry?如何将容器的全宽用于砌体?

mwe (just an example from the documentation with a Box added on top): mwe(只是文档中的一个示例,顶部添加了一个 Box):


const heights = [150, 30, 90, 70, 110, 150, 130, 80, 50, 90, 100, 150, 30, 50, 80];

const Item = styled(Paper)(({ theme }) => ({
    ...theme.typography.body2,
    color: theme.palette.text.secondary,
    border: '1px solid black',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
}));

<Container>
    <Box style={{ border: '1px solid black', padding: '20px' }}>
        <Typography variant="h5">
          An Element to show the width of the contianer
        </Typography>
    </Box>

    <Box style={{ marginTop: '20px' }}>
        <Masonry columns={4} spacing={4}>
          {heights.map((height, index) => (
            <Item key={index} sx={{ height }}>
              {index + 1}
            </Item>
          ))}
        </Masonry>
    </Box>
</Container>

Screenshot of the MWE. MWE 的屏幕截图。 Missing Area marked in red:缺失区域标记为红色:

MWE 的屏幕截图。缺失区域标记为红色

You can fix this by setting marginRight with the negation of your masonry spacing in the sx prop.您可以通过设置marginRight来解决这个问题,并在sx道具中否定您的砌体间距。

<Box sx={{ marginTop: '20px', marginRight: -4 }}>
   {/* Masonry code */}
</Box>

I fix it simply by changing Masonry component width from "100%" to "auto", I don't know why, but it works great.我只是通过将 Masonry 组件宽度从“100%”更改为“auto”来修复它,我不知道为什么,但效果很好。

<Masonry columns={4} spacing={4} sx={{ width: "auto" }}>
  {* Masonry items *}
</Masonry>

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

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