最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I want to display images in a grid fashion of 3*X using react - Stack Overflow

programmeradmin0浏览0评论

I am having an array list which prises of image url. I want to bind each items into a div using react-bootstrap(library is not contraint but it should be patible with 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.

Below is the collection

const images = [
    {
      src:
        ';auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 1000,
    },
    {
      src:
        ';auto=format&fit=crop&w=1000&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 666,
      height: 1000,
    },
    {
      src:
        ';auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 844,
    },
{
      src:
        ';auto=format&fit=crop&w=1500&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 1500,
      height: 1000,
    },
    {
      src:
        ';auto=format&fit=crop&w=1000&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 666,
      height: 1000,
    }
  ]
 

I am having an array list which prises of image url. I want to bind each items into a div using react-bootstrap(library is not contraint but it should be patible with 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.

Below is the collection

const images = [
    {
      src:
        'https://images.unsplash./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./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./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./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./photo-1509641498745-13c26fd1ed89?dpr=1&auto=format&fit=crop&w=1000&q=80&cs=tinysrgb&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D',
      width: 666,
      height: 1000,
    }
  ]
 
Share Improve this question asked Oct 28, 2021 at 14:44 CoderInUiCoderInUi 1362 gold badges4 silver badges12 bronze badges 1
  • Should they be resized to have the same height/width? How should they be aligned? Usually I would simply do this with flexbox. – ShamPooSham Commented Oct 28, 2021 at 14:48
Add a ment  | 

2 Answers 2

Reset to default 3

I would make a wrapping Div with display: flex.

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

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: "flex-grow" "flex-shrink" "flex-basis";

edit: as Baruch Mashasha stated, grid would be even better for this.

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;
}
发布评论

评论列表(0)

  1. 暂无评论