javascript - Material-UI Masonry : Remove space on right side

标签 javascript reactjs material-ui masonry

使用 Material-UI,Masonry 组件的宽度不会填满父容器的宽度。这个缺失的空格的宽度恰好是间距的宽度,如果旁边有一个元素,这是有意义的。

我试图将 masonry 的宽度计算为 Box 元素的宽度加上 8 * 间距,但是一旦涉及滚动条,这就会中断。

如何使用 Masonry 容器的整个宽度?

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>

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

Screenshot of the MWE. Missing Area marked in red

最佳答案

我简单地通过将 Masonry 组件宽度从“100%”更改为“自动”来修复它, 我不知道为什么,但效果很好。

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

关于javascript - Material-UI Masonry : Remove space on right side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70922760/

相关文章:

javascript - 在故事书中用 react native 矢量图标 react native 网络问题

reactjs - 如何使徽章出现在 Material UI 中的卡片标题旁边

reactjs - React + Material-UI + Typescript : Inherit props from button to add different variants

reactjs - 如何使用 Material-Ui-Next Mini 变体抽屉正确布局主要内容

javascript - 尝试在异步函数中使用 bcrypt 散列密码

javascript - 在 Backbone.js 验证期间更改值

arrays - 如何为 Array<Object|string> 定义 PropType

reactjs - 如何在 JSS 中将样式应用于子类

JavaScript .replace() 只替换最后一个匹配项?

javascript - 删除所有按下的按键事件