简体   繁体   English

我想使用 react 以 3*X 的网格方式显示图像

[英]I want to display images in a grid fashion of 3*X using react

I am having an array list which comprises of image url.我有一个包含图像 url 的数组列表。 I want to bind each items into a div using react-bootstrap(library is not contraint but it should be compatible with react).我想使用 react-bootstrap 将每个项目绑定到一个 div(库不受约束,但它应该与 react 兼容)。 I want to align divs in a way that in each row I will have 3 images only and if there is 4th item in collection then it slides to next row.我想以一种方式对齐 div,在每一行中我将只有 3 个图像,如果集合中有第 4 个项目,则它会滑动到下一行。

Below is the collection下面是合集

const images = [
    {
      src:
        'https://images.unsplash.com/photo-1509420316987-d27b02f81864?dpr=1&auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 1000,
    },
    {
      src:
        'https://images.unsplash.com/photo-1509641498745-13c26fd1ed89?dpr=1&auto=format&fit=crop&w=1000&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 666,
      height: 1000,
    },
    {
      src:
        'https://images.unsplash.com/photo-1491146179969-d674118945ff?dpr=1&auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 844,
    },
{
      src:
        'https://images.unsplash.com/photo-1509420316987-d27b02f81864?dpr=1&auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 1000,
    },
    {
      src:
        'https://images.unsplash.com/photo-1509641498745-13c26fd1ed89?dpr=1&auto=format&fit=crop&w=1000&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 666,
      height: 1000,
    }
  ]
 

put all the images inside container and use grid将所有图像放入容器并使用网格

.container {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-row-gap: 10px;
}

img {
       width: 200px;
       height: 200px;
}

I would make a wrapping Div with display: flex.我会用 display: flex 制作一个包装 Div。

<div id="img-wrapper">
 <div><img src="your image" /></div>
 <div><img src="your image" /></div>
</div>

CSS: CSS:

#img-wrapper {
 display: flex;
 flex-wrap: wrap;
}

#img-wrapper > div {
 flex: 0 1 33%;
}

flex: tells the child-div if it should grow, shrink and which size it should have: flex: 告诉 child-div 它是否应该增长、收缩以及它应该具有的大小:

flex: "flex-grow" "flex-shrink" "flex-basis";弹性:“弹性增长”“弹性收缩”“弹性基础”;

edit: as Baruch Mashasha stated, grid would be even better for this.编辑:正如 Baruch Mashasha 所说,网格会更好。

I would use Bootstrap classes to create a grid for this, something like this...我会使用 Bootstrap 类为此创建一个网格,就像这样......

<div className="row">
 <div className="col"></div>
 <div className="col"></div>
 <div className="col"></div>
</div>

If you want to have another row you can just keep going with this idea.如果你想要另一排,你可以继续这个想法。 You can check here: https://getbootstrap.com/docs/4.0/layout/grid/ for a more thorough explanation, and to customize to your liking.您可以在此处查看: https : //getbootstrap.com/docs/4.0/layout/grid/以获得更详尽的解释,并根据您的喜好进行自定义。

暂无
暂无

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

相关问题 我想使用 React with hooks 随机化 css 类网格中的项目 - I want to randomize the css classes the items in a grid using React with hooks 尝试访问json内容并以网格方式显示 - Trying to access json content and display in a grid fashion 如何使用three.js等距显示10 x 10的多维数据集? - How to display cubes in 10 x 10 fashion with equal distance using three.js? 我想获取存储在 android 设备中的所有图像,以将它们显示到我的 React Native 应用程序中 - I want to get all the images stored in an android device to display them into my React Native app 使用PHP显示目录中的图像。 由于大小不同,我想一一显示。 - Using PHP to display images from a directory. I want to display it one by one since there are different size. 我想显示不同的图像OnMouseOver和OnMouseOut - I want to display different images OnMouseOver and OnMouseOut 我想使用php以网格形式显示来自mySQL数据库的项目,但我正在获取 - I want to display items from mySQL database in grid form using php but i'm getting this 为什么react-grid-gallery两次显示我的图像? - Why does react-grid-gallery display my images twice? 我有7张图片。 我想使用javascript向不同的用户随机显示不同的图像。 但是一旦显示,它就不会改变 - I have 7 images. I want to display different images to different users randomly using javascript. But once it is displayed, it should not change 我想在React中显示一个小吃店 - I want to display a Snack Bar in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM